There was an error while loading. Please reload this page.
2 parents 4e67e1d + c96c4dc commit c6ad8b9Copy full SHA for c6ad8b9
1 file changed
leetcode3/염혜정/476. Number Complement.java
@@ -0,0 +1,33 @@
1
+// 10진수 -> 2진수
2
+// 1과 0을 서로 바꿈
3
+// 2진수 -> 10진수
4
+
5
6
+class Solution {
7
+ public int findComplement(int num) {
8
+ StringBuilder sb = new StringBuilder();
9
+ while (num>0) {
10
+ int bit = num%2;
11
+ // 0과 1을 체인지
12
+ if (bit == 0) sb.append(1);
13
+ else sb.append(0);
14
+ num /= 2;
15
+ }
16
17
+ String ten = sb.toString();
18
+ int result = ten.charAt(0) - '0';
19
+ if (ten.length() == 1) return result;
20
21
+ for (int i = 1; i<ten.length(); i++) {
22
+ int digit = ten.charAt(i) - '0';
23
+ if (digit == 0) continue;
24
+ int pow = 1;
25
+ for (int cnt = 0; cnt<i; cnt++) {
26
+ pow *= 2;
27
28
+ result += pow;
29
30
31
+ return result;
32
33
+}
0 commit comments