Skip to content

feat: add solutions to lc problem: No.1228 #3499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ tags:

### 方法一:等差数列求和公式

等差数列求和公式为 $\frac{n(a_1 + a_n)}{2}$,其中 $n$ 为等差数列的项数,$a_1$ 为等差数列的首项,$a_n$ 为等差数列的末项
等差数列求和公式为 $\frac{(a_1 + a_n)n}{2}$,其中 $n$ 为等差数列的项数,等差数列的首项为 $a_1$,末项为 $a_n$。

因为题目中给出的数组是一个等差数列,且缺失了一个数,所以数组的项数为 $n + 1$,首项为 $a_1$,末项为 $a_n$,则数组的和为 $\frac{n + 1}{2}(a_1 + a_n)$。
因为题目中给出的数组是一个等差数列,且缺失了一个数,所以数组的项数为 $n + 1$,首项为 $a_1$,末项为 $a_n$,则数组的和为 $\frac{(a_1 + a_n)(n + 1)}{2}$。

因此,缺失的数为 $\frac{n + 1}{2}(a_1 + a_n) - \sum_{i = 0}^n a_i$。
因此,缺失的数为 $\frac{(a_1 + a_n)(n + 1)}{2} - \sum_{i = 0}^n a_i$。

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

Expand Down Expand Up @@ -110,13 +110,22 @@ public:
```go
func missingNumber(arr []int) int {
n := len(arr)
d := (arr[n-1] - arr[0]) / n
for i := 1; i < n; i++ {
if arr[i] != arr[i-1]+d {
return arr[i-1] + d
}
x := (arr[0] + arr[n-1]) * (n + 1) / 2
y := 0
for _, v := range arr {
y += v
}
return arr[0]
return x - y
}
```

#### TypeScript

```ts
function missingNumber(arr: number[]): number {
const x = ((arr[0] + arr.at(-1)!) * (arr.length + 1)) >> 1;
const y = arr.reduce((acc, cur) => acc + cur, 0);
return x - y;
}
```

Expand All @@ -126,7 +135,15 @@ func missingNumber(arr []int) int {

<!-- solution:start -->

### 方法二
### 方法二:求公差 + 遍历

因为题目中给出的数组是一个等差数列,且缺失了一个数,首项为 $a_1$,末项为 $a_n$,那么公差 $d = \frac{a_n - a_1}{n}$。

遍历数组,如果 $a_i \neq a_{i - 1} + d$,则返回 $a_{i - 1} + d$。

如果遍历完数组都没有找到缺失的数,说明数组的所有数都相等,直接返回数组的第一个数即可。

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

<!-- tabs:start -->

Expand Down Expand Up @@ -168,13 +185,45 @@ public:
int missingNumber(vector<int>& arr) {
int n = arr.size();
int d = (arr[n - 1] - arr[0]) / n;
for (int i = 1; i < n; ++i)
if (arr[i] != arr[i - 1] + d) return arr[i - 1] + d;
for (int i = 1; i < n; ++i) {
if (arr[i] != arr[i - 1] + d) {
return arr[i - 1] + d;
}
}
return arr[0];
}
};
```

#### Go

```go
func missingNumber(arr []int) int {
n := len(arr)
d := (arr[n-1] - arr[0]) / n
for i := 1; i < n; i++ {
if arr[i] != arr[i-1]+d {
return arr[i-1] + d
}
}
return arr[0]
}
```

#### TypeScript

```ts
function missingNumber(arr: number[]): number {
const d = ((arr.at(-1)! - arr[0]) / arr.length) | 0;
for (let i = 1; i < arr.length; ++i) {
if (arr[i] - arr[i - 1] !== d) {
return arr[i - 1] + d;
}
}
return arr[0];
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ tags:

### Solution 1: Arithmetic Series Sum Formula

The sum formula for an arithmetic series is $\frac{n(a_1 + a_n)}{2}$, where $n$ is the number of terms in the arithmetic series, $a_1$ is the first term of the arithmetic series, and $a_n$ is the last term of the arithmetic series.
The sum formula for an arithmetic series is $\frac{(a_1 + a_n)n}{2}$, where $n$ is the number of terms in the arithmetic series, the first term is $a_1$, and the last term is $a_n$.

Since the array given in the problem is an arithmetic series and is missing a number, the number of terms in the array is $n + 1$, the first term is $a_1$, and the last term is $a_n$, so the sum of the array is $\frac{n + 1}{2}(a_1 + a_n)$.
Since the array given in the problem is an arithmetic series with one missing number, the number of terms in the array is $n + 1$, the first term is $a_1$, and the last term is $a_n$. Therefore, the sum of the array is $\frac{(a_1 + a_n)(n + 1)}{2}$.

Therefore, the missing number is $\frac{n + 1}{2}(a_1 + a_n) - \sum_{i = 0}^n a_i$.
Thus, the missing number is $\frac{(a_1 + a_n)(n + 1)}{2} - \sum_{i = 0}^n a_i$.

The time complexity is $O(n)$, and the space complexity is $O(1)$. Where $n$ is the length of the array.
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -108,13 +108,22 @@ public:
```go
func missingNumber(arr []int) int {
n := len(arr)
d := (arr[n-1] - arr[0]) / n
for i := 1; i < n; i++ {
if arr[i] != arr[i-1]+d {
return arr[i-1] + d
}
x := (arr[0] + arr[n-1]) * (n + 1) / 2
y := 0
for _, v := range arr {
y += v
}
return arr[0]
return x - y
}
```

#### TypeScript

```ts
function missingNumber(arr: number[]): number {
const x = ((arr[0] + arr.at(-1)!) * (arr.length + 1)) >> 1;
const y = arr.reduce((acc, cur) => acc + cur, 0);
return x - y;
}
```

Expand All @@ -124,7 +133,15 @@ func missingNumber(arr []int) int {

<!-- solution:start -->

### Solution 2
### Solution 2: Find Common Difference + Traverse

Since the array given in the problem is an arithmetic series with one missing number, the first term is $a_1$, and the last term is $a_n$. The common difference $d$ is $\frac{a_n - a_1}{n}$.

Traverse the array, and if $a_i \neq a_{i - 1} + d$, then return $a_{i - 1} + d$.

If the traversal completes without finding the missing number, it means all numbers in the array are equal. In this case, directly return the first number of the array.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -166,13 +183,45 @@ public:
int missingNumber(vector<int>& arr) {
int n = arr.size();
int d = (arr[n - 1] - arr[0]) / n;
for (int i = 1; i < n; ++i)
if (arr[i] != arr[i - 1] + d) return arr[i - 1] + d;
for (int i = 1; i < n; ++i) {
if (arr[i] != arr[i - 1] + d) {
return arr[i - 1] + d;
}
}
return arr[0];
}
};
```

#### Go

```go
func missingNumber(arr []int) int {
n := len(arr)
d := (arr[n-1] - arr[0]) / n
for i := 1; i < n; i++ {
if arr[i] != arr[i-1]+d {
return arr[i-1] + d
}
}
return arr[0]
}
```

#### TypeScript

```ts
function missingNumber(arr: number[]): number {
const d = ((arr.at(-1)! - arr[0]) / arr.length) | 0;
for (let i = 1; i < arr.length; ++i) {
if (arr[i] - arr[i - 1] !== d) {
return arr[i - 1] + d;
}
}
return arr[0];
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
func missingNumber(arr []int) int {
n := len(arr)
d := (arr[n-1] - arr[0]) / n
for i := 1; i < n; i++ {
if arr[i] != arr[i-1]+d {
return arr[i-1] + d
}
}
return arr[0]
}
func missingNumber(arr []int) int {
n := len(arr)
x := (arr[0] + arr[n-1]) * (n + 1) / 2
y := 0
for _, v := range arr {
y += v
}
return x - y
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function missingNumber(arr: number[]): number {
const x = ((arr[0] + arr.at(-1)!) * (arr.length + 1)) >> 1;
const y = arr.reduce((acc, cur) => acc + cur, 0);
return x - y;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ class Solution {
int missingNumber(vector<int>& arr) {
int n = arr.size();
int d = (arr[n - 1] - arr[0]) / n;
for (int i = 1; i < n; ++i)
if (arr[i] != arr[i - 1] + d) return arr[i - 1] + d;
for (int i = 1; i < n; ++i) {
if (arr[i] != arr[i - 1] + d) {
return arr[i - 1] + d;
}
}
return arr[0];
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
func missingNumber(arr []int) int {
n := len(arr)
d := (arr[n-1] - arr[0]) / n
for i := 1; i < n; i++ {
if arr[i] != arr[i-1]+d {
return arr[i-1] + d
}
}
return arr[0]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function missingNumber(arr: number[]): number {
const d = ((arr.at(-1)! - arr[0]) / arr.length) | 0;
for (let i = 1; i < arr.length; ++i) {
if (arr[i] - arr[i - 1] !== d) {
return arr[i - 1] + d;
}
}
return arr[0];
}
Loading