Skip to content

Commit 9d1c85c

Browse files
Merge pull request #1955 from CodingTestStudy2/이진희
[이진희] Day02
2 parents 5bf5394 + f1ae9db commit 9d1c85c

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
3+
1. 아이디어 : 10진법 숫자를 2진법 변환시 모든 숫자가 0이나 1인 수의 개수
4+
경우의 수가 0,1,11,111,111 .. 로 정해져 있으므로 n이하의 조건을 만족하는 수만 미리 구하면 된다
5+
6+
2. 시간복잡도 : O(logN)
7+
8+
3. 자료구조/알고리즘 : 완전탐색
9+
10+
*/
11+
class Solution {
12+
public int countMonobit(int n) {
13+
// 포함되는 모든 bit가 같을때
14+
15+
//0, 1, 1+2, 1+2+4, 1+2+4+8 ..
16+
17+
List<Integer> list = new ArrayList<>();
18+
list.add(0);
19+
20+
int num = 0;
21+
int idx = 0;
22+
while(num<=n) {
23+
int add = (int)Math.pow(2,idx++);
24+
if((num+add)>n) break;
25+
num+=add;
26+
27+
list.add(num);
28+
}
29+
30+
return list.size();
31+
}
32+
}

0 commit comments

Comments
 (0)