From 9d08eba4d82dc83d001249a7e68ca5373629722a Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 14 Oct 2023 17:07:10 +0800 Subject: [PATCH 1/2] feat: add solutions to lc problem: No.0268 No.0268.Missing Number --- .../0100-0199/0136.Single Number/README.md | 2 +- .../0100-0199/0136.Single Number/README_EN.md | 11 +++ .../0200-0299/0268.Missing Number/README.md | 61 +++++++++++++++- .../0268.Missing Number/README_EN.md | 69 +++++++++++++++++++ .../0200-0299/0268.Missing Number/Solution.rs | 10 +++ .../0200-0299/0268.Missing Number/Solution.ts | 8 +++ 6 files changed, 158 insertions(+), 3 deletions(-) create mode 100644 solution/0200-0299/0268.Missing Number/Solution.rs create mode 100644 solution/0200-0299/0268.Missing Number/Solution.ts diff --git a/solution/0100-0199/0136.Single Number/README.md b/solution/0100-0199/0136.Single Number/README.md index 3fb23aef36913..c521433a81668 100644 --- a/solution/0100-0199/0136.Single Number/README.md +++ b/solution/0100-0199/0136.Single Number/README.md @@ -60,7 +60,7 @@ 我们对该数组所有元素进行异或运算,结果就是那个只出现一次的数字。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是数组 `nums` 的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是数组 $nums$ 的长度。 diff --git a/solution/0100-0199/0136.Single Number/README_EN.md b/solution/0100-0199/0136.Single Number/README_EN.md index 2476665e167e5..31a6386f58b35 100644 --- a/solution/0100-0199/0136.Single Number/README_EN.md +++ b/solution/0100-0199/0136.Single Number/README_EN.md @@ -30,6 +30,17 @@ ## Solutions +**Solution 1: Bitwise Operation** + +The XOR operation has the following properties: + +- Any number XOR 0 is still the original number, i.e., $x \oplus 0 = x$; +- Any number XOR itself is 0, i.e., $x \oplus x = 0$; + +Performing XOR operation on all elements in the array will result in the number that only appears once. + +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. + ### **Python3** diff --git a/solution/0200-0299/0268.Missing Number/README.md b/solution/0200-0299/0268.Missing Number/README.md index 2ac110459e347..23ca26d64479b 100644 --- a/solution/0200-0299/0268.Missing Number/README.md +++ b/solution/0200-0299/0268.Missing Number/README.md @@ -62,7 +62,12 @@ **方法一:位运算** -对于数组中的每个元素,都可以与下标进行异或运算,最终的结果就是缺失的数字。 +异或运算的性质: + +- 任何数和 $0$ 做异或运算,结果仍然是原来的数,即 $x \oplus 0 = x$; +- 任何数和其自身做异或运算,结果是 $0$,即 $x \oplus x = 0$; + +因此,我们可以遍历数组,将数字 $[0,..n]$ 与数组中的元素进行异或运算,最后的结果就是缺失的数字。 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度。 @@ -70,7 +75,7 @@ 我们也可以用数学求解。求出 $[0,..n]$ 的和,减去数组中所有数的和,就得到了缺失的数字。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度。 +时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。 @@ -171,6 +176,58 @@ func missingNumber(nums []int) (ans int) { } ``` +### **Rust** + +```rust +impl Solution { + pub fn missing_number(nums: Vec) -> i32 { + let n = nums.len() as i32; + let mut ans = n; + for (i, v) in nums.iter().enumerate() { + ans ^= i as i32 ^ v; + } + ans + } +} +``` + +```rust +impl Solution { + pub fn missing_number(nums: Vec) -> i32 { + let n = nums.len() as i32; + let mut ans = n; + for (i, &v) in nums.iter().enumerate() { + ans += i as i32 - v; + } + ans + } +} +``` + +### **TypeScript** + +```ts +function missingNumber(nums: number[]): number { + const n = nums.length; + let ans = n; + for (let i = 0; i < n; ++i) { + ans ^= i ^ nums[i]; + } + return ans; +} +``` + +```ts +function missingNumber(nums: number[]): number { + const n = nums.length; + let ans = n; + for (let i = 0; i < n; ++i) { + ans += i - nums[i]; + } + return ans; +} +``` + ### **JavaScript** ```js diff --git a/solution/0200-0299/0268.Missing Number/README_EN.md b/solution/0200-0299/0268.Missing Number/README_EN.md index c2aa4a5e7f24b..30196a67fd671 100644 --- a/solution/0200-0299/0268.Missing Number/README_EN.md +++ b/solution/0200-0299/0268.Missing Number/README_EN.md @@ -46,6 +46,23 @@ ## Solutions +**Solution 1: Bitwise Operation** + +The XOR operation has the following properties: + +- Any number XOR 0 is still the original number, i.e., $x \oplus 0 = x$; +- Any number XOR itself is 0, i.e., $x \oplus x = 0$; + +Therefore, we can traverse the array, perform XOR operation between each element and the numbers $[0,..n]$, and the final result will be the missing number. + +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. + +**Solution 2: Mathematics** + +We can also solve this problem using mathematics. By calculating the sum of $[0,..n]$, subtracting the sum of all numbers in the array, we can obtain the missing number. + +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. + ### **Python3** @@ -141,6 +158,58 @@ func missingNumber(nums []int) (ans int) { } ``` +### **Rust** + +```rust +impl Solution { + pub fn missing_number(nums: Vec) -> i32 { + let n = nums.len() as i32; + let mut ans = n; + for (i, v) in nums.iter().enumerate() { + ans ^= i as i32 ^ v; + } + ans + } +} +``` + +```rust +impl Solution { + pub fn missing_number(nums: Vec) -> i32 { + let n = nums.len() as i32; + let mut ans = n; + for (i, &v) in nums.iter().enumerate() { + ans += i as i32 - v; + } + ans + } +} +``` + +### **TypeScript** + +```ts +function missingNumber(nums: number[]): number { + const n = nums.length; + let ans = n; + for (let i = 0; i < n; ++i) { + ans ^= i ^ nums[i]; + } + return ans; +} +``` + +```ts +function missingNumber(nums: number[]): number { + const n = nums.length; + let ans = n; + for (let i = 0; i < n; ++i) { + ans += i - nums[i]; + } + return ans; +} +``` + ### **JavaScript** ```js diff --git a/solution/0200-0299/0268.Missing Number/Solution.rs b/solution/0200-0299/0268.Missing Number/Solution.rs new file mode 100644 index 0000000000000..a3047794ed9db --- /dev/null +++ b/solution/0200-0299/0268.Missing Number/Solution.rs @@ -0,0 +1,10 @@ +impl Solution { + pub fn missing_number(nums: Vec) -> i32 { + let n = nums.len() as i32; + let mut ans = n; + for (i, v) in nums.iter().enumerate() { + ans ^= i as i32 ^ v; + } + ans + } +} \ No newline at end of file diff --git a/solution/0200-0299/0268.Missing Number/Solution.ts b/solution/0200-0299/0268.Missing Number/Solution.ts new file mode 100644 index 0000000000000..0ba735f7d49a2 --- /dev/null +++ b/solution/0200-0299/0268.Missing Number/Solution.ts @@ -0,0 +1,8 @@ +function missingNumber(nums: number[]): number { + const n = nums.length; + let ans = n; + for (let i = 0; i < n; ++i) { + ans ^= i ^ nums[i]; + } + return ans; +} From 3064d662ea184e5a2ebf8416909f7ff8e155f03c Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 14 Oct 2023 18:03:07 +0800 Subject: [PATCH 2/2] feat: add solutions to lc problem: No.0645 No.0645.Set Mismatch --- .../README_EN.md | 8 ++ .../0600-0699/0645.Set Mismatch/README.md | 85 ++++++++++++++----- .../0600-0699/0645.Set Mismatch/README_EN.md | 85 ++++++++++++++----- .../0600-0699/0645.Set Mismatch/Solution.cpp | 8 +- .../0600-0699/0645.Set Mismatch/Solution.go | 8 +- .../0600-0699/0645.Set Mismatch/Solution.java | 8 +- .../0600-0699/0645.Set Mismatch/Solution.py | 8 +- .../0600-0699/0645.Set Mismatch/Solution.rs | 25 ++++++ .../0600-0699/0645.Set Mismatch/Solution.ts | 8 +- 9 files changed, 181 insertions(+), 62 deletions(-) create mode 100644 solution/0600-0699/0645.Set Mismatch/Solution.rs diff --git a/solution/0200-0299/0287.Find the Duplicate Number/README_EN.md b/solution/0200-0299/0287.Find the Duplicate Number/README_EN.md index 9b616b978e6fa..654de635a701d 100644 --- a/solution/0200-0299/0287.Find the Duplicate Number/README_EN.md +++ b/solution/0200-0299/0287.Find the Duplicate Number/README_EN.md @@ -45,6 +45,14 @@ ## Solutions +**Solution 1: Binary Search** + +We can observe that if the number of elements in $[1,..x]$ is greater than $x$, then the duplicate number must be in $[1,..x]$, otherwise the duplicate number must be in $[x+1,..n]$. + +Therefore, we can use binary search to find $x$, and check whether the number of elements in $[1,..x]$ is greater than $x$ at each iteration. This way, we can determine which interval the duplicate number is in, and narrow down the search range until we find the duplicate number. + +The time complexity is $O(n \times \log n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$. + ### **Python3** diff --git a/solution/0600-0699/0645.Set Mismatch/README.md b/solution/0600-0699/0645.Set Mismatch/README.md index 6112d240a3577..accfae3864fdf 100644 --- a/solution/0600-0699/0645.Set Mismatch/README.md +++ b/solution/0600-0699/0645.Set Mismatch/README.md @@ -59,7 +59,7 @@ **方法三:位运算** -根据异或运算的性质,对于整数 $x$,有 $x \oplus x = 0$ 以及 $x \oplus 0 = x$,因此我们对数组 $nums$ 中的所有元素以及 $i \in [1, n]$ 的所有数字进行异或运算,可以消除出现两次的数字,最终只留下缺失的数字和重复的数字的异或结果,即 $eor = a \oplus b$。 +根据异或运算的性质,对于整数 $x$,有 $x \oplus x = 0$ 以及 $x \oplus 0 = x$,因此我们对数组 $nums$ 中的所有元素以及 $i \in [1, n]$ 的所有数字进行异或运算,可以消除出现两次的数字,最终只留下缺失的数字和重复的数字的异或结果,即 $xs = a \oplus b$。 由于这两个数字不相等,因此异或结果中至少存在一位为 $1$。我们通过 $lowbit$ 运算找到异或结果中最低位的 $1$,然后将数组 $nums$ 中所有数字以及 $i \in [1, n]$ 的所有数字按照该位是否为 $1$ 分为两组,这样两个数字就被分到了不同的组中。其中一组数字的异或结果为 $a$,另一组数字的异或结果为 $b$。那么这两个数字就是我们要找的答案。 @@ -100,17 +100,17 @@ class Solution: ```python class Solution: def findErrorNums(self, nums: List[int]) -> List[int]: - eor = 0 + xs = 0 for i, x in enumerate(nums, 1): - eor ^= i ^ x + xs ^= i ^ x a = 0 - lb = eor & -eor + lb = xs & -xs for i, x in enumerate(nums, 1): if i & lb: a ^= i if x & lb: a ^= x - b = eor ^ a + b = xs ^ a for x in nums: if x == a: return [a, b] @@ -166,11 +166,11 @@ class Solution { class Solution { public int[] findErrorNums(int[] nums) { int n = nums.length; - int eor = 0; + int xs = 0; for (int i = 1; i <= n; ++i) { - eor ^= i ^ nums[i - 1]; + xs ^= i ^ nums[i - 1]; } - int lb = eor & -eor; + int lb = xs & -xs; int a = 0; for (int i = 1; i <= n; ++i) { if ((i & lb) > 0) { @@ -180,7 +180,7 @@ class Solution { a ^= nums[i - 1]; } } - int b = eor ^ a; + int b = xs ^ a; for (int i = 0; i < n; ++i) { if (nums[i] == a) { return new int[] {a, b}; @@ -237,11 +237,11 @@ class Solution { public: vector findErrorNums(vector& nums) { int n = nums.size(); - int eor = 0; + int xs = 0; for (int i = 1; i <= n; ++i) { - eor ^= i ^ nums[i - 1]; + xs ^= i ^ nums[i - 1]; } - int lb = eor & -eor; + int lb = xs & -xs; int a = 0; for (int i = 1; i <= n; ++i) { if (i & lb) { @@ -251,7 +251,7 @@ public: a ^= nums[i - 1]; } } - int b = eor ^ a; + int b = xs ^ a; for (int i = 0; i < n; ++i) { if (nums[i] == a) { return {a, b}; @@ -302,11 +302,11 @@ func findErrorNums(nums []int) []int { ```go func findErrorNums(nums []int) []int { - eor := 0 + xs := 0 for i, x := range nums { - eor ^= x ^ (i + 1) + xs ^= x ^ (i + 1) } - lb := eor & -eor + lb := xs & -xs a := 0 for i, x := range nums { if (i+1)&lb != 0 { @@ -316,7 +316,7 @@ func findErrorNums(nums []int) []int { a ^= x } } - b := eor ^ a + b := xs ^ a for _, x := range nums { if x == a { return []int{a, b} @@ -326,6 +326,49 @@ func findErrorNums(nums []int) []int { } ``` +### **Rust** + +```rust +use std::collections::HashSet; +impl Solution { + pub fn find_error_nums(nums: Vec) -> Vec { + let n = nums.len() as i32; + let s1 = (1 + n) * n / 2; + let s2 = nums.iter().cloned().collect::>().iter().sum::(); + let s: i32 = nums.iter().sum(); + vec![s - s2, s1 - s2] + } +} +``` + +```rust +impl Solution { + pub fn find_error_nums(nums: Vec) -> Vec { + let mut xs = 0; + for (i, x) in nums.iter().enumerate() { + xs ^= (i + 1) as i32 ^ x; + } + let mut a = 0; + let lb = xs & -xs; + for (i, x) in nums.iter().enumerate() { + if (i + 1) as i32 & lb != 0 { + a ^= (i + 1) as i32; + } + if *x & lb != 0 { + a ^= *x; + } + } + let b = xs ^ a; + for x in nums.iter() { + if *x == a { + return vec![a, b]; + } + } + vec![b, a] + } +} +``` + ### **TypeScript** ```ts @@ -361,11 +404,11 @@ function findErrorNums(nums: number[]): number[] { ```ts function findErrorNums(nums: number[]): number[] { const n = nums.length; - let eor = 0; + let xs = 0; for (let i = 1; i <= n; ++i) { - eor ^= i ^ nums[i - 1]; + xs ^= i ^ nums[i - 1]; } - const lb = eor & -eor; + const lb = xs & -xs; let a = 0; for (let i = 1; i <= n; ++i) { if (i & lb) { @@ -375,7 +418,7 @@ function findErrorNums(nums: number[]): number[] { a ^= nums[i - 1]; } } - const b = eor ^ a; + const b = xs ^ a; return nums.includes(a) ? [a, b] : [b, a]; } ``` diff --git a/solution/0600-0699/0645.Set Mismatch/README_EN.md b/solution/0600-0699/0645.Set Mismatch/README_EN.md index 37560b58c335c..70950cf8bfc87 100644 --- a/solution/0600-0699/0645.Set Mismatch/README_EN.md +++ b/solution/0600-0699/0645.Set Mismatch/README_EN.md @@ -46,7 +46,7 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is **Solution 3: Bit Operation** -According to the properties of the XOR operation, for integer $x$, we have $x \oplus x = 0$ and $x \oplus 0 = x$. Therefore, if we perform the XOR operation on all elements in the array $nums$ and all numbers $i \in [1, n]$, we can eliminate the numbers that appear twice, leaving only the XOR result of the missing number and the duplicate number, i.e., $eor = a \oplus b$. +According to the properties of the XOR operation, for integer $x$, we have $x \oplus x = 0$ and $x \oplus 0 = x$. Therefore, if we perform the XOR operation on all elements in the array $nums$ and all numbers $i \in [1, n]$, we can eliminate the numbers that appear twice, leaving only the XOR result of the missing number and the duplicate number, i.e., $xs = a \oplus b$. Since these two numbers are not equal, there must be at least one bit in the XOR result that is $1$. We find the lowest bit of $1$ in the XOR result through the $lowbit$ operation, and then divide all numbers in the array $nums$ and all numbers $i \in [1, n]$ into two groups according to whether this bit is $1$. In this way, the two numbers are divided into different groups. The XOR result of one group of numbers is $a$, and the XOR result of the other group is $b$. These two numbers are the answers we are looking for. @@ -85,17 +85,17 @@ class Solution: ```python class Solution: def findErrorNums(self, nums: List[int]) -> List[int]: - eor = 0 + xs = 0 for i, x in enumerate(nums, 1): - eor ^= i ^ x + xs ^= i ^ x a = 0 - lb = eor & -eor + lb = xs & -xs for i, x in enumerate(nums, 1): if i & lb: a ^= i if x & lb: a ^= x - b = eor ^ a + b = xs ^ a for x in nums: if x == a: return [a, b] @@ -149,11 +149,11 @@ class Solution { class Solution { public int[] findErrorNums(int[] nums) { int n = nums.length; - int eor = 0; + int xs = 0; for (int i = 1; i <= n; ++i) { - eor ^= i ^ nums[i - 1]; + xs ^= i ^ nums[i - 1]; } - int lb = eor & -eor; + int lb = xs & -xs; int a = 0; for (int i = 1; i <= n; ++i) { if ((i & lb) > 0) { @@ -163,7 +163,7 @@ class Solution { a ^= nums[i - 1]; } } - int b = eor ^ a; + int b = xs ^ a; for (int i = 0; i < n; ++i) { if (nums[i] == a) { return new int[] {a, b}; @@ -220,11 +220,11 @@ class Solution { public: vector findErrorNums(vector& nums) { int n = nums.size(); - int eor = 0; + int xs = 0; for (int i = 1; i <= n; ++i) { - eor ^= i ^ nums[i - 1]; + xs ^= i ^ nums[i - 1]; } - int lb = eor & -eor; + int lb = xs & -xs; int a = 0; for (int i = 1; i <= n; ++i) { if (i & lb) { @@ -234,7 +234,7 @@ public: a ^= nums[i - 1]; } } - int b = eor ^ a; + int b = xs ^ a; for (int i = 0; i < n; ++i) { if (nums[i] == a) { return {a, b}; @@ -285,11 +285,11 @@ func findErrorNums(nums []int) []int { ```go func findErrorNums(nums []int) []int { - eor := 0 + xs := 0 for i, x := range nums { - eor ^= x ^ (i + 1) + xs ^= x ^ (i + 1) } - lb := eor & -eor + lb := xs & -xs a := 0 for i, x := range nums { if (i+1)&lb != 0 { @@ -299,7 +299,7 @@ func findErrorNums(nums []int) []int { a ^= x } } - b := eor ^ a + b := xs ^ a for _, x := range nums { if x == a { return []int{a, b} @@ -309,6 +309,49 @@ func findErrorNums(nums []int) []int { } ``` +### **Rust** + +```rust +use std::collections::HashSet; +impl Solution { + pub fn find_error_nums(nums: Vec) -> Vec { + let n = nums.len() as i32; + let s1 = (1 + n) * n / 2; + let s2 = nums.iter().cloned().collect::>().iter().sum::(); + let s: i32 = nums.iter().sum(); + vec![s - s2, s1 - s2] + } +} +``` + +```rust +impl Solution { + pub fn find_error_nums(nums: Vec) -> Vec { + let mut xs = 0; + for (i, x) in nums.iter().enumerate() { + xs ^= (i + 1) as i32 ^ x; + } + let mut a = 0; + let lb = xs & -xs; + for (i, x) in nums.iter().enumerate() { + if (i + 1) as i32 & lb != 0 { + a ^= (i + 1) as i32; + } + if *x & lb != 0 { + a ^= *x; + } + } + let b = xs ^ a; + for x in nums.iter() { + if *x == a { + return vec![a, b]; + } + } + vec![b, a] + } +} +``` + ### **TypeScript** ```ts @@ -344,11 +387,11 @@ function findErrorNums(nums: number[]): number[] { ```ts function findErrorNums(nums: number[]): number[] { const n = nums.length; - let eor = 0; + let xs = 0; for (let i = 1; i <= n; ++i) { - eor ^= i ^ nums[i - 1]; + xs ^= i ^ nums[i - 1]; } - const lb = eor & -eor; + const lb = xs & -xs; let a = 0; for (let i = 1; i <= n; ++i) { if (i & lb) { @@ -358,7 +401,7 @@ function findErrorNums(nums: number[]): number[] { a ^= nums[i - 1]; } } - const b = eor ^ a; + const b = xs ^ a; return nums.includes(a) ? [a, b] : [b, a]; } ``` diff --git a/solution/0600-0699/0645.Set Mismatch/Solution.cpp b/solution/0600-0699/0645.Set Mismatch/Solution.cpp index 711a9c07732e1..e08f21e60a0c1 100644 --- a/solution/0600-0699/0645.Set Mismatch/Solution.cpp +++ b/solution/0600-0699/0645.Set Mismatch/Solution.cpp @@ -2,11 +2,11 @@ class Solution { public: vector findErrorNums(vector& nums) { int n = nums.size(); - int eor = 0; + int xs = 0; for (int i = 1; i <= n; ++i) { - eor ^= i ^ nums[i - 1]; + xs ^= i ^ nums[i - 1]; } - int lb = eor & -eor; + int lb = xs & -xs; int a = 0; for (int i = 1; i <= n; ++i) { if (i & lb) { @@ -16,7 +16,7 @@ class Solution { a ^= nums[i - 1]; } } - int b = eor ^ a; + int b = xs ^ a; for (int i = 0; i < n; ++i) { if (nums[i] == a) { return {a, b}; diff --git a/solution/0600-0699/0645.Set Mismatch/Solution.go b/solution/0600-0699/0645.Set Mismatch/Solution.go index 5c0e59c32d961..ab485e1856262 100644 --- a/solution/0600-0699/0645.Set Mismatch/Solution.go +++ b/solution/0600-0699/0645.Set Mismatch/Solution.go @@ -1,9 +1,9 @@ func findErrorNums(nums []int) []int { - eor := 0 + xs := 0 for i, x := range nums { - eor ^= x ^ (i + 1) + xs ^= x ^ (i + 1) } - lb := eor & -eor + lb := xs & -xs a := 0 for i, x := range nums { if (i+1)&lb != 0 { @@ -13,7 +13,7 @@ func findErrorNums(nums []int) []int { a ^= x } } - b := eor ^ a + b := xs ^ a for _, x := range nums { if x == a { return []int{a, b} diff --git a/solution/0600-0699/0645.Set Mismatch/Solution.java b/solution/0600-0699/0645.Set Mismatch/Solution.java index e31e5d65988ed..d4da115a83be9 100644 --- a/solution/0600-0699/0645.Set Mismatch/Solution.java +++ b/solution/0600-0699/0645.Set Mismatch/Solution.java @@ -1,11 +1,11 @@ class Solution { public int[] findErrorNums(int[] nums) { int n = nums.length; - int eor = 0; + int xs = 0; for (int i = 1; i <= n; ++i) { - eor ^= i ^ nums[i - 1]; + xs ^= i ^ nums[i - 1]; } - int lb = eor & -eor; + int lb = xs & -xs; int a = 0; for (int i = 1; i <= n; ++i) { if ((i & lb) > 0) { @@ -15,7 +15,7 @@ public int[] findErrorNums(int[] nums) { a ^= nums[i - 1]; } } - int b = eor ^ a; + int b = xs ^ a; for (int i = 0; i < n; ++i) { if (nums[i] == a) { return new int[] {a, b}; diff --git a/solution/0600-0699/0645.Set Mismatch/Solution.py b/solution/0600-0699/0645.Set Mismatch/Solution.py index 72c304d3eeedf..10c999b8c5b50 100644 --- a/solution/0600-0699/0645.Set Mismatch/Solution.py +++ b/solution/0600-0699/0645.Set Mismatch/Solution.py @@ -1,16 +1,16 @@ class Solution: def findErrorNums(self, nums: List[int]) -> List[int]: - eor = 0 + xs = 0 for i, x in enumerate(nums, 1): - eor ^= i ^ x + xs ^= i ^ x a = 0 - lb = eor & -eor + lb = xs & -xs for i, x in enumerate(nums, 1): if i & lb: a ^= i if x & lb: a ^= x - b = eor ^ a + b = xs ^ a for x in nums: if x == a: return [a, b] diff --git a/solution/0600-0699/0645.Set Mismatch/Solution.rs b/solution/0600-0699/0645.Set Mismatch/Solution.rs new file mode 100644 index 0000000000000..4e8385d344274 --- /dev/null +++ b/solution/0600-0699/0645.Set Mismatch/Solution.rs @@ -0,0 +1,25 @@ +impl Solution { + pub fn find_error_nums(nums: Vec) -> Vec { + let mut xs = 0; + for (i, x) in nums.iter().enumerate() { + xs ^= (i + 1) as i32 ^ x; + } + let mut a = 0; + let lb = xs & -xs; + for (i, x) in nums.iter().enumerate() { + if (i + 1) as i32 & lb != 0 { + a ^= (i + 1) as i32; + } + if *x & lb != 0 { + a ^= *x; + } + } + let b = xs ^ a; + for x in nums.iter() { + if *x == a { + return vec![a, b]; + } + } + vec![b, a] + } +} \ No newline at end of file diff --git a/solution/0600-0699/0645.Set Mismatch/Solution.ts b/solution/0600-0699/0645.Set Mismatch/Solution.ts index 7292fa0ab6154..8a89356b633ba 100644 --- a/solution/0600-0699/0645.Set Mismatch/Solution.ts +++ b/solution/0600-0699/0645.Set Mismatch/Solution.ts @@ -1,10 +1,10 @@ function findErrorNums(nums: number[]): number[] { const n = nums.length; - let eor = 0; + let xs = 0; for (let i = 1; i <= n; ++i) { - eor ^= i ^ nums[i - 1]; + xs ^= i ^ nums[i - 1]; } - const lb = eor & -eor; + const lb = xs & -xs; let a = 0; for (let i = 1; i <= n; ++i) { if (i & lb) { @@ -14,6 +14,6 @@ function findErrorNums(nums: number[]): number[] { a ^= nums[i - 1]; } } - const b = eor ^ a; + const b = xs ^ a; return nums.includes(a) ? [a, b] : [b, a]; }