요깨비's LAB

[프로그래머스, Java] 비밀지도 본문

알고리즘(Java)/프로그래머스

[프로그래머스, Java] 비밀지도

요깨비 2022. 1. 8. 15:01

class Solution {
    public String[] solution(int n, int[] arr1, int[] arr2) {
        String[] answer;
        String[] result = new String[n];

        for(int i=0;i<n;i++) {
            StringBuilder strArr1 = new StringBuilder();
            StringBuilder strArr2 = new StringBuilder();
            int n1 = arr1[i];
            int n2 = arr2[i];

            strArr1.append(Integer.toBinaryString(n1));
            while(strArr1.length() < n) {
                strArr1.insert(0, "0");
            }

            strArr2.append(Integer.toBinaryString(n2));
            while (strArr2.length() < n) {
                strArr2.insert(0, "0");
            }

            char[] carr1 = strArr1.toString().toCharArray();
            char[] carr2 = strArr2.toString().toCharArray();

            StringBuilder sb = new StringBuilder();
            for(int j=0;j<n;j++) {
                int c = (carr1[j] - '0') + (carr2[j] - '0');

                if(c == 0) {
                    sb.append(" ");
                }else {
                    sb.append("#");
                }
            }
            result[i] = sb.toString();
        }

        return result;
    }
}

 

Comments