diff --git a/solution/1200-1299/1228.Missing Number In Arithmetic Progression/README.md b/solution/1200-1299/1228.Missing Number In Arithmetic Progression/README.md index bb67502177c5b..7e371168c78f2 100644 --- a/solution/1200-1299/1228.Missing Number In Arithmetic Progression/README.md +++ b/solution/1200-1299/1228.Missing Number In Arithmetic Progression/README.md @@ -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)$。 @@ -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; } ``` @@ -126,7 +135,15 @@ func missingNumber(arr []int) int { -### 方法二 +### 方法二:求公差 + 遍历 + +因为题目中给出的数组是一个等差数列,且缺失了一个数,首项为 $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)$。 @@ -168,13 +185,45 @@ public: int missingNumber(vector& 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]; +} +``` + diff --git a/solution/1200-1299/1228.Missing Number In Arithmetic Progression/README_EN.md b/solution/1200-1299/1228.Missing Number In Arithmetic Progression/README_EN.md index 024ad2cdec560..d230e9e230ab0 100644 --- a/solution/1200-1299/1228.Missing Number In Arithmetic Progression/README_EN.md +++ b/solution/1200-1299/1228.Missing Number In Arithmetic Progression/README_EN.md @@ -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)$. @@ -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; } ``` @@ -124,7 +133,15 @@ func missingNumber(arr []int) int { -### 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)$. @@ -166,13 +183,45 @@ public: int missingNumber(vector& 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]; +} +``` + diff --git a/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution.go b/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution.go index db6075cfc2e2d..ec8560ff1f1eb 100644 --- a/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution.go +++ b/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution.go @@ -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] -} \ No newline at end of file +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 +} diff --git a/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution.ts b/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution.ts new file mode 100644 index 0000000000000..f1999e3d14178 --- /dev/null +++ b/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution.ts @@ -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; +} diff --git a/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution2.cpp b/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution2.cpp index e6e938d6ab51b..f4bd34ae714b3 100644 --- a/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution2.cpp +++ b/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution2.cpp @@ -3,8 +3,11 @@ class Solution { int missingNumber(vector& 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]; } -}; \ No newline at end of file +}; diff --git a/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution2.go b/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution2.go new file mode 100644 index 0000000000000..db6075cfc2e2d --- /dev/null +++ b/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution2.go @@ -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] +} \ No newline at end of file diff --git a/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution2.ts b/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution2.ts new file mode 100644 index 0000000000000..1cc82f957c495 --- /dev/null +++ b/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution2.ts @@ -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]; +}