Skip to content

Commit fafe9c8

Browse files
authored
feat: add solutions to lcof2 problem: No.004 (doocs#1419)
No.004.只出现一次的数字
1 parent 583a135 commit fafe9c8

File tree

6 files changed

+101
-70
lines changed

6 files changed

+101
-70
lines changed

lcof2/剑指 Offer II 004. 只出现一次的数字/README.md

+45-26
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47-
统计所有数字每个位中 1 出现的次数,对于某个位,1 出现的次数一定是 3 的倍数 +1 或 0。对这个数 %3 得到的结果就是那个出现一次的数字在该位上的值。
47+
**方法一:位运算**
48+
49+
我们可以统计所有数字中每个位上出现的 $1$ 的个数,然后对 $3$ 取模。如果某一位上的出现的 $1$ 的个数无法被 $3$ 整除,说明只出现一次的数字在该位上是 $1$,否则是 $0$。
50+
51+
时间复杂度 $O(n \times \log M)$,其中 $n$ 是数组 $nums$ 的长度,而 $M$ 是数组中元素的最大值。空间复杂度 $O(1)$。
4852

4953
<!-- tabs:start -->
5054

@@ -57,7 +61,7 @@ class Solution:
5761
def singleNumber(self, nums: List[int]) -> int:
5862
ans = 0
5963
for i in range(32):
60-
cnt = sum(num >> i & 1 for num in nums)
64+
cnt = sum(x >> i & 1 for x in nums)
6165
if cnt % 3:
6266
if i == 31:
6367
ans -= 1 << i
@@ -74,10 +78,10 @@ class Solution:
7478
class Solution {
7579
public int singleNumber(int[] nums) {
7680
int ans = 0;
77-
for (int i = 0; i < 32; i++) {
81+
for (int i = 0; i < 32; ++i) {
7882
int cnt = 0;
79-
for (int num : nums) {
80-
cnt += num >> i & 1;
83+
for (int x : nums) {
84+
cnt += x >> i & 1;
8185
}
8286
cnt %= 3;
8387
ans |= cnt << i;
@@ -87,25 +91,6 @@ class Solution {
8791
}
8892
```
8993

90-
### **Go**
91-
92-
需要注意 Golang 中的 `int` 在 64 位平台上相当于 `int64`
93-
94-
```go
95-
func singleNumber(nums []int) int {
96-
ans := int32(0)
97-
for i := 0; i < 32; i++ {
98-
cnt := int32(0)
99-
for _, num := range nums {
100-
cnt += int32(num) >> i & 1
101-
}
102-
cnt %= 3
103-
ans |= cnt << i
104-
}
105-
return int(ans)
106-
}
107-
```
108-
10994
### **C++**
11095

11196
```cpp
@@ -115,8 +100,8 @@ public:
115100
int ans = 0;
116101
for (int i = 0; i < 32; ++i) {
117102
int cnt = 0;
118-
for (int num : nums) {
119-
cnt += ((num >> i) & 1);
103+
for (int x : nums) {
104+
cnt += (x >> i) & 1;
120105
}
121106
cnt %= 3;
122107
ans |= cnt << i;
@@ -126,6 +111,40 @@ public:
126111
};
127112
```
128113
114+
### **Go**
115+
116+
```go
117+
func singleNumber(nums []int) int {
118+
var ans int32
119+
for i := 0; i < 32; i++ {
120+
cnt := 0
121+
for _, x := range nums {
122+
cnt += x >> i & 1
123+
}
124+
cnt %= 3
125+
ans |= int32(cnt) << i
126+
}
127+
return int(ans)
128+
}
129+
```
130+
131+
### **TypeScript**
132+
133+
```ts
134+
function singleNumber(nums: number[]): number {
135+
let ans = 0;
136+
for (let i = 0; i < 32; ++i) {
137+
let cnt = 0;
138+
for (const x of nums) {
139+
cnt += (x >> i) & 1;
140+
}
141+
cnt %= 3;
142+
ans |= cnt << i;
143+
}
144+
return ans;
145+
}
146+
```
147+
129148
### **...**
130149

131150
```
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
class Solution {
2-
public:
3-
int singleNumber(vector<int>& nums) {
4-
int ans = 0;
5-
for (int i = 0; i < 32; ++i) {
6-
int cnt = 0;
7-
for (int num : nums) {
8-
cnt += ((num >> i) & 1);
9-
}
10-
cnt %= 3;
11-
ans |= cnt << i;
12-
}
13-
return ans;
14-
}
1+
class Solution {
2+
public:
3+
int singleNumber(vector<int>& nums) {
4+
int ans = 0;
5+
for (int i = 0; i < 32; ++i) {
6+
int cnt = 0;
7+
for (int x : nums) {
8+
cnt += (x >> i) & 1;
9+
}
10+
cnt %= 3;
11+
ans |= cnt << i;
12+
}
13+
return ans;
14+
}
1515
};
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
func singleNumber(nums []int) int {
2-
ans := int32(0)
2+
var ans int32
33
for i := 0; i < 32; i++ {
4-
cnt := int32(0)
5-
for _, num := range nums {
6-
cnt += int32(num) >> i & 1
4+
cnt := 0
5+
for _, x := range nums {
6+
cnt += x >> i & 1
77
}
88
cnt %= 3
9-
ans |= cnt << i
9+
ans |= int32(cnt) << i
1010
}
1111
return int(ans)
1212
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
class Solution {
2-
public int singleNumber(int[] nums) {
3-
int ans = 0;
4-
for (int i = 0; i < 32; i++) {
5-
int cnt = 0;
6-
for (int num : nums) {
7-
cnt += num >> i & 1;
8-
}
9-
cnt %= 3;
10-
ans |= cnt << i;
11-
}
12-
return ans;
13-
}
14-
}
1+
class Solution {
2+
public int singleNumber(int[] nums) {
3+
int ans = 0;
4+
for (int i = 0; i < 32; ++i) {
5+
int cnt = 0;
6+
for (int x : nums) {
7+
cnt += x >> i & 1;
8+
}
9+
cnt %= 3;
10+
ans |= cnt << i;
11+
}
12+
return ans;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
class Solution:
2-
def singleNumber(self, nums: List[int]) -> int:
3-
ans = 0
4-
for i in range(32):
5-
cnt = sum(num >> i & 1 for num in nums)
6-
if cnt % 3:
7-
if i == 31:
8-
ans -= 1 << i
9-
else:
10-
ans |= 1 << i
11-
return ans
1+
class Solution:
2+
def singleNumber(self, nums: List[int]) -> int:
3+
ans = 0
4+
for i in range(32):
5+
cnt = sum(x >> i & 1 for x in nums)
6+
if cnt % 3:
7+
if i == 31:
8+
ans -= 1 << i
9+
else:
10+
ans |= 1 << i
11+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function singleNumber(nums: number[]): number {
2+
let ans = 0;
3+
for (let i = 0; i < 32; ++i) {
4+
let cnt = 0;
5+
for (const x of nums) {
6+
cnt += (x >> i) & 1;
7+
}
8+
cnt %= 3;
9+
ans |= cnt << i;
10+
}
11+
return ans;
12+
}

0 commit comments

Comments
 (0)