File tree 13 files changed +285
-32
lines changed
lcof/面试题56 - II. 数组中数字出现的次数 II
0600-0699/0645.Set Mismatch
13 files changed +285
-32
lines changed Original file line number Diff line number Diff line change 23
23
- ` 1 <= nums[i] < 2^31 `
24
24
25
25
## 解法
26
+
27
+ 统计所有数字每个位中 1 出现的次数,对于某个位,1 出现的次数一定是 3 的倍数 +1 或 0。对这个数 %3 得到的结果就是那个出现一次的数字在该位上的值。
28
+
29
+
26
30
<!-- tabs:start -->
27
31
28
32
### ** Python3**
@@ -34,11 +38,10 @@ class Solution:
34
38
for i in range (32 ):
35
39
bits[i] += (num & 1 )
36
40
num >>= 1
37
-
38
41
res = 0
39
42
for i in range (32 ):
40
43
if bits[i] % 3 == 1 :
41
- res += pow ( 2 , i)
44
+ res += ( 1 << i)
42
45
return res
43
46
```
44
47
@@ -56,7 +59,7 @@ class Solution {
56
59
int res = 0 ;
57
60
for (int i = 0 ; i < 32 ; ++ i) {
58
61
if (bits[i] % 3 == 1 ) {
59
- res += (int ) Math . pow( 2 , i);
62
+ res += (1 << i);
60
63
}
61
64
}
62
65
return res;
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ public int singleNumber(int[] nums) {
10
10
int res = 0 ;
11
11
for (int i = 0 ; i < 32 ; ++i ) {
12
12
if (bits [i ] % 3 == 1 ) {
13
- res += (int ) Math . pow ( 2 , i );
13
+ res += (1 << i );
14
14
}
15
15
}
16
16
return res ;
Original file line number Diff line number Diff line change @@ -5,9 +5,8 @@ def singleNumber(self, nums: List[int]) -> int:
5
5
for i in range (32 ):
6
6
bits [i ] += (num & 1 )
7
7
num >>= 1
8
-
9
8
res = 0
10
9
for i in range (32 ):
11
10
if bits [i ] % 3 == 1 :
12
- res += pow ( 2 , i )
11
+ res += ( 1 << i )
13
12
return res
Original file line number Diff line number Diff line change 26
26
## 解法
27
27
<!-- 这里可写通用的实现逻辑 -->
28
28
29
+ 异或运算求解。
30
+
31
+ 首先明确,两个相同的数异或之后的结果为 0。对该数组所有元素进行异或运算,结果就是那个只出现一次的数字。
29
32
30
33
<!-- tabs:start -->
31
34
32
35
### ** Python3**
33
36
<!-- 这里可写当前语言的特殊实现逻辑 -->
34
37
35
38
``` python
36
-
39
+ class Solution :
40
+ def singleNumber (self , nums : List[int ]) -> int :
41
+ res = 0
42
+ for num in nums:
43
+ res ^= num
44
+ return res
37
45
```
38
46
39
47
### ** Java**
40
48
<!-- 这里可写当前语言的特殊实现逻辑 -->
41
49
42
50
``` java
43
-
51
+ class Solution {
52
+ public int singleNumber (int [] nums ) {
53
+ int res = 0 ;
54
+ for (int num : nums) {
55
+ res ^ = num;
56
+ }
57
+ return res;
58
+ }
59
+ }
44
60
```
45
61
46
62
### ** ...**
Original file line number Diff line number Diff line change 52
52
### ** Python3**
53
53
54
54
``` python
55
-
55
+ class Solution :
56
+ def singleNumber (self , nums : List[int ]) -> int :
57
+ res = 0
58
+ for num in nums:
59
+ res ^= num
60
+ return res
56
61
```
57
62
58
63
### ** Java**
59
64
60
65
``` java
61
-
66
+ class Solution {
67
+ public int singleNumber (int [] nums ) {
68
+ int res = 0 ;
69
+ for (int num : nums) {
70
+ res ^ = num;
71
+ }
72
+ return res;
73
+ }
74
+ }
62
75
```
63
76
64
77
### ** ...**
Original file line number Diff line number Diff line change 1
1
class Solution :
2
- def singleNumber (self , nums ):
3
- """
4
- :type nums: List[int]
5
- :rtype: int
6
- """
7
- res = 0
8
- for i in nums :
9
- res = res ^ i
10
- return res
2
+ def singleNumber (self , nums : List [int ]) -> int :
3
+ res = 0
4
+ for num in nums :
5
+ res ^= num
6
+ return res
Original file line number Diff line number Diff line change 26
26
## 解法
27
27
<!-- 这里可写通用的实现逻辑 -->
28
28
29
+ 统计所有数字每个位中 1 出现的次数,对于某个位,1 出现的次数一定是 3 的倍数 +1 或 0。对这个数 %3 得到的结果就是那个出现一次的数字在该位上的值。
29
30
30
31
<!-- tabs:start -->
31
32
40
41
<!-- 这里可写当前语言的特殊实现逻辑 -->
41
42
42
43
``` java
43
-
44
+ class Solution {
45
+ public int singleNumber (int [] nums ) {
46
+ int [] bits = new int [32 ];
47
+ for (int num : nums) {
48
+ for (int i = 0 ; i < 32 ; ++ i) {
49
+ bits[i] += (num & 1 );
50
+ num >> = 1 ;
51
+ }
52
+ }
53
+
54
+ int res = 0 ;
55
+ for (int i = 0 ; i < 32 ; ++ i) {
56
+ if (bits[i] % 3 == 1 ) {
57
+ res += (1 << i);
58
+ }
59
+ }
60
+ return res;
61
+ }
62
+ }
44
63
```
45
64
46
65
### ** ...**
Original file line number Diff line number Diff line change 56
56
### ** Java**
57
57
58
58
``` java
59
-
59
+ class Solution {
60
+ public int singleNumber (int [] nums ) {
61
+ int [] bits = new int [32 ];
62
+ for (int num : nums) {
63
+ for (int i = 0 ; i < 32 ; ++ i) {
64
+ bits[i] += (num & 1 );
65
+ num >> = 1 ;
66
+ }
67
+ }
68
+
69
+ int res = 0 ;
70
+ for (int i = 0 ; i < 32 ; ++ i) {
71
+ if (bits[i] % 3 == 1 ) {
72
+ res += (1 << i);
73
+ }
74
+ }
75
+ return res;
76
+ }
77
+ }
60
78
```
61
79
62
80
### ** ...**
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int singleNumber (int [] nums ) {
3
3
int [] bits = new int [32 ];
4
- int n = nums . length ;
5
- for (int i = 0 ; i < n ; ++i ) {
6
- for ( int j = 0 ; j < 32 ; ++ j ) {
7
- bits [ j ] += (( nums [ i ] >> j ) & 1 ) ;
4
+ for ( int num : nums ) {
5
+ for (int i = 0 ; i < 32 ; ++i ) {
6
+ bits [ i ] += ( num & 1 );
7
+ num >>= 1 ;
8
8
}
9
9
}
10
-
10
+
11
11
int res = 0 ;
12
12
for (int i = 0 ; i < 32 ; ++i ) {
13
- if (bits [i ] % 3 != 0 ) {
13
+ if (bits [i ] % 3 == 1 ) {
14
14
res += (1 << i );
15
15
}
16
16
}
17
17
return res ;
18
-
19
18
}
20
19
}
Original file line number Diff line number Diff line change 27
27
## 解法
28
28
<!-- 这里可写通用的实现逻辑 -->
29
29
30
+ 首先使用 1 到 n 的所有数字做异或运算,然后再与数组中的所有数字异或,得到的值就是缺失数字与重复的数字异或的结果。
31
+
32
+ 接着计算中这个值中其中一个非零的位 pos。然后 pos 位是否为 1,将 nums 数组的元素分成两部分,分别异或;接着将 ` 1~n ` 的元素也分成两部分,分别异或。得到的两部分结果分别为 a,b,即是缺失数字与重复数字。
33
+
34
+ 最后判断数组中是否存在 a 或 b,若存在 a,说明重复数字是 a,返回 ` [a,b] ` ,否则返回 ` [b,a] ` 。
30
35
31
36
<!-- tabs:start -->
32
37
33
38
### ** Python3**
34
39
<!-- 这里可写当前语言的特殊实现逻辑 -->
35
40
36
41
``` python
37
-
42
+ class Solution :
43
+ def findErrorNums (self , nums : List[int ]) -> List[int ]:
44
+ res = 0
45
+ for num in nums:
46
+ res ^= num
47
+ for i in range (1 , len (nums) + 1 ):
48
+ res ^= i
49
+ pos = 0
50
+ while (res & 1 ) == 0 :
51
+ res >>= 1
52
+ pos += 1
53
+ a = b = 0
54
+ for num in nums:
55
+ if ((num >> pos) & 1 ) == 0 :
56
+ a ^= num
57
+ else :
58
+ b ^= num
59
+ for i in range (1 , len (nums) + 1 ):
60
+ if ((i >> pos) & 1 ) == 0 :
61
+ a ^= i
62
+ else :
63
+ b ^= i
64
+ for num in nums:
65
+ if num == a:
66
+ return [a, b]
67
+ return [b, a]
38
68
```
39
69
40
70
### ** Java**
41
71
<!-- 这里可写当前语言的特殊实现逻辑 -->
42
72
43
73
``` java
44
-
74
+ class Solution {
75
+ public int [] findErrorNums (int [] nums ) {
76
+ int res = 0 ;
77
+ for (int num : nums) {
78
+ res ^ = num;
79
+ }
80
+ for (int i = 1 , n = nums. length; i < n + 1 ; ++ i) {
81
+ res ^ = i;
82
+ }
83
+ int pos = 0 ;
84
+ while ((res & 1 ) == 0 ) {
85
+ res >> = 1 ;
86
+ ++ pos;
87
+ }
88
+ int a = 0 , b = 0 ;
89
+ for (int num : nums) {
90
+ if (((num >> pos) & 1 ) == 0 ) {
91
+ a ^ = num;
92
+ } else {
93
+ b ^ = num;
94
+ }
95
+ }
96
+ for (int i = 1 , n = nums. length; i < n + 1 ; ++ i) {
97
+ if (((i >> pos) & 1 ) == 0 ) {
98
+ a ^ = i;
99
+ } else {
100
+ b ^ = i;
101
+ }
102
+ }
103
+ for (int num : nums) {
104
+ if (num == a) {
105
+ return new int []{a, b};
106
+ }
107
+ }
108
+ return new int []{b, a};
109
+ }
110
+ }
45
111
```
46
112
47
113
### ** ...**
Original file line number Diff line number Diff line change @@ -56,13 +56,74 @@ Given an array <code>nums</code> representing the data status of this set after
56
56
### ** Python3**
57
57
58
58
``` python
59
-
59
+ class Solution :
60
+ def findErrorNums (self , nums : List[int ]) -> List[int ]:
61
+ res = 0
62
+ for num in nums:
63
+ res ^= num
64
+ for i in range (1 , len (nums) + 1 ):
65
+ res ^= i
66
+ pos = 0
67
+ while (res & 1 ) == 0 :
68
+ res >>= 1
69
+ pos += 1
70
+ a = b = 0
71
+ for num in nums:
72
+ if ((num >> pos) & 1 ) == 0 :
73
+ a ^= num
74
+ else :
75
+ b ^= num
76
+ for i in range (1 , len (nums) + 1 ):
77
+ if ((i >> pos) & 1 ) == 0 :
78
+ a ^= i
79
+ else :
80
+ b ^= i
81
+ for num in nums:
82
+ if num == a:
83
+ return [a, b]
84
+ return [b, a]
60
85
```
61
86
62
87
### ** Java**
63
88
64
89
``` java
65
-
90
+ class Solution {
91
+ public int [] findErrorNums (int [] nums ) {
92
+ int res = 0 ;
93
+ for (int num : nums) {
94
+ res ^ = num;
95
+ }
96
+ for (int i = 1 , n = nums. length; i < n + 1 ; ++ i) {
97
+ res ^ = i;
98
+ }
99
+ int pos = 0 ;
100
+ while ((res & 1 ) == 0 ) {
101
+ res >> = 1 ;
102
+ ++ pos;
103
+ }
104
+ int a = 0 , b = 0 ;
105
+ for (int num : nums) {
106
+ if (((num >> pos) & 1 ) == 0 ) {
107
+ a ^ = num;
108
+ } else {
109
+ b ^ = num;
110
+ }
111
+ }
112
+ for (int i = 1 , n = nums. length; i < n + 1 ; ++ i) {
113
+ if (((i >> pos) & 1 ) == 0 ) {
114
+ a ^ = i;
115
+ } else {
116
+ b ^ = i;
117
+ }
118
+ }
119
+ for (int num : nums) {
120
+ if (num == a) {
121
+ return new int []{a, b};
122
+ }
123
+ }
124
+ return new int []{b, a};
125
+ }
126
+ }
66
127
```
67
128
68
129
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int [] findErrorNums (int [] nums ) {
3
+ int res = 0 ;
4
+ for (int num : nums ) {
5
+ res ^= num ;
6
+ }
7
+ for (int i = 1 , n = nums .length ; i < n + 1 ; ++i ) {
8
+ res ^= i ;
9
+ }
10
+ int pos = 0 ;
11
+ while ((res & 1 ) == 0 ) {
12
+ res >>= 1 ;
13
+ ++pos ;
14
+ }
15
+ int a = 0 , b = 0 ;
16
+ for (int num : nums ) {
17
+ if (((num >> pos ) & 1 ) == 0 ) {
18
+ a ^= num ;
19
+ } else {
20
+ b ^= num ;
21
+ }
22
+ }
23
+ for (int i = 1 , n = nums .length ; i < n + 1 ; ++i ) {
24
+ if (((i >> pos ) & 1 ) == 0 ) {
25
+ a ^= i ;
26
+ } else {
27
+ b ^= i ;
28
+ }
29
+ }
30
+ for (int num : nums ) {
31
+ if (num == a ) {
32
+ return new int []{a , b };
33
+ }
34
+ }
35
+ return new int []{b , a };
36
+ }
37
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def findErrorNums (self , nums : List [int ]) -> List [int ]:
3
+ res = 0
4
+ for num in nums :
5
+ res ^= num
6
+ for i in range (1 , len (nums ) + 1 ):
7
+ res ^= i
8
+ pos = 0
9
+ while (res & 1 ) == 0 :
10
+ res >>= 1
11
+ pos += 1
12
+ a = b = 0
13
+ for num in nums :
14
+ if ((num >> pos ) & 1 ) == 0 :
15
+ a ^= num
16
+ else :
17
+ b ^= num
18
+ for i in range (1 , len (nums ) + 1 ):
19
+ if ((i >> pos ) & 1 ) == 0 :
20
+ a ^= i
21
+ else :
22
+ b ^= i
23
+ for num in nums :
24
+ if num == a :
25
+ return [a , b ]
26
+ return [b , a ]
You can’t perform that action at this time.
0 commit comments