Skip to content

Commit ffe9948

Browse files
authored
Create 1012._Complement_of_Base_10_Integer.md
1 parent 8319b71 commit ffe9948

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# 1012. Complement of Base 10 Integer
2+
3+
**<font color=red>难度: Easy</font>**
4+
5+
## 刷题内容
6+
7+
> 原题连接
8+
9+
* https://leetcode.com/problems/complement-of-base-10-integer/
10+
11+
> 内容描述
12+
13+
```
14+
Every non-negative integer N has a binary representation. For example, 5 can be represented as "101" in binary, 11 as "1011" in binary, and so on. Note that except for N = 0, there are no leading zeroes in any binary representation.
15+
16+
The complement of a binary representation is the number in binary you get when changing every 1 to a 0 and 0 to a 1. For example, the complement of "101" in binary is "010" in binary.
17+
18+
For a given number N in base-10, return the complement of it's binary representation as a base-10 integer.
19+
20+
21+
22+
Example 1:
23+
24+
Input: 5
25+
Output: 2
26+
Explanation: 5 is "101" in binary, with complement "010" in binary, which is 2 in base-10.
27+
Example 2:
28+
29+
Input: 7
30+
Output: 0
31+
Explanation: 7 is "111" in binary, with complement "000" in binary, which is 0 in base-10.
32+
Example 3:
33+
34+
Input: 10
35+
Output: 5
36+
Explanation: 10 is "1010" in binary, with complement "0101" in binary, which is 5 in base-10.
37+
38+
39+
Note:
40+
41+
0 <= N < 10^9
42+
```
43+
44+
## 解题方案
45+
46+
> 思路 1
47+
******- 时间复杂度: O(lgN)******- 空间复杂度: O(lgN)******
48+
49+
一行
50+
51+
```python
52+
class Solution:
53+
def bitwiseComplement(self, N: int) -> int:
54+
return int(''.join(['0' if i == '1' else '1' for i in bin(N)[2:]]), 2)
55+
```

0 commit comments

Comments
 (0)