File tree 7 files changed +93
-29
lines changed
solution/0600-0699/0645.Set Mismatch
7 files changed +93
-29
lines changed Original file line number Diff line number Diff line change 42
42
<!-- 这里可写当前语言的特殊实现逻辑 -->
43
43
44
44
``` python
45
-
45
+ class Solution :
46
+ def minCount (self , coins : List[int ]) -> int :
47
+ return sum ((coin + 1 ) // 2 for coin in coins)
46
48
```
47
49
48
50
### ** Java**
49
51
50
52
<!-- 这里可写当前语言的特殊实现逻辑 -->
51
53
52
54
``` java
55
+ class Solution {
56
+ public int minCount (int [] coins ) {
57
+ int ans = 0 ;
58
+ for (int coin : coins) {
59
+ ans += (coin + 1 ) / 2 ;
60
+ }
61
+ return ans;
62
+ }
63
+ }
64
+ ```
65
+
66
+ ### ** C++**
67
+
68
+ ``` cpp
69
+ class Solution {
70
+ public:
71
+ int minCount(vector<int >& coins) {
72
+ int ans = 0;
73
+ for (int coin : coins) ans += (coin + 1) / 2;
74
+ return ans;
75
+ }
76
+ };
77
+ ```
78
+
79
+ ### **Go**
53
80
81
+ ```go
82
+ func minCount(coins []int) int {
83
+ ans := 0
84
+ for _, coin := range coins {
85
+ ans += (coin + 1) / 2
86
+ }
87
+ return ans
88
+ }
54
89
```
55
90
56
91
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int minCount (vector<int >& coins) {
4
+ int ans = 0 ;
5
+ for (int coin : coins) ans += (coin + 1 ) / 2 ;
6
+ return ans;
7
+ }
8
+ };
Original file line number Diff line number Diff line change
1
+ func minCount (coins []int ) int {
2
+ ans := 0
3
+ for _ , coin := range coins {
4
+ ans += (coin + 1 ) / 2
5
+ }
6
+ return ans
7
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int minCount (int [] coins ) {
3
+ int ans = 0 ;
4
+ for (int coin : coins ) {
5
+ ans += (coin + 1 ) / 2 ;
6
+ }
7
+ return ans ;
8
+ }
9
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def minCount (self , coins : List [int ]) -> int :
3
+ return sum ((coin + 1 ) // 2 for coin in coins )
Original file line number Diff line number Diff line change @@ -85,7 +85,6 @@ class Solution {
85
85
int eor = 0 ;
86
86
for (int i = 1 ; i <= nums. length; ++ i) {
87
87
eor ^ = (i ^ nums[i - 1 ]);
88
- <<<<<< < Updated upstream
89
88
}
90
89
int diff = eor & (~ eor + 1 );
91
90
int a = 0 ;
@@ -158,27 +157,14 @@ public:
158
157
if ((nums[ i - 1] & diff) == 0) {
159
158
a ^= nums[ i - 1] ;
160
159
}
161
- =======
162
- }
163
- int diff = eor & (~eor + 1 );
164
- int a = 0 ;
165
- for (int i = 1 ; i <= nums.length; ++i) {
166
- if ((nums[i - 1] & diff) == 0) {
167
- a ^= nums[i - 1];
168
- }
169
- >>>>>>> Stashed changes
170
160
if ((i & diff) == 0) {
171
161
a ^= i;
172
162
}
173
163
}
174
164
int b = eor ^ a;
175
165
for (int num : nums) {
176
166
if (a == num) {
177
- <<<<<<< Updated upstream
178
167
return {a, b};
179
- =======
180
- return new int[]{a, b};
181
- >>>>>>> Stashed changes
182
168
}
183
169
}
184
170
return {b, a};
Original file line number Diff line number Diff line change @@ -61,7 +61,6 @@ class Solution {
61
61
int eor = 0 ;
62
62
for (int i = 1 ; i <= nums. length; ++ i) {
63
63
eor ^ = (i ^ nums[i - 1 ]);
64
- <<<<<< < Updated upstream
65
64
}
66
65
int diff = eor & (~ eor + 1 );
67
66
int a = 0 ;
@@ -134,27 +133,14 @@ public:
134
133
if ((nums[ i - 1] & diff) == 0) {
135
134
a ^= nums[ i - 1] ;
136
135
}
137
- =======
138
- }
139
- int diff = eor & (~eor + 1 );
140
- int a = 0 ;
141
- for (int i = 1 ; i <= nums.length; ++i) {
142
- if ((nums[i - 1] & diff) == 0) {
143
- a ^= nums[i - 1];
144
- }
145
- >>>>>>> Stashed changes
146
136
if ((i & diff) == 0) {
147
137
a ^= i;
148
138
}
149
139
}
150
140
int b = eor ^ a;
151
141
for (int num : nums) {
152
142
if (a == num) {
153
- <<<<<<< Updated upstream
154
143
return {a, b};
155
- =======
156
- return new int[]{a, b};
157
- >>>>>>> Stashed changes
158
144
}
159
145
}
160
146
return {b, a};
@@ -164,6 +150,8 @@ public:
164
150
165
151
### **Go**
166
152
153
+ 把每个数都放到它应该在的位置,最后出现“异常”的就是重复的数和丢失的数。
154
+
167
155
```go
168
156
func findErrorNums(nums []int) []int {
169
157
n := len(nums)
@@ -181,6 +169,34 @@ func findErrorNums(nums []int) []int {
181
169
}
182
170
```
183
171
172
+ 也可以使用位运算。
173
+
174
+ ``` go
175
+ func findErrorNums (nums []int ) []int {
176
+ eor , n := 0 , len (nums)
177
+ for i := 1 ; i <= n; i++ {
178
+ eor ^= (i ^ nums[i-1 ])
179
+ }
180
+ diff := eor & (-eor)
181
+ a := 0
182
+ for i := 1 ; i <= n; i++ {
183
+ if (nums[i-1 ] & diff) == 0 {
184
+ a ^= nums[i-1 ]
185
+ }
186
+ if (i & diff) == 0 {
187
+ a ^= i
188
+ }
189
+ }
190
+ b := eor ^ a
191
+ for _ , num := range nums {
192
+ if a == num {
193
+ return []int {a, b}
194
+ }
195
+ }
196
+ return []int {b, a}
197
+ }
198
+ ```
199
+
184
200
### ** C++**
185
201
186
202
``` cpp
You can’t perform that action at this time.
0 commit comments