Contents

๊ทธ๋ฆผ ํ™•๋Œ€(with.Java)

   Nov 28, 2023     2 min read

โ€œ๊ทธ๋ฆผ ํ™•๋Œ€โ€ ๋ฌธ์ œ์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณธ ๊ธ€์ž…๋‹ˆ๋‹ค.

์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉฐ, ํ’€์—ˆ๋˜ ๋ฌธ์ œ์— ๋Œ€ํ•œ ํšŒ๊ณ ์™€ ๋‹ค๋ฅธ ํ’€์ด ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๋ฉฐ, ์•Œ์•„๊ฐ€๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ œ์— ๋Œ€ํ•ด ๋จผ์ € ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ

์ง์‚ฌ๊ฐํ˜• ํ˜•ํƒœ์˜ ๊ทธ๋ฆผ ํŒŒ์ผ์ด ์žˆ๊ณ , ์ด ๊ทธ๋ฆผ ํŒŒ์ผ์€ 1 ร— 1 ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜• ํฌ๊ธฐ์˜ ํ”ฝ์…€๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ๊ทธ๋ฆผ ํŒŒ์ผ์„ ๋‚˜ํƒ€๋‚ธ ๋ฌธ์ž์—ด ๋ฐฐ์—ด picture๊ณผ ์ •์ˆ˜ k๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ด ๊ทธ๋ฆผ ํŒŒ์ผ์„ ๊ฐ€๋กœ ์„ธ๋กœ๋กœ k๋ฐฐ ๋Š˜๋ฆฐ ๊ทธ๋ฆผ ํŒŒ์ผ์„ ๋‚˜ํƒ€๋‚ด๋„๋ก ๋ฌธ์ž์—ด ๋ฐฐ์—ด์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

picturekresult
[โ€œx.xโ€, โ€œ.x.โ€, โ€œx.xโ€]3[โ€œxxxโ€ฆxxxโ€, โ€œxxxโ€ฆxxxโ€, โ€œxxxโ€ฆxxxโ€, โ€œโ€ฆxxxโ€ฆโ€, โ€œโ€ฆxxxโ€ฆโ€, โ€œโ€ฆxxxโ€ฆโ€, โ€œxxxโ€ฆxxxโ€, โ€œxxxโ€ฆxxxโ€, โ€œxxxโ€ฆxxxโ€]

๋ฌธ์ œ์— ๋Œ€ํ•œ ๋‚˜์˜ ํ’€์ด

class Solution {
    public String[] solution(String[] picture, int k) {
        int rows = picture.length;
        int cols = picture[0].length();

        String[] answer = new String[rows * k];

        StringBuilder tempStr = new StringBuilder();
        int count = 0;

        for (String str : picture) {
            for (int i = 0; i < str.length(); i++) {
                char ch = str.charAt(i);
                for (int j = 0; j < k; j++) {
                    tempStr.append(ch);
                }
            }

            for (int j = 0; j < k; j++) {
                answer[count++] = tempStr.toString();
            }

            tempStr.setLength(0);
        }

        return answer;
    }
}

ํ’€์ด ์„ค๋ช…

int rows = picture.length;์™€ int cols = picture[0].length();: ์ฃผ์–ด์ง„ picture ๋ฐฐ์—ด์˜ ํ–‰๊ณผ ์—ด ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

String[] answer = new String[rows * k];: ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•  ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด ๋ฐฐ์—ด answer๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” rows * k๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

StringBuilder tempStr = new StringBuilder();: ์ž„์‹œ ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ StringBuilder ๊ฐ์ฒด tempStr์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

int count = 0;: ๊ฒฐ๊ณผ ๋ฐฐ์—ด answer์˜ ์ธ๋ฑ์Šค๋ฅผ ์ถ”์ ํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜ count๋ฅผ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.

for (String str : picture) : ์ฃผ์–ด์ง„ picture ๋ฐฐ์—ด์˜ ๊ฐ ๋ฌธ์ž์—ด str์— ๋Œ€ํ•ด ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

for (int i = 0; i < str.length(); i++) : ๊ฐ ๋ฌธ์ž์—ด str์˜ ๋ฌธ์ž๋ฅผ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ:

๋ฌธ์ž ch๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฌธ์ž ch๋ฅผ k๋ฒˆ ๋ฐ˜๋ณตํ•˜์—ฌ tempStr์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. for (int j = 0; j < k; j++) : ๋ฌธ์ž์—ด tempStr์„ k๋ฒˆ ๋ฐ˜๋ณตํ•˜์—ฌ answer ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

tempStr.setLength(0);: ์ž„์‹œ ๋ฌธ์ž์—ด tempStr์„ ๋น„์›Œ์ค๋‹ˆ๋‹ค.

return answer;: ์ƒˆ๋กœ ์ƒ์„ฑ๋œ ๋ฌธ์ž์—ด ๋ฐฐ์—ด answer๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.