Skip to content

Commit 263ac07

Browse files
committed
476. Number Complement
1 parent 4fe9969 commit 263ac07

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
3+
1. 아이디어 : 10진법을 이진법으로 바꾸고, 1과 0을 바꾼 후, 값을 계산하는 문제
4+
10진법이 포함되는 이진법의 범위를 파악후, 원래 값의 num을 빼면 된다
5+
6+
2. 시간복잡도 : O(31)
7+
8+
3. 자료구조/알고리즘 : 비트 연산
9+
10+
*/
11+
12+
class Solution {
13+
public int findComplement(int num) {
14+
int sum = 1;
15+
int idx = 1;
16+
while(num>sum) {
17+
sum += idx*2;
18+
idx*=2;
19+
}
20+
21+
return sum - num;
22+
}
23+
}

0 commit comments

Comments
 (0)