ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

[๋ฌธ์ œ]

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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

[ํ’€์ด]

์šฐ์„  arr1[]๊ณผ arr2[]์˜ ์ˆซ์ž๋“ค์„ binaryNumber()์„ ํ†ตํ•ด 2์ง„์ˆ˜์˜ String ๋ณ€์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์œผ๋กœ ํ•ด๋‹น String๋ณ€์ˆ˜๋ฅผ split()์„ ํ†ตํ•ด bit๋กœ ์ชผ๊ฐ ํ›„, || ์—ฐ์‚ฐ์„ ํ†ตํ•ด ๋‘˜์ค‘ 1๊ฐœ๊ฐ€ 1์ด๋ผ๋ฉด #์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

 

[์ฝ”๋“œ]

class Solution {
    public String[] solution(int n, int[] arr1, int[] arr2) {
        String[] answer = new String[n];
        for(int i=0;i<n;i++) {
            String[] s1 = binaryNumber(arr1[i], n).split("");
            String[] s2 = binaryNumber(arr2[i], n).split("");
            String result = "";
            for(int j=0;j<n;j++) {
                if(s1[j].equals("1") || s2[j].equals("1")) {
                    result += "#";
                } else {
                    result += " ";
                }
            }
            answer[i] = result;
        }
        return answer;
    }
    public static String binaryNumber(int num, int n) {
        String str = "";
        while(num > 0) {
            if(num%2 == 0) {
                str = 0 + str;
            } else {
                str = 1 + str;
            }
            num /= 2;
        }
        int len = str.length();
        if(len < n) {
            for(int i=0;i<n-len;i++) {
                str = 0 + str;
            }
        }
        return str;
    }
}