[programmers] ๊ทธ๋ฆผ ํ๋ - Java
์๋ฐ๋ก ์์ฑ๋ ๋ฌธ์ ํด๊ฒฐ ๋ฐฉ๋ฒ์ 1x1 ํฝ์
๋ก ์ด๋ฃจ์ด์ง ๊ทธ๋ฆผ ํ์ผ์ ๊ฐ๋ก ์ธ๋ก๋ก k๋ฐฐ ๋๋ฆฌ๋ ๊ฒ์
๋๋ค. StringBuilder์ append ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ k๋ฐฐ ๋งํผ ๋ฌธ์๋ฅผ ์ถ๊ฐํ๊ณ , ์ด๋ฅผ k๋ฒ ๋ฐ๋ณตํ์ฌ ArrayList์ ์ถ๊ฐํฉ๋๋ค. ๋ง์ง๋ง์ผ๋ก ArrayList๋ฅผ ๋ฌธ์์ด ๋ฐฐ์ด๋ก ๋ณํํ์ฌ ๋ฐํํฉ๋๋ค. ์ด ๋ฌธ์ ๋ฅผ ํตํด StringBuilder์ ๋ฉ์๋ ์ฌ์ฉ๋ฒ๊ณผ ArrayList๋ฅผ ๋ฌธ์์ด ๋ฐฐ์ด๋ก ๋ณํํ๋ ๋ฐฉ๋ฒ์ ์ตํ ์ ์์ต๋๋ค.
Jan 21, 2024
๋ฌธ์ ์ค๋ช
์ง์ฌ๊ฐํ ํํ์ ๊ทธ๋ฆผ ํ์ผ์ด ์๊ณ , ์ด ๊ทธ๋ฆผ ํ์ผ์ 1 ร 1 ํฌ๊ธฐ์ ์ ์ฌ๊ฐํ ํฌ๊ธฐ์ ํฝ์
๋ก ์ด๋ฃจ์ด์ ธ ์์ต๋๋ค. ์ด ๊ทธ๋ฆผ ํ์ผ์ ๋ํ๋ธ ๋ฌธ์์ด ๋ฐฐ์ด
picture
๊ณผ ์ ์ k
๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, ์ด ๊ทธ๋ฆผ ํ์ผ์ ๊ฐ๋ก ์ธ๋ก๋ก k
๋ฐฐ ๋๋ฆฐ ๊ทธ๋ฆผ ํ์ผ์ ๋ํ๋ด๋๋ก ๋ฌธ์์ด ๋ฐฐ์ด์ return ํ๋ solution ํจ์๋ฅผ ์์ฑํด ์ฃผ์ธ์.์ ํ์ฌํญ
- 1 โค
picture
์ ๊ธธ์ด โค 20
- 1 โค
picture
์ ์์์ ๊ธธ์ด โค 20
- ๋ชจ๋
picture
์ ์์์ ๊ธธ์ด๋ ๊ฐ์ต๋๋ค.
picture
์ ์์๋ '.'๊ณผ 'x'๋ก ์ด๋ฃจ์ด์ ธ ์์ต๋๋ค.
- 1 โค
k
โค 10
์ ์ถ๋ ฅ ์
picture | k | result |
[".xx...xx.", "x..x.x..x", "x...x...x", ".x.....x.", "..x...x..", "...x.x...", "....x...."] | 2 | ["..xxxx......xxxx..", "..xxxx......xxxx..", "xx....xx..xx....xx", "xx....xx..xx....xx", "xx......xx......xx", "xx......xx......xx", "..xx..........xx..", "..xx..........xx..", "....xx......xx....", "....xx......xx....", "......xx..xx......", "......xx..xx......", "........xx........", "........xx........"] |
["x.x", ".x.", "x.x"] | 3 | ["xxx...xxx", "xxx...xxx", "xxx...xxx", "...xxx...", "...xxx...", "...xxx...", "xxx...xxx", "xxx...xxx", "xxx...xxx"] |
์ ์ถ๋ ฅ ์ ์ค๋ช
์
์ถ๋ ฅ ์ #1
- ์์ 1๋ฒ์
picture
๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
.xx...xx.
x..x.x..x
x...x...x
.x.....x.
..x...x..
...x.x...
....x....
์ด๋ฅผ ๊ฐ๋ก ์ธ๋ก๋ก
k
๋ฐฐ, ์ฆ 2๋ฐฐ ํ๋ํ๋ฉด ๊ทธ๋ฆผ ํ์ผ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค...xxxx......xxxx..
..xxxx......xxxx..
xx....xx..xx....xx
xx....xx..xx....xx
xx......xx......xx
xx......xx......xx
..xx..........xx..
..xx..........xx..
....xx......xx....
....xx......xx....
......xx..xx......
......xx..xx......
........xx........
........xx........
๋ฐ๋ผ์ ["..xxxx......xxxx..", "..xxxx......xxxx..", "xx....xx..xx....xx", "xx....xx..xx....xx", "xx......xx......xx", "xx......xx......xx", "..xx..........xx..", "..xx..........xx..", "....xx......xx....", "....xx......xx....", "......xx..xx......", "......xx..xx......", "........xx........", "........xx........"]๋ฅผ return ํฉ๋๋ค.
์
์ถ๋ ฅ ์ #2
- ์์ 2๋ฒ์
picture
๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
x.x
.x.
x.x
์ด๋ฅผ ๊ฐ๋ก ์ธ๋ก๋ก
k
๋ฐฐ, ์ฆ 3๋ฐฐ ํ๋ํ๋ฉด ๊ทธ๋ฆผ ํ์ผ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.xxx...xxx
xxx...xxx
xxx...xxx
...xxx...
...xxx...
...xxx...
xxx...xxx
xxx...xxx
xxx...xxx
๋ฐ๋ผ์ ["xxx...xxx", "xxx...xxx", "xxx...xxx", "...xxx...", "...xxx...", "...xxx...", "xxx...xxx", "xxx...xxx", "xxx...xxx"]๋ฅผ return ํฉ๋๋ค.
solution.java
import java.util.*; class Solution { public String[] solution(String[] picture, int k) { List<String> list = new ArrayList<>(); StringBuilder sb = new StringBuilder(); for(int i=0; i<picture.length; i++){ for(int j=0; j<picture[i].length(); j++){ for(int x=0; x<k; x++){ sb.append(picture[i].charAt(j)); } } for(int j=0; j<k; j++){ list.add(sb.toString()); } sb.delete(0, sb.length()); } return list.toArray(new String[list.size()]); } }
ํต์ฌ ํค์๋
- StringBuilder์ ๊ฐ์ ์ ๋ ฅํ๊ณ ์ถ์ผ๋ฉด .append(์ถ๊ฐํ ๋ฌธ์ ๋๋ ๋ฌธ์์ด) ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
- StringBuilder๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํ๊ณ ์ถ์ผ๋ฉด .toString() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
- StringBuilder๋ฅผ ์ด๊ธฐํํ๊ณ ์ถ์ผ๋ฉด .delete(์์ ์ธ๋ฑ์ค, ๋ ์ธ๋ฑ์ค) ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
- ArrayList๋ฅผ ๋ฌธ์์ด ๋ฐฐ์ด๋ก ๋ณํํ๊ณ ์ถ์ผ๋ฉด .toArray() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
๊ฒฐ๋ก !
ํด๋น ๋ฌธ์ ๋ฅผ ํ๋ฉด์ StringBuilder์ ๋ฉ์๋์ ์ฌ์ฉ๋ฒ์ ์ตํ ์ ์์๊ณ , ArrayList๋ฅผ ๋ฌธ์์ด ๋ฐฐ์ด๋ก ๋ณ๊ฒฝํ๋ ค๋ฉด .toArray() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค๋ ๊ฒ์ ์ตํ ์ ์์๋ค.
Share article