File tree Expand file tree Collapse file tree 12 files changed +98
-30
lines changed
lcci/17.04.Missing Number Expand file tree Collapse file tree 12 files changed +98
-30
lines changed Original file line number Diff line number Diff line change @@ -39,9 +39,7 @@ class Solution:
39
39
def missingNumber (self , nums : List[int ]) -> int :
40
40
res = 0
41
41
for i, num in enumerate (nums):
42
- res ^= i
43
- res ^= num
44
- res ^= len (nums)
42
+ res = res ^ num ^ (i + 1 )
45
43
return res
46
44
```
47
45
@@ -52,12 +50,10 @@ class Solution:
52
50
``` java
53
51
class Solution {
54
52
public int missingNumber (int [] nums ) {
55
- int res = 0 , n = nums. length;
56
- for (int i = 0 ; i < n; ++ i) {
57
- res ^ = i;
58
- res ^ = nums[i];
53
+ int res = 0 ;
54
+ for (int i = 0 ; i < nums. length; ++ i) {
55
+ res = res ^ nums[i] ^ (i + 1 );
59
56
}
60
- res ^ = n;
61
57
return res;
62
58
}
63
59
}
@@ -79,6 +75,21 @@ var missingNumber = function(nums) {
79
75
};
80
76
```
81
77
78
+ ### ** C++**
79
+
80
+ ``` cpp
81
+ class Solution {
82
+ public:
83
+ int missingNumber(vector<int >& nums) {
84
+ int res = 0;
85
+ for (int i = 0; i < nums.size(); ++i) {
86
+ res = res ^ nums[ i] ^ (i + 1);
87
+ }
88
+ return res;
89
+ }
90
+ };
91
+ ```
92
+
82
93
### **...**
83
94
84
95
```
Original file line number Diff line number Diff line change @@ -39,9 +39,7 @@ class Solution:
39
39
def missingNumber (self , nums : List[int ]) -> int :
40
40
res = 0
41
41
for i, num in enumerate (nums):
42
- res ^= i
43
- res ^= num
44
- res ^= len (nums)
42
+ res = res ^ num ^ (i + 1 )
45
43
return res
46
44
```
47
45
@@ -50,12 +48,10 @@ class Solution:
50
48
``` java
51
49
class Solution {
52
50
public int missingNumber (int [] nums ) {
53
- int res = 0 , n = nums. length;
54
- for (int i = 0 ; i < n; ++ i) {
55
- res ^ = i;
56
- res ^ = nums[i];
51
+ int res = 0 ;
52
+ for (int i = 0 ; i < nums. length; ++ i) {
53
+ res = res ^ nums[i] ^ (i + 1 );
57
54
}
58
- res ^ = n;
59
55
return res;
60
56
}
61
57
}
@@ -77,6 +73,21 @@ var missingNumber = function(nums) {
77
73
};
78
74
```
79
75
76
+ ### ** C++**
77
+
78
+ ``` cpp
79
+ class Solution {
80
+ public:
81
+ int missingNumber(vector<int >& nums) {
82
+ int res = 0;
83
+ for (int i = 0; i < nums.size(); ++i) {
84
+ res = res ^ nums[ i] ^ (i + 1);
85
+ }
86
+ return res;
87
+ }
88
+ };
89
+ ```
90
+
80
91
### **...**
81
92
82
93
```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int missingNumber (vector<int >& nums) {
4
+ int res = 0 ;
5
+ for (int i = 0 ; i < nums.size (); ++i) {
6
+ res = res ^ nums[i] ^ (i + 1 );
7
+ }
8
+ return res;
9
+ }
10
+ };
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int missingNumber (int [] nums ) {
3
- int res = 0 , n = nums .length ;
4
- for (int i = 0 ; i < n ; ++i ) {
5
- res ^= i ;
6
- res ^= nums [i ];
3
+ int res = 0 ;
4
+ for (int i = 0 ; i < nums .length ; ++i ) {
5
+ res = res ^ nums [i ] ^ (i + 1 );
7
6
}
8
- res ^= n ;
9
7
return res ;
10
8
}
11
9
}
Original file line number Diff line number Diff line change @@ -2,7 +2,5 @@ class Solution:
2
2
def missingNumber (self , nums : List [int ]) -> int :
3
3
res = 0
4
4
for i , num in enumerate (nums ):
5
- res ^= i
6
- res ^= num
7
- res ^= len (nums )
5
+ res = res ^ num ^ (i + 1 )
8
6
return res
Original file line number Diff line number Diff line change @@ -91,6 +91,21 @@ func singleNumber(nums []int) int {
91
91
}
92
92
```
93
93
94
+ ### ** C++**
95
+
96
+ ``` cpp
97
+ class Solution {
98
+ public:
99
+ int singleNumber(vector<int >& nums) {
100
+ int res = 0;
101
+ for (auto num : nums) {
102
+ res ^= num;
103
+ }
104
+ return res;
105
+ }
106
+ };
107
+ ```
108
+
94
109
### **...**
95
110
96
111
```
Original file line number Diff line number Diff line change @@ -86,6 +86,21 @@ func singleNumber(nums []int) int {
86
86
}
87
87
```
88
88
89
+ ### ** C++**
90
+
91
+ ``` cpp
92
+ class Solution {
93
+ public:
94
+ int singleNumber(vector<int >& nums) {
95
+ int res = 0;
96
+ for (auto num : nums) {
97
+ res ^= num;
98
+ }
99
+ return res;
100
+ }
101
+ };
102
+ ```
103
+
89
104
### **...**
90
105
91
106
```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int singleNumber (vector<int >& nums) {
4
+ int res = 0 ;
5
+ for (auto num : nums) {
6
+ res ^= num;
7
+ }
8
+ return res;
9
+ }
10
+ };
Original file line number Diff line number Diff line change @@ -62,7 +62,7 @@ class Solution:
62
62
res = 0
63
63
for i in range (32 ):
64
64
if bits[i] % 3 != 0 :
65
- res + = (1 << i)
65
+ res | = (1 << i)
66
66
# 如果为负数,先将 0-32 位取反(即 res ^ 0xffffffff ),再将所有位取反(即 ~ )
67
67
return res if bits[31 ] % 3 == 0 else ~ (res ^ 0x ffffffff )
68
68
```
@@ -85,7 +85,7 @@ class Solution {
85
85
int res = 0 ;
86
86
for (int i = 0 ; i < 32 ; ++ i) {
87
87
if (bits[i] % 3 == 1 ) {
88
- res + = (1 << i);
88
+ res | = (1 << i);
89
89
}
90
90
}
91
91
return res;
Original file line number Diff line number Diff line change @@ -44,7 +44,7 @@ class Solution:
44
44
res = 0
45
45
for i in range (32 ):
46
46
if bits[i] % 3 != 0 :
47
- res + = (1 << i)
47
+ res | = (1 << i)
48
48
return res if bits[31 ] % 3 == 0 else ~ (res ^ 0x ffffffff )
49
49
```
50
50
@@ -64,7 +64,7 @@ class Solution {
64
64
int res = 0 ;
65
65
for (int i = 0 ; i < 32 ; ++ i) {
66
66
if (bits[i] % 3 == 1 ) {
67
- res + = (1 << i);
67
+ res | = (1 << i);
68
68
}
69
69
}
70
70
return res;
You can’t perform that action at this time.
0 commit comments