[programmers] ๊ทธ๋ฆผ ํ™•๋Œ€ - Java

์ž๋ฐ”๋กœ ์ž‘์„ฑ๋œ ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์€ 1x1 ํ”ฝ์…€๋กœ ์ด๋ฃจ์–ด์ง„ ๊ทธ๋ฆผ ํŒŒ์ผ์„ ๊ฐ€๋กœ ์„ธ๋กœ๋กœ k๋ฐฐ ๋Š˜๋ฆฌ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. StringBuilder์˜ append ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ k๋ฐฐ ๋งŒํผ ๋ฌธ์ž๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ์ด๋ฅผ k๋ฒˆ ๋ฐ˜๋ณตํ•˜์—ฌ ArrayList์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ArrayList๋ฅผ ๋ฌธ์ž์—ด ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํ†ตํ•ด StringBuilder์˜ ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ๋ฒ•๊ณผ ArrayList๋ฅผ ๋ฌธ์ž์—ด ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ตํž ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
Jan 21, 2024
[programmers] ๊ทธ๋ฆผ ํ™•๋Œ€ - Java

๋ฌธ์ œ ์„ค๋ช…

์ง์‚ฌ๊ฐํ˜• ํ˜•ํƒœ์˜ ๊ทธ๋ฆผ ํŒŒ์ผ์ด ์žˆ๊ณ , ์ด ๊ทธ๋ฆผ ํŒŒ์ผ์€ 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

More articles

See more posts

[์ด๊ฒƒ์ด ์ž๋ฐ”๋‹ค] 18์žฅ ์ •๋ฆฌ

January 22, 2024
[์ด๊ฒƒ์ด ์ž๋ฐ”๋‹ค] 18์žฅ ์ •๋ฆฌ

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ปDriedPollack's Blog