-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJY_150367.java
More file actions
44 lines (37 loc) Β· 1.31 KB
/
JY_150367.java
File metadata and controls
44 lines (37 loc) Β· 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.*;
class Solution {
public int[] solution(long[] numbers) {
int[] answer = new int[numbers.length];
for(int i=0; i<numbers.length; i++) {
long num = numbers[i];
// 0 μΆκ°νκΈ°
String ts = makeTree(Long.toBinaryString(num));
// System.out.println(ts);
// ν¬νμ΄μ§νΈλ¦¬κ° κ°λ₯νμ§ μ²΄ν¬
// λΆλͺ¨κ° μλλ° -> μμμ μμ
if(checkS(ts)) answer[i] = 1;
}
return answer;
}
public static String makeTree(String s) {
int size = s.length();
int n = 1;
while(size > (1 << n)-1) {
n++;
}
int cnt = (1 << n) - size - 1;
return "0".repeat(cnt) + s;
}
public static boolean checkS(String now) {
// 리νλ
Έλ
if(now.length() == 1) return true;
int rIdx = now.length() / 2;
// λΆλͺ¨κ° μλλ° μμμ΄ μ‘΄μ¬
String left = now.substring(0, rIdx);
String right = now.substring(rIdx+1);
if(now.charAt(rIdx) == '0'
&& (left.contains("1") || right.contains("1"))) return false;
// κ°λ₯νλ€λ©΄ λ€μ νμ νΈλ¦¬ νμ
return checkS(left) && checkS(right);
}
}