본문 바로가기
Algorithm/Programmers

[Programmers] Lv.0 / 그림 확대 / Java

by unknownomad 2024. 3. 7.

문제

 

풀이

import java.util.Arrays;

class Solution {
    public String[] solution(String[] picture, int k) {
        int rows = picture.length;
        int cols = picture[0].length(); // 조건 : 모든 picture의 원소의 길이는 같습니다.

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

        for (int i = 0; i < rows * k; i++) {
            StringBuilder row = new StringBuilder();

            for (int j = 0; j < cols * k; j++) {
                char pixel = picture[i / k].charAt(j / k);
                row.append(String.valueOf(pixel));
            }
            enlargedPicture[i] = row.toString();
        }
        return enlargedPicture;
    }
}
class Solution {
    public String[] solution(String[] picture, int k) {
        
        int rows = picture.length;
        String[] answer = new String[rows * k];
        int idx = 0;

        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < k; j++) {
                StringBuilder sb = new StringBuilder();
                for(int l = 0; l < picture[i].length(); l++) {
                    sb.append(String.valueOf(picture[i].charAt(l)).repeat(k));
                }
                answer[idx++] = sb.toString();
            }
        }
        return answer;
    }
}

 

출처

https://school.programmers.co.kr/learn/courses/30/lessons/181836

댓글