Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0268
Browse files Browse the repository at this point in the history
No.0268.Missing Number
  • Loading branch information
yanglbme committed Nov 20, 2022
1 parent bbcbc50 commit 22d8974
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 73 deletions.
115 changes: 89 additions & 26 deletions solution/0200-0299/0268.Missing Number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,17 @@

<!-- 这里可写通用的实现逻辑 -->

异或求解。两个相同的数异或的结果为 0。
**方法一:位运算**

也可以用数学求解。求出 `[0..n]` 的和,减去数组中所有数的和,就得到了缺失的数字。
对于数组中的每个元素,都可以与下标进行异或运算,最终的结果就是缺失的数字。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度。

**方法二:数学**

我们也可以用数学求解。求出 $[0,..n]$ 的和,减去数组中所有数的和,就得到了缺失的数字。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度。

<!-- tabs:start -->

Expand All @@ -73,40 +81,42 @@
```python
class Solution:
def missingNumber(self, nums: List[int]) -> int:
res = len(nums)
for i, v in enumerate(nums):
res ^= i ^ v
return res
return reduce(xor, (i ^ v for i, v in enumerate(nums, 1)))
```

```python
class Solution:
def missingNumber(self, nums: List[int]) -> int:
n = len(nums)
return (1 + n) * n // 2 - sum(nums)
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

- 异或

```java
class Solution {
public int missingNumber(int[] nums) {
int res = nums.length;
for (int i = 0, n = res; i < n; ++i) {
res ^= (i ^ nums[i]);
int n = nums.length;
int ans = n;
for (int i = 0; i < n; ++i) {
ans ^= (i ^ nums[i]);
}
return res;
return ans;
}
}
```

- 数学

```java
class Solution {
public int missingNumber(int[] nums) {
int res = nums.length;
for (int i = 0, n = res; i < n; ++i) {
res += (i - nums[i]);
int n = nums.length;
int ans = n;
for (int i = 0; i < n; ++i) {
ans += i - nums[i];
}
return res;
return ans;
}
}
```
Expand All @@ -118,28 +128,81 @@ class Solution {
public:
int missingNumber(vector<int>& nums) {
int n = nums.size();
int res = n;
int ans = n;
for (int i = 0; i < n; ++i) {
res ^= (i ^ nums[i]);
ans ^= (i ^ nums[i]);
}
return res;
return ans;
}
};
```
```cpp
class Solution {
public:
int missingNumber(vector<int>& nums) {
int n = nums.size();
return (1 + n) * n / 2 - accumulate(nums.begin(), nums.end(), 0);
}
};
```

### **Go**

```go
func missingNumber(nums []int) int {
func missingNumber(nums []int) (ans int) {
n := len(nums)
ans = n
for i, v := range nums {
ans ^= (i ^ v)
}
return
}
```

```go
func missingNumber(nums []int) (ans int) {
n := len(nums)
res := n
for i := 0; i < n; i++ {
res ^= (i ^ nums[i])
ans = n
for i, v := range nums {
ans += i - v
}
return res
return
}
```

### **JavaScript**

```js
/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function (nums) {
const n = nums.length;
let ans = n;
for (let i = 0; i < n; ++i) {
ans ^= i ^ nums[i];
}
return ans;
};
```

```js
/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function (nums) {
const n = nums.length;
let ans = n;
for (let i = 0; i < n; ++i) {
ans += i - nums[i];
}
return ans;
};
```

### **...**

```
Expand Down
103 changes: 79 additions & 24 deletions solution/0200-0299/0268.Missing Number/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,38 +53,40 @@
```python
class Solution:
def missingNumber(self, nums: List[int]) -> int:
res = len(nums)
for i, v in enumerate(nums):
res ^= i ^ v
return res
return reduce(xor, (i ^ v for i, v in enumerate(nums, 1)))
```

### **Java**
```python
class Solution:
def missingNumber(self, nums: List[int]) -> int:
n = len(nums)
return (1 + n) * n // 2 - sum(nums)
```

- XOR
### **Java**

```java
class Solution {
public int missingNumber(int[] nums) {
int res = nums.length;
for (int i = 0, n = res; i < n; ++i) {
res ^= (i ^ nums[i]);
int n = nums.length;
int ans = n;
for (int i = 0; i < n; ++i) {
ans ^= (i ^ nums[i]);
}
return res;
return ans;
}
}
```

- Math

```java
class Solution {
public int missingNumber(int[] nums) {
int res = nums.length;
for (int i = 0, n = res; i < n; ++i) {
res += (i - nums[i]);
int n = nums.length;
int ans = n;
for (int i = 0; i < n; ++i) {
ans += i - nums[i];
}
return res;
return ans;
}
}
```
Expand All @@ -96,28 +98,81 @@ class Solution {
public:
int missingNumber(vector<int>& nums) {
int n = nums.size();
int res = n;
int ans = n;
for (int i = 0; i < n; ++i) {
res ^= (i ^ nums[i]);
ans ^= (i ^ nums[i]);
}
return res;
return ans;
}
};
```
```cpp
class Solution {
public:
int missingNumber(vector<int>& nums) {
int n = nums.size();
return (1 + n) * n / 2 - accumulate(nums.begin(), nums.end(), 0);
}
};
```

### **Go**

```go
func missingNumber(nums []int) int {
func missingNumber(nums []int) (ans int) {
n := len(nums)
ans = n
for i, v := range nums {
ans ^= (i ^ v)
}
return
}
```

```go
func missingNumber(nums []int) (ans int) {
n := len(nums)
res := n
for i := 0; i < n; i++ {
res ^= (i ^ nums[i])
ans = n
for i, v := range nums {
ans += i - v
}
return res
return
}
```

### **JavaScript**

```js
/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function (nums) {
const n = nums.length;
let ans = n;
for (let i = 0; i < n; ++i) {
ans ^= i ^ nums[i];
}
return ans;
};
```

```js
/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function (nums) {
const n = nums.length;
let ans = n;
for (let i = 0; i < n; ++i) {
ans += i - nums[i];
}
return ans;
};
```

### **...**

```
Expand Down
8 changes: 4 additions & 4 deletions solution/0200-0299/0268.Missing Number/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ class Solution {
public:
int missingNumber(vector<int>& nums) {
int n = nums.size();
int res = n;
int ans = n;
for (int i = 0; i < n; ++i) {
res ^= (i ^ nums[i]);
ans ^= (i ^ nums[i]);
}
return res;
return ans;
}
};
};
12 changes: 6 additions & 6 deletions solution/0200-0299/0268.Missing Number/Solution.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
func missingNumber(nums []int) int {
func missingNumber(nums []int) (ans int) {
n := len(nums)
res := n
for i := 0; i < n; i++ {
res ^= (i ^ nums[i])
ans = n
for i, v := range nums {
ans ^= (i ^ v)
}
return res
}
return
}
9 changes: 5 additions & 4 deletions solution/0200-0299/0268.Missing Number/Solution.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class Solution {
public int missingNumber(int[] nums) {
int res = nums.length;
for (int i = 0, n = res; i < n; ++i) {
res ^= (i ^ nums[i]);
int n = nums.length;
int ans = n;
for (int i = 0; i < n; ++i) {
ans ^= (i ^ nums[i]);
}
return res;
return ans;
}
}
16 changes: 11 additions & 5 deletions solution/0200-0299/0268.Missing Number/Solution.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
const missingNumber = function (nums) {
return (
((1 + nums.length) * nums.length) / 2 -
nums.reduce((prev, cur) => (prev += cur), 0)
);
/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function (nums) {
const n = nums.length;
let ans = n;
for (let i = 0; i < n; ++i) {
ans ^= i ^ nums[i];
}
return ans;
};
5 changes: 1 addition & 4 deletions solution/0200-0299/0268.Missing Number/Solution.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
class Solution:
def missingNumber(self, nums: List[int]) -> int:
res = len(nums)
for i, v in enumerate(nums):
res ^= i ^ v
return res
return reduce(xor, (i ^ v for i, v in enumerate(nums, 1)))

0 comments on commit 22d8974

Please sign in to comment.