-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberComplement.java
30 lines (27 loc) · 1.02 KB
/
NumberComplement.java
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
package easy;
/**
* Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
* Note:
* The given integer is guaranteed to fit within the range of a 32-bit signed integer.
* You could assume no leading zero bit in the integer’s binary representation.
* Example:
* Input: 5 Output: 2
* Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
*
* 解决思路:
* 直接翻转二进制
* 没懂为啥直接翻转叫这个是 complement number 补码。。?
*
* Created by second on 2017/9/3.
*/
public class NumberComplement {
public int findComplement(int num) {
String a = Integer.toBinaryString(num);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < a.length(); i++){
if ('0' == a.charAt(i)) sb.append(1);
if ('1' == a.charAt(i)) sb.append(0);
}
return Integer.parseInt(sb.toString(), 2);
}
}