diff --git a/solution/0100-0199/0162.Find Peak Element/README.md b/solution/0100-0199/0162.Find Peak Element/README.md index 216d8ec7bd478..5fecca8cf6bbc 100644 --- a/solution/0100-0199/0162.Find Peak Element/README.md +++ b/solution/0100-0199/0162.Find Peak Element/README.md @@ -46,7 +46,15 @@ <!-- 这里可写通用的实现逻辑 --> -二分查找。 +**方法一:二分查找** + +我们定义二分查找的左边界 $left=0$,右边界 $right=n-1$,其中 $n$ 是数组的长度。在每一步二分查找中,我们找到当前区间的中间元素 $mid$,然后比较 $mid$ 与其右边元素 $mid+1$ 的值: + +- 如果 $mid$ 的值大于 $mid+1$ 的值,则左侧存在峰值元素,我们将右边界 $right$ 更新为 $mid$; +- 否则,右侧存在峰值元素,我们将左边界 $left$ 更新为 $mid+1$。 +- 最后,当左边界 $left$ 与右边界 $right$ 相等时,我们就找到了数组的峰值元素。 + +时间复杂度 $O(\log n)$,其中 $n$ 是数组 $nums$ 的长度。每一步二分查找可以将搜索区间减少一半,因此时间复杂度为 $O(\log n)$。空间复杂度 $O(1)$。 <!-- tabs:start --> @@ -88,22 +96,24 @@ class Solution { } ``` -### **TypeScript** +### **C++** -```ts -function findPeakElement(nums: number[]): number { - let left = 0, - right = nums.length - 1; - while (left < right) { - let mid: number = (left + right) >> 1; - if (nums[mid] <= nums[mid + 1]) { - left = mid + 1; - } else { - right = mid; +```cpp +class Solution { +public: + int findPeakElement(vector<int>& nums) { + int left = 0, right = nums.size() - 1; + while (left < right) { + int mid = left + right >> 1; + if (nums[mid] > nums[mid + 1]) { + right = mid; + } else { + left = mid + 1; + } } + return left; } - return left; -} +}; ``` ### **Go** @@ -123,24 +133,21 @@ func findPeakElement(nums []int) int { } ``` -### **C++** +### **TypeScript** -```cpp -class Solution { -public: - int findPeakElement(vector<int>& nums) { - int left = 0, right = nums.size() - 1; - while (left < right) { - int mid = left + right >> 1; - if (nums[mid] > nums[mid + 1]) { - right = mid; - } else { - left = mid + 1; - } +```ts +function findPeakElement(nums: number[]): number { + let [left, right] = [0, nums.length - 1]; + while (left < right) { + const mid = (left + right) >> 1; + if (nums[mid] > nums[mid + 1]) { + right = mid; + } else { + left = mid + 1; } - return left; } -}; + return left; +} ``` ### **...** diff --git a/solution/0100-0199/0162.Find Peak Element/README_EN.md b/solution/0100-0199/0162.Find Peak Element/README_EN.md index d36e0247db225..fec9e4a7c01e7 100644 --- a/solution/0100-0199/0162.Find Peak Element/README_EN.md +++ b/solution/0100-0199/0162.Find Peak Element/README_EN.md @@ -38,7 +38,15 @@ ## Solutions -Binary search. +**Solution 1: Binary Search** + +We define the left boundary of binary search as $left=0$ and the right boundary as $right=n-1$, where $n$ is the length of the array. In each step of binary search, we find the middle element $mid$ of the current interval, and compare the values of $mid$ and its right neighbor $mid+1$: + +- If the value of $mid$ is greater than the value of $mid+1$, there exists a peak element on the left side, and we update the right boundary $right$ to $mid$. +- Otherwise, there exists a peak element on the right side, and we update the left boundary $left$ to $mid+1$. +- Finally, when the left boundary $left$ is equal to the right boundary $right$, we have found the peak element of the array. + +The time complexity is $O(\log n)$, where $n$ is the length of the array $nums$. Each step of binary search can reduce the search interval by half, so the time complexity is $O(\log n)$. The space complexity is $O(1)$. <!-- tabs:start --> @@ -76,22 +84,24 @@ class Solution { } ``` -### **TypeScript** +### **C++** -```ts -function findPeakElement(nums: number[]): number { - let left = 0, - right = nums.length - 1; - while (left < right) { - let mid: number = (left + right) >> 1; - if (nums[mid] <= nums[mid + 1]) { - left = mid + 1; - } else { - right = mid; +```cpp +class Solution { +public: + int findPeakElement(vector<int>& nums) { + int left = 0, right = nums.size() - 1; + while (left < right) { + int mid = left + right >> 1; + if (nums[mid] > nums[mid + 1]) { + right = mid; + } else { + left = mid + 1; + } } + return left; } - return left; -} +}; ``` ### **Go** @@ -111,24 +121,21 @@ func findPeakElement(nums []int) int { } ``` -### **C++** +### **TypeScript** -```cpp -class Solution { -public: - int findPeakElement(vector<int>& nums) { - int left = 0, right = nums.size() - 1; - while (left < right) { - int mid = left + right >> 1; - if (nums[mid] > nums[mid + 1]) { - right = mid; - } else { - left = mid + 1; - } +```ts +function findPeakElement(nums: number[]): number { + let [left, right] = [0, nums.length - 1]; + while (left < right) { + const mid = (left + right) >> 1; + if (nums[mid] > nums[mid + 1]) { + right = mid; + } else { + left = mid + 1; } - return left; } -}; + return left; +} ``` ### **...** diff --git a/solution/0100-0199/0162.Find Peak Element/Solution.ts b/solution/0100-0199/0162.Find Peak Element/Solution.ts index a083c465b27ee..6a0f0868fe5f6 100644 --- a/solution/0100-0199/0162.Find Peak Element/Solution.ts +++ b/solution/0100-0199/0162.Find Peak Element/Solution.ts @@ -1,12 +1,11 @@ function findPeakElement(nums: number[]): number { - let left = 0, - right = nums.length - 1; + let [left, right] = [0, nums.length - 1]; while (left < right) { - let mid: number = (left + right) >> 1; - if (nums[mid] <= nums[mid + 1]) { - left = mid + 1; - } else { + const mid = (left + right) >> 1; + if (nums[mid] > nums[mid + 1]) { right = mid; + } else { + left = mid + 1; } } return left; diff --git a/solution/0100-0199/0163.Missing Ranges/README.md b/solution/0100-0199/0163.Missing Ranges/README.md index cfb52bdef9ed9..5c60511443e12 100644 --- a/solution/0100-0199/0163.Missing Ranges/README.md +++ b/solution/0100-0199/0163.Missing Ranges/README.md @@ -48,9 +48,9 @@ **方法一:模拟** -按照题意模拟即可。 +我们直接按照题意模拟即可。 -时间复杂度 $O(n)$,忽略答案的空间消耗,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。 +时间复杂度 $O(n)$,其中 $n$ 为数组 $nums$ 的长度。忽略答案的空间消耗,空间复杂度 $O(1)$。 <!-- tabs:start --> @@ -60,21 +60,20 @@ ```python class Solution: - def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[str]: - def f(a, b): - return str(a) if a == b else f'{a}->{b}' - + def findMissingRanges( + self, nums: List[int], lower: int, upper: int + ) -> List[List[int]]: n = len(nums) if n == 0: - return [f(lower, upper)] + return [[lower, upper]] ans = [] if nums[0] > lower: - ans.append(f(lower, nums[0] - 1)) + ans.append([lower, nums[0] - 1]) for a, b in pairwise(nums): if b - a > 1: - ans.append(f(a + 1, b - 1)) + ans.append([a + 1, b - 1]) if nums[-1] < upper: - ans.append(f(nums[-1] + 1, upper)) + ans.append([nums[-1] + 1, upper]) return ans ``` @@ -84,31 +83,25 @@ class Solution: ```java class Solution { - public List<String> findMissingRanges(int[] nums, int lower, int upper) { + public List<List<Integer>> findMissingRanges(int[] nums, int lower, int upper) { int n = nums.length; - List<String> ans = new ArrayList<>(); if (n == 0) { - ans.add(f(lower, upper)); - return ans; + return List.of(List.of(lower, upper)); } + List<List<Integer>> ans = new ArrayList<>(); if (nums[0] > lower) { - ans.add(f(lower, nums[0] - 1)); + ans.add(List.of(lower, nums[0] - 1)); } for (int i = 1; i < n; ++i) { - int a = nums[i - 1], b = nums[i]; - if (b - a > 1) { - ans.add(f(a + 1, b - 1)); + if (nums[i] - nums[i - 1] > 1) { + ans.add(List.of(nums[i - 1] + 1, nums[i] - 1)); } } if (nums[n - 1] < upper) { - ans.add(f(nums[n - 1] + 1, upper)); + ans.add(List.of(nums[n - 1] + 1, upper)); } return ans; } - - private String f(int a, int b) { - return a == b ? a + "" : a + "->" + b; - } } ``` @@ -117,27 +110,22 @@ class Solution { ```cpp class Solution { public: - vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) { - auto f = [](int a, int b) { - return a == b ? to_string(a) : to_string(a) + "->" + to_string(b); - }; + vector<vector<int>> findMissingRanges(vector<int>& nums, int lower, int upper) { int n = nums.size(); - vector<string> ans; if (n == 0) { - ans.emplace_back(f(lower, upper)); - return ans; + return {{lower, upper}}; } + vector<vector<int>> ans; if (nums[0] > lower) { - ans.emplace_back(f(lower, nums[0] - 1)); + ans.push_back({lower, nums[0] - 1}); } - for (int i = 1; i < n; ++i) { - int a = nums[i - 1], b = nums[i]; - if (b - a > 1) { - ans.emplace_back(f(a + 1, b - 1)); + for (int i = 1; i < nums.size(); ++i) { + if (nums[i] - nums[i - 1] > 1) { + ans.push_back({nums[i - 1] + 1, nums[i] - 1}); } } if (nums[n - 1] < upper) { - ans.emplace_back(f(nums[n - 1] + 1, upper)); + ans.push_back({nums[n - 1] + 1, upper}); } return ans; } @@ -147,34 +135,50 @@ public: ### **Go** ```go -func findMissingRanges(nums []int, lower int, upper int) (ans []string) { - f := func(a, b int) string { - if a == b { - return strconv.Itoa(a) - } - return strconv.Itoa(a) + "->" + strconv.Itoa(b) - } +func findMissingRanges(nums []int, lower int, upper int) (ans [][]int) { n := len(nums) if n == 0 { - ans = append(ans, f(lower, upper)) - return + return [][]int{{lower, upper}} } if nums[0] > lower { - ans = append(ans, f(lower, nums[0]-1)) + ans = append(ans, []int{lower, nums[0] - 1}) } - for i := 1; i < n; i++ { - a, b := nums[i-1], nums[i] - if b-a > 1 { - ans = append(ans, f(a+1, b-1)) + for i, b := range nums[1:] { + if a := nums[i]; b-a > 1 { + ans = append(ans, []int{a + 1, b - 1}) } } if nums[n-1] < upper { - ans = append(ans, f(nums[n-1]+1, upper)) + ans = append(ans, []int{nums[n-1] + 1, upper}) } return } ``` +### **TypeScript** + +```ts +function findMissingRanges(nums: number[], lower: number, upper: number): number[][] { + const n = nums.length; + if (n === 0) { + return [[lower, upper]]; + } + const ans: number[][] = []; + if (nums[0] > lower) { + ans.push([lower, nums[0] - 1]); + } + for (let i = 1; i < n; ++i) { + if (nums[i] - nums[i - 1] > 1) { + ans.push([nums[i - 1] + 1, nums[i] - 1]); + } + } + if (nums[n - 1] < upper) { + ans.push([nums[n - 1] + 1, upper]); + } + return ans; +} +``` + ### **...** ``` diff --git a/solution/0100-0199/0163.Missing Ranges/README_EN.md b/solution/0100-0199/0163.Missing Ranges/README_EN.md index d027f18d36bb1..aab082c6f4cba 100644 --- a/solution/0100-0199/0163.Missing Ranges/README_EN.md +++ b/solution/0100-0199/0163.Missing Ranges/README_EN.md @@ -45,27 +45,32 @@ ## Solutions +**Solution 1: Simulation** + +We can simulate the problem directly according to the requirements. + +The time complexity is $O(n)$, where $n$ is the length of the array $nums$. Ignoring the space consumption of the answer, the space complexity is $O(1)$. + <!-- tabs:start --> ### **Python3** ```python class Solution: - def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[str]: - def f(a, b): - return str(a) if a == b else f'{a}->{b}' - + def findMissingRanges( + self, nums: List[int], lower: int, upper: int + ) -> List[List[int]]: n = len(nums) if n == 0: - return [f(lower, upper)] + return [[lower, upper]] ans = [] if nums[0] > lower: - ans.append(f(lower, nums[0] - 1)) + ans.append([lower, nums[0] - 1]) for a, b in pairwise(nums): if b - a > 1: - ans.append(f(a + 1, b - 1)) + ans.append([a + 1, b - 1]) if nums[-1] < upper: - ans.append(f(nums[-1] + 1, upper)) + ans.append([nums[-1] + 1, upper]) return ans ``` @@ -73,31 +78,25 @@ class Solution: ```java class Solution { - public List<String> findMissingRanges(int[] nums, int lower, int upper) { + public List<List<Integer>> findMissingRanges(int[] nums, int lower, int upper) { int n = nums.length; - List<String> ans = new ArrayList<>(); if (n == 0) { - ans.add(f(lower, upper)); - return ans; + return List.of(List.of(lower, upper)); } + List<List<Integer>> ans = new ArrayList<>(); if (nums[0] > lower) { - ans.add(f(lower, nums[0] - 1)); + ans.add(List.of(lower, nums[0] - 1)); } for (int i = 1; i < n; ++i) { - int a = nums[i - 1], b = nums[i]; - if (b - a > 1) { - ans.add(f(a + 1, b - 1)); + if (nums[i] - nums[i - 1] > 1) { + ans.add(List.of(nums[i - 1] + 1, nums[i] - 1)); } } if (nums[n - 1] < upper) { - ans.add(f(nums[n - 1] + 1, upper)); + ans.add(List.of(nums[n - 1] + 1, upper)); } return ans; } - - private String f(int a, int b) { - return a == b ? a + "" : a + "->" + b; - } } ``` @@ -106,27 +105,22 @@ class Solution { ```cpp class Solution { public: - vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) { - auto f = [](int a, int b) { - return a == b ? to_string(a) : to_string(a) + "->" + to_string(b); - }; + vector<vector<int>> findMissingRanges(vector<int>& nums, int lower, int upper) { int n = nums.size(); - vector<string> ans; if (n == 0) { - ans.emplace_back(f(lower, upper)); - return ans; + return {{lower, upper}}; } + vector<vector<int>> ans; if (nums[0] > lower) { - ans.emplace_back(f(lower, nums[0] - 1)); + ans.push_back({lower, nums[0] - 1}); } - for (int i = 1; i < n; ++i) { - int a = nums[i - 1], b = nums[i]; - if (b - a > 1) { - ans.emplace_back(f(a + 1, b - 1)); + for (int i = 1; i < nums.size(); ++i) { + if (nums[i] - nums[i - 1] > 1) { + ans.push_back({nums[i - 1] + 1, nums[i] - 1}); } } if (nums[n - 1] < upper) { - ans.emplace_back(f(nums[n - 1] + 1, upper)); + ans.push_back({nums[n - 1] + 1, upper}); } return ans; } @@ -136,34 +130,50 @@ public: ### **Go** ```go -func findMissingRanges(nums []int, lower int, upper int) (ans []string) { - f := func(a, b int) string { - if a == b { - return strconv.Itoa(a) - } - return strconv.Itoa(a) + "->" + strconv.Itoa(b) - } +func findMissingRanges(nums []int, lower int, upper int) (ans [][]int) { n := len(nums) if n == 0 { - ans = append(ans, f(lower, upper)) - return + return [][]int{{lower, upper}} } if nums[0] > lower { - ans = append(ans, f(lower, nums[0]-1)) + ans = append(ans, []int{lower, nums[0] - 1}) } - for i := 1; i < n; i++ { - a, b := nums[i-1], nums[i] - if b-a > 1 { - ans = append(ans, f(a+1, b-1)) + for i, b := range nums[1:] { + if a := nums[i]; b-a > 1 { + ans = append(ans, []int{a + 1, b - 1}) } } if nums[n-1] < upper { - ans = append(ans, f(nums[n-1]+1, upper)) + ans = append(ans, []int{nums[n-1] + 1, upper}) } return } ``` +### **TypeScript** + +```ts +function findMissingRanges(nums: number[], lower: number, upper: number): number[][] { + const n = nums.length; + if (n === 0) { + return [[lower, upper]]; + } + const ans: number[][] = []; + if (nums[0] > lower) { + ans.push([lower, nums[0] - 1]); + } + for (let i = 1; i < n; ++i) { + if (nums[i] - nums[i - 1] > 1) { + ans.push([nums[i - 1] + 1, nums[i] - 1]); + } + } + if (nums[n - 1] < upper) { + ans.push([nums[n - 1] + 1, upper]); + } + return ans; +} +``` + ### **...** ``` diff --git a/solution/0100-0199/0163.Missing Ranges/Solution.cpp b/solution/0100-0199/0163.Missing Ranges/Solution.cpp index 61187d44b4b8f..47454ae71cdf7 100644 --- a/solution/0100-0199/0163.Missing Ranges/Solution.cpp +++ b/solution/0100-0199/0163.Missing Ranges/Solution.cpp @@ -1,27 +1,22 @@ -class Solution { -public: - vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) { - auto f = [](int a, int b) { - return a == b ? to_string(a) : to_string(a) + "->" + to_string(b); - }; - int n = nums.size(); - vector<string> ans; - if (n == 0) { - ans.emplace_back(f(lower, upper)); - return ans; - } - if (nums[0] > lower) { - ans.emplace_back(f(lower, nums[0] - 1)); - } - for (int i = 1; i < n; ++i) { - int a = nums[i - 1], b = nums[i]; - if (b - a > 1) { - ans.emplace_back(f(a + 1, b - 1)); - } - } - if (nums[n - 1] < upper) { - ans.emplace_back(f(nums[n - 1] + 1, upper)); - } - return ans; - } +class Solution { +public: + vector<vector<int>> findMissingRanges(vector<int>& nums, int lower, int upper) { + int n = nums.size(); + if (n == 0) { + return {{lower, upper}}; + } + vector<vector<int>> ans; + if (nums[0] > lower) { + ans.push_back({lower, nums[0] - 1}); + } + for (int i = 1; i < nums.size(); ++i) { + if (nums[i] - nums[i - 1] > 1) { + ans.push_back({nums[i - 1] + 1, nums[i] - 1}); + } + } + if (nums[n - 1] < upper) { + ans.push_back({nums[n - 1] + 1, upper}); + } + return ans; + } }; \ No newline at end of file diff --git a/solution/0100-0199/0163.Missing Ranges/Solution.go b/solution/0100-0199/0163.Missing Ranges/Solution.go index e17ea2234a378..543a51f691b6e 100644 --- a/solution/0100-0199/0163.Missing Ranges/Solution.go +++ b/solution/0100-0199/0163.Missing Ranges/Solution.go @@ -1,26 +1,18 @@ -func findMissingRanges(nums []int, lower int, upper int) (ans []string) { - f := func(a, b int) string { - if a == b { - return strconv.Itoa(a) - } - return strconv.Itoa(a) + "->" + strconv.Itoa(b) - } +func findMissingRanges(nums []int, lower int, upper int) (ans [][]int) { n := len(nums) if n == 0 { - ans = append(ans, f(lower, upper)) - return + return [][]int{{lower, upper}} } if nums[0] > lower { - ans = append(ans, f(lower, nums[0]-1)) + ans = append(ans, []int{lower, nums[0] - 1}) } - for i := 1; i < n; i++ { - a, b := nums[i-1], nums[i] - if b-a > 1 { - ans = append(ans, f(a+1, b-1)) + for i, b := range nums[1:] { + if a := nums[i]; b-a > 1 { + ans = append(ans, []int{a + 1, b - 1}) } } if nums[n-1] < upper { - ans = append(ans, f(nums[n-1]+1, upper)) + ans = append(ans, []int{nums[n-1] + 1, upper}) } return } \ No newline at end of file diff --git a/solution/0100-0199/0163.Missing Ranges/Solution.java b/solution/0100-0199/0163.Missing Ranges/Solution.java index dd49d243ef0ef..3f882d888c2f7 100644 --- a/solution/0100-0199/0163.Missing Ranges/Solution.java +++ b/solution/0100-0199/0163.Missing Ranges/Solution.java @@ -1,27 +1,21 @@ -class Solution { - public List<String> findMissingRanges(int[] nums, int lower, int upper) { - int n = nums.length; - List<String> ans = new ArrayList<>(); - if (n == 0) { - ans.add(f(lower, upper)); - return ans; - } - if (nums[0] > lower) { - ans.add(f(lower, nums[0] - 1)); - } - for (int i = 1; i < n; ++i) { - int a = nums[i - 1], b = nums[i]; - if (b - a > 1) { - ans.add(f(a + 1, b - 1)); - } - } - if (nums[n - 1] < upper) { - ans.add(f(nums[n - 1] + 1, upper)); - } - return ans; - } - - private String f(int a, int b) { - return a == b ? a + "" : a + "->" + b; - } +class Solution { + public List<List<Integer>> findMissingRanges(int[] nums, int lower, int upper) { + int n = nums.length; + if (n == 0) { + return List.of(List.of(lower, upper)); + } + List<List<Integer>> ans = new ArrayList<>(); + if (nums[0] > lower) { + ans.add(List.of(lower, nums[0] - 1)); + } + for (int i = 1; i < n; ++i) { + if (nums[i] - nums[i - 1] > 1) { + ans.add(List.of(nums[i - 1] + 1, nums[i] - 1)); + } + } + if (nums[n - 1] < upper) { + ans.add(List.of(nums[n - 1] + 1, upper)); + } + return ans; + } } \ No newline at end of file diff --git a/solution/0100-0199/0163.Missing Ranges/Solution.py b/solution/0100-0199/0163.Missing Ranges/Solution.py index 5a5aea904a5f2..18e9817b7eb39 100644 --- a/solution/0100-0199/0163.Missing Ranges/Solution.py +++ b/solution/0100-0199/0163.Missing Ranges/Solution.py @@ -1,17 +1,16 @@ -class Solution: - def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[str]: - def f(a, b): - return str(a) if a == b else f'{a}->{b}' - - n = len(nums) - if n == 0: - return [f(lower, upper)] - ans = [] - if nums[0] > lower: - ans.append(f(lower, nums[0] - 1)) - for a, b in pairwise(nums): - if b - a > 1: - ans.append(f(a + 1, b - 1)) - if nums[-1] < upper: - ans.append(f(nums[-1] + 1, upper)) - return ans +class Solution: + def findMissingRanges( + self, nums: List[int], lower: int, upper: int + ) -> List[List[int]]: + n = len(nums) + if n == 0: + return [[lower, upper]] + ans = [] + if nums[0] > lower: + ans.append([lower, nums[0] - 1]) + for a, b in pairwise(nums): + if b - a > 1: + ans.append([a + 1, b - 1]) + if nums[-1] < upper: + ans.append([nums[-1] + 1, upper]) + return ans diff --git a/solution/0100-0199/0163.Missing Ranges/Solution.ts b/solution/0100-0199/0163.Missing Ranges/Solution.ts new file mode 100644 index 0000000000000..3fc65d338f377 --- /dev/null +++ b/solution/0100-0199/0163.Missing Ranges/Solution.ts @@ -0,0 +1,19 @@ +function findMissingRanges(nums: number[], lower: number, upper: number): number[][] { + const n = nums.length; + if (n === 0) { + return [[lower, upper]]; + } + const ans: number[][] = []; + if (nums[0] > lower) { + ans.push([lower, nums[0] - 1]); + } + for (let i = 1; i < n; ++i) { + if (nums[i] - nums[i - 1] > 1) { + ans.push([nums[i - 1] + 1, nums[i] - 1]); + } + } + if (nums[n - 1] < upper) { + ans.push([nums[n - 1] + 1, upper]); + } + return ans; +}