File tree Expand file tree Collapse file tree 4 files changed +194
-4
lines changed
solution/0600-0699/0645.Set Mismatch Expand file tree Collapse file tree 4 files changed +194
-4
lines changed Original file line number Diff line number Diff line change 2727## 解法
2828<!-- 这里可写通用的实现逻辑 -->
2929
30+ 首先使用 1 到 n 的所有数字做异或运算,然后再与数组中的所有数字异或,得到的值就是缺失数字与重复的数字异或的结果。
31+
32+ 接着计算中这个值中其中一个非零的位 pos。然后 pos 位是否为 1,将 nums 数组的元素分成两部分,分别异或;接着将 ` 1~n ` 的元素也分成两部分,分别异或。得到的两部分结果分别为 a,b,即是缺失数字与重复数字。
33+
34+ 最后判断数组中是否存在 a 或 b,若存在 a,说明重复数字是 a,返回 ` [a,b] ` ,否则返回 ` [b,a] ` 。
3035
3136<!-- tabs:start -->
3237
3338### ** Python3**
3439<!-- 这里可写当前语言的特殊实现逻辑 -->
3540
3641``` 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]
3868```
3969
4070### ** Java**
4171<!-- 这里可写当前语言的特殊实现逻辑 -->
4272
4373``` 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+ }
45111```
46112
47113### ** ...**
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
5656### ** Python3**
5757
5858``` 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]
6085```
6186
6287### ** Java**
6388
6489``` 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+ }
66127```
67128
68129### ** ...**
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