Skip to content

Commit 8020b53

Browse files
committed
feat: 20260311 check in
1 parent 4dc42e7 commit 8020b53

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 1009. 十进制整数的反码
2+
3+
> **日期**:2026-03-09
4+
> **所用时间**:3min
5+
> **知识点**:位运算
6+
7+
## 1. 构造反码
8+
9+
先把整数 \(n\) 转成二进制字符串,然后从高位到低位依次遍历每一位:
10+
如果当前位是 `0`,在答案里加一位 `1`;如果当前位是 `1`,在答案里加一位 `0`,相当于逐位构造补码。
11+
12+
复杂度分析:
13+
14+
- 时间复杂度:$O(\log n)$
15+
- 空间复杂度:$O(1)$
16+
17+
**Python3**
18+
19+
```python
20+
class Solution:
21+
def bitwiseComplement(self, n: int) -> int:
22+
ans = 0
23+
for c in bin(n)[2:]:
24+
ans = (ans << 1) + 1 - int(c)
25+
return ans
26+
```

0 commit comments

Comments
 (0)