From 82e2e3e0618c5a82a29595244bca9b223a083595 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 21 Jan 2025 19:33:02 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2229 (#3978) No.2229.Check if an Array Is Consecutive --- .../README.md | 98 +++++++++++++++---- .../README_EN.md | 92 +++++++++++++---- .../Solution.cpp | 18 ++-- .../Solution.go | 11 ++- .../Solution.java | 18 ++-- .../Solution.js | 17 ++++ .../Solution.py | 3 +- .../Solution.ts | 13 +++ 8 files changed, 211 insertions(+), 59 deletions(-) create mode 100644 solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.js create mode 100644 solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.ts diff --git a/solution/2200-2299/2229.Check if an Array Is Consecutive/README.md b/solution/2200-2299/2229.Check if an Array Is Consecutive/README.md index 24d8ac69375f0..311cc1ebaf80f 100644 --- a/solution/2200-2299/2229.Check if an Array Is Consecutive/README.md +++ b/solution/2200-2299/2229.Check if an Array Is Consecutive/README.md @@ -41,9 +41,9 @@ tags: 输入:nums = [1,3] 输出:false 解释: -最小值是 1 ,数组长度为 2 。 -范围 [x, x + n - 1] 中的所有值没有都出现在 nums 中:[1, 1 + 2 - 1] = [1, 2] = (1, 2) 。 -因此,nums 不是一个连贯数组。 +最小值是 1 ,数组长度为 2 。 +范围 [x, x + n - 1] 中的所有值没有都出现在 nums 中:[1, 1 + 2 - 1] = [1, 2] = (1, 2) 。 +因此,nums 不是一个连贯数组。

示例 3:

@@ -71,7 +71,13 @@ tags: -### 方法一 +### 方法一:哈希表 + +我们可以用一个哈希表 $\textit{s}$ 来存储数组 $\textit{nums}$ 中的所有元素,用两个变量 $\textit{mi}$ 和 $\textit{mx}$ 分别表示数组中的最小值和最大值。 + +如果数组中的所有元素都不相同,且数组的长度等于最大值和最小值之间的差值加 $1$,那么数组就是连贯数组,返回 $\textit{true}$;否则返回 $\textit{false}$。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。 @@ -81,8 +87,7 @@ tags: class Solution: def isConsecutive(self, nums: List[int]) -> bool: mi, mx = min(nums), max(nums) - n = len(nums) - return len(set(nums)) == n and mx == mi + n - 1 + return len(set(nums)) == mx - mi + 1 == len(nums) ``` #### Java @@ -90,16 +95,16 @@ class Solution: ```java class Solution { public boolean isConsecutive(int[] nums) { - int mi = nums[0]; - int mx = nums[0]; + int mi = nums[0], mx = 0; Set s = new HashSet<>(); - for (int v : nums) { - mi = Math.min(mi, v); - mx = Math.max(mx, v); - s.add(v); + for (int x : nums) { + if (!s.add(x)) { + return false; + } + mi = Math.min(mi, x); + mx = Math.max(mx, x); } - int n = nums.length; - return s.size() == n && mx == mi + n - 1; + return mx - mi + 1 == nums.length; } } ``` @@ -110,11 +115,17 @@ class Solution { class Solution { public: bool isConsecutive(vector& nums) { - unordered_set s(nums.begin(), nums.end()); - int mi = *min_element(nums.begin(), nums.end()); - int mx = *max_element(nums.begin(), nums.end()); - int n = nums.size(); - return s.size() == n && mx == mi + n - 1; + unordered_set s; + int mi = nums[0], mx = 0; + for (int x : nums) { + if (s.contains(x)) { + return false; + } + s.insert(x); + mi = min(mi, x); + mx = max(mx, x); + } + return mx - mi + 1 == nums.size(); } }; ``` @@ -124,14 +135,59 @@ public: ```go func isConsecutive(nums []int) bool { s := map[int]bool{} - mi, mx := slices.Min(nums), slices.Max(nums) + mi, mx := nums[0], 0 for _, x := range nums { + if s[x] { + return false + } s[x] = true + mi = min(mi, x) + mx = max(mx, x) } - return len(s) == len(nums) && mx == mi+len(nums)-1 + return mx-mi+1 == len(nums) } ``` +#### TypeScript + +```ts +function isConsecutive(nums: number[]): boolean { + let [mi, mx] = [nums[0], 0]; + const s = new Set(); + for (const x of nums) { + if (s.has(x)) { + return false; + } + s.add(x); + mi = Math.min(mi, x); + mx = Math.max(mx, x); + } + return mx - mi + 1 === nums.length; +} +``` + +#### JavaScript + +```js +/** + * @param {number[]} nums + * @return {boolean} + */ +var isConsecutive = function (nums) { + let [mi, mx] = [nums[0], 0]; + const s = new Set(); + for (const x of nums) { + if (s.has(x)) { + return false; + } + s.add(x); + mi = Math.min(mi, x); + mx = Math.max(mx, x); + } + return mx - mi + 1 === nums.length; +}; +``` + diff --git a/solution/2200-2299/2229.Check if an Array Is Consecutive/README_EN.md b/solution/2200-2299/2229.Check if an Array Is Consecutive/README_EN.md index d1529274fc66a..5f7244517fa35 100644 --- a/solution/2200-2299/2229.Check if an Array Is Consecutive/README_EN.md +++ b/solution/2200-2299/2229.Check if an Array Is Consecutive/README_EN.md @@ -70,7 +70,13 @@ Therefore, nums is consecutive. -### Solution 1 +### Solution 1: Hash Table + +We can use a hash table $\textit{s}$ to store all the elements in the array $\textit{nums}$, and use two variables $\textit{mi}$ and $\textit{mx}$ to represent the minimum and maximum values in the array, respectively. + +If all elements in the array are distinct and the length of the array equals the difference between the maximum and minimum values plus $1$, then the array is consecutive, and we return $\textit{true}$; otherwise, we return $\textit{false}$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$. @@ -80,8 +86,7 @@ Therefore, nums is consecutive. class Solution: def isConsecutive(self, nums: List[int]) -> bool: mi, mx = min(nums), max(nums) - n = len(nums) - return len(set(nums)) == n and mx == mi + n - 1 + return len(set(nums)) == mx - mi + 1 == len(nums) ``` #### Java @@ -89,16 +94,16 @@ class Solution: ```java class Solution { public boolean isConsecutive(int[] nums) { - int mi = nums[0]; - int mx = nums[0]; + int mi = nums[0], mx = 0; Set s = new HashSet<>(); - for (int v : nums) { - mi = Math.min(mi, v); - mx = Math.max(mx, v); - s.add(v); + for (int x : nums) { + if (!s.add(x)) { + return false; + } + mi = Math.min(mi, x); + mx = Math.max(mx, x); } - int n = nums.length; - return s.size() == n && mx == mi + n - 1; + return mx - mi + 1 == nums.length; } } ``` @@ -109,11 +114,17 @@ class Solution { class Solution { public: bool isConsecutive(vector& nums) { - unordered_set s(nums.begin(), nums.end()); - int mi = *min_element(nums.begin(), nums.end()); - int mx = *max_element(nums.begin(), nums.end()); - int n = nums.size(); - return s.size() == n && mx == mi + n - 1; + unordered_set s; + int mi = nums[0], mx = 0; + for (int x : nums) { + if (s.contains(x)) { + return false; + } + s.insert(x); + mi = min(mi, x); + mx = max(mx, x); + } + return mx - mi + 1 == nums.size(); } }; ``` @@ -123,14 +134,59 @@ public: ```go func isConsecutive(nums []int) bool { s := map[int]bool{} - mi, mx := slices.Min(nums), slices.Max(nums) + mi, mx := nums[0], 0 for _, x := range nums { + if s[x] { + return false + } s[x] = true + mi = min(mi, x) + mx = max(mx, x) } - return len(s) == len(nums) && mx == mi+len(nums)-1 + return mx-mi+1 == len(nums) } ``` +#### TypeScript + +```ts +function isConsecutive(nums: number[]): boolean { + let [mi, mx] = [nums[0], 0]; + const s = new Set(); + for (const x of nums) { + if (s.has(x)) { + return false; + } + s.add(x); + mi = Math.min(mi, x); + mx = Math.max(mx, x); + } + return mx - mi + 1 === nums.length; +} +``` + +#### JavaScript + +```js +/** + * @param {number[]} nums + * @return {boolean} + */ +var isConsecutive = function (nums) { + let [mi, mx] = [nums[0], 0]; + const s = new Set(); + for (const x of nums) { + if (s.has(x)) { + return false; + } + s.add(x); + mi = Math.min(mi, x); + mx = Math.max(mx, x); + } + return mx - mi + 1 === nums.length; +}; +``` + diff --git a/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.cpp b/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.cpp index d464e98b7172b..1b4c373531e24 100644 --- a/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.cpp +++ b/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.cpp @@ -1,10 +1,16 @@ class Solution { public: bool isConsecutive(vector& nums) { - unordered_set s(nums.begin(), nums.end()); - int mi = *min_element(nums.begin(), nums.end()); - int mx = *max_element(nums.begin(), nums.end()); - int n = nums.size(); - return s.size() == n && mx == mi + n - 1; + unordered_set s; + int mi = nums[0], mx = 0; + for (int x : nums) { + if (s.contains(x)) { + return false; + } + s.insert(x); + mi = min(mi, x); + mx = max(mx, x); + } + return mx - mi + 1 == nums.size(); } -}; \ No newline at end of file +}; diff --git a/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.go b/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.go index 0128845b2eb21..08dc608f9e1e0 100644 --- a/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.go +++ b/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.go @@ -1,8 +1,13 @@ func isConsecutive(nums []int) bool { s := map[int]bool{} - mi, mx := slices.Min(nums), slices.Max(nums) + mi, mx := nums[0], 0 for _, x := range nums { + if s[x] { + return false + } s[x] = true + mi = min(mi, x) + mx = max(mx, x) } - return len(s) == len(nums) && mx == mi+len(nums)-1 -} \ No newline at end of file + return mx-mi+1 == len(nums) +} diff --git a/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.java b/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.java index e58dbdaa3d138..413e69982eea4 100644 --- a/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.java +++ b/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.java @@ -1,14 +1,14 @@ class Solution { public boolean isConsecutive(int[] nums) { - int mi = nums[0]; - int mx = nums[0]; + int mi = nums[0], mx = 0; Set s = new HashSet<>(); - for (int v : nums) { - mi = Math.min(mi, v); - mx = Math.max(mx, v); - s.add(v); + for (int x : nums) { + if (!s.add(x)) { + return false; + } + mi = Math.min(mi, x); + mx = Math.max(mx, x); } - int n = nums.length; - return s.size() == n && mx == mi + n - 1; + return mx - mi + 1 == nums.length; } -} \ No newline at end of file +} diff --git a/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.js b/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.js new file mode 100644 index 0000000000000..607d0a8e8a603 --- /dev/null +++ b/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.js @@ -0,0 +1,17 @@ +/** + * @param {number[]} nums + * @return {boolean} + */ +var isConsecutive = function (nums) { + let [mi, mx] = [nums[0], 0]; + const s = new Set(); + for (const x of nums) { + if (s.has(x)) { + return false; + } + s.add(x); + mi = Math.min(mi, x); + mx = Math.max(mx, x); + } + return mx - mi + 1 === nums.length; +}; diff --git a/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.py b/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.py index f44b88be569a9..8eccea14df0eb 100644 --- a/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.py +++ b/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.py @@ -1,5 +1,4 @@ class Solution: def isConsecutive(self, nums: List[int]) -> bool: mi, mx = min(nums), max(nums) - n = len(nums) - return len(set(nums)) == n and mx == mi + n - 1 + return len(set(nums)) == mx - mi + 1 == len(nums) diff --git a/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.ts b/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.ts new file mode 100644 index 0000000000000..f6b0a4e8144fe --- /dev/null +++ b/solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.ts @@ -0,0 +1,13 @@ +function isConsecutive(nums: number[]): boolean { + let [mi, mx] = [nums[0], 0]; + const s = new Set(); + for (const x of nums) { + if (s.has(x)) { + return false; + } + s.add(x); + mi = Math.min(mi, x); + mx = Math.max(mx, x); + } + return mx - mi + 1 === nums.length; +}