Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 82e2e3e

Browse files
authored
feat: add solutions to lc problem: No.2229 (doocs#3978)
No.2229.Check if an Array Is Consecutive
1 parent 2ec66da commit 82e2e3e

File tree

8 files changed

+211
-59
lines changed

8 files changed

+211
-59
lines changed

solution/2200-2299/2229.Check if an Array Is Consecutive/README.md

Lines changed: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ tags:
4141
<strong>输入:</strong>nums = [1,3]
4242
<strong>输出:</strong>false
4343
<strong>解释:
44-
</strong>最小值是 1 ,数组长度为 2 。
45-
范围 [x, x + n - 1] 中的所有值没有都出现在 nums 中:[1, 1 + 2 - 1] = [1, 2] = (1, 2) 。
46-
因此,nums 不是一个连贯数组。
44+
</strong>最小值是 1 ,数组长度为 2 。
45+
范围 [x, x + n - 1] 中的所有值没有都出现在 nums 中:[1, 1 + 2 - 1] = [1, 2] = (1, 2) 。
46+
因此,nums 不是一个连贯数组。
4747
</pre>
4848

4949
<p><strong>示例 3:</strong></p>
@@ -71,7 +71,13 @@ tags:
7171

7272
<!-- solution:start -->
7373

74-
### 方法一
74+
### 方法一:哈希表
75+
76+
我们可以用一个哈希表 $\textit{s}$ 来存储数组 $\textit{nums}$ 中的所有元素,用两个变量 $\textit{mi}$ 和 $\textit{mx}$ 分别表示数组中的最小值和最大值。
77+
78+
如果数组中的所有元素都不相同,且数组的长度等于最大值和最小值之间的差值加 $1$,那么数组就是连贯数组,返回 $\textit{true}$;否则返回 $\textit{false}$。
79+
80+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。
7581

7682
<!-- tabs:start -->
7783

@@ -81,25 +87,24 @@ tags:
8187
class Solution:
8288
def isConsecutive(self, nums: List[int]) -> bool:
8389
mi, mx = min(nums), max(nums)
84-
n = len(nums)
85-
return len(set(nums)) == n and mx == mi + n - 1
90+
return len(set(nums)) == mx - mi + 1 == len(nums)
8691
```
8792

8893
#### Java
8994

9095
```java
9196
class Solution {
9297
public boolean isConsecutive(int[] nums) {
93-
int mi = nums[0];
94-
int mx = nums[0];
98+
int mi = nums[0], mx = 0;
9599
Set<Integer> s = new HashSet<>();
96-
for (int v : nums) {
97-
mi = Math.min(mi, v);
98-
mx = Math.max(mx, v);
99-
s.add(v);
100+
for (int x : nums) {
101+
if (!s.add(x)) {
102+
return false;
103+
}
104+
mi = Math.min(mi, x);
105+
mx = Math.max(mx, x);
100106
}
101-
int n = nums.length;
102-
return s.size() == n && mx == mi + n - 1;
107+
return mx - mi + 1 == nums.length;
103108
}
104109
}
105110
```
@@ -110,11 +115,17 @@ class Solution {
110115
class Solution {
111116
public:
112117
bool isConsecutive(vector<int>& nums) {
113-
unordered_set<int> s(nums.begin(), nums.end());
114-
int mi = *min_element(nums.begin(), nums.end());
115-
int mx = *max_element(nums.begin(), nums.end());
116-
int n = nums.size();
117-
return s.size() == n && mx == mi + n - 1;
118+
unordered_set<int> s;
119+
int mi = nums[0], mx = 0;
120+
for (int x : nums) {
121+
if (s.contains(x)) {
122+
return false;
123+
}
124+
s.insert(x);
125+
mi = min(mi, x);
126+
mx = max(mx, x);
127+
}
128+
return mx - mi + 1 == nums.size();
118129
}
119130
};
120131
```
@@ -124,14 +135,59 @@ public:
124135
```go
125136
func isConsecutive(nums []int) bool {
126137
s := map[int]bool{}
127-
mi, mx := slices.Min(nums), slices.Max(nums)
138+
mi, mx := nums[0], 0
128139
for _, x := range nums {
140+
if s[x] {
141+
return false
142+
}
129143
s[x] = true
144+
mi = min(mi, x)
145+
mx = max(mx, x)
130146
}
131-
return len(s) == len(nums) && mx == mi+len(nums)-1
147+
return mx-mi+1 == len(nums)
132148
}
133149
```
134150

151+
#### TypeScript
152+
153+
```ts
154+
function isConsecutive(nums: number[]): boolean {
155+
let [mi, mx] = [nums[0], 0];
156+
const s = new Set<number>();
157+
for (const x of nums) {
158+
if (s.has(x)) {
159+
return false;
160+
}
161+
s.add(x);
162+
mi = Math.min(mi, x);
163+
mx = Math.max(mx, x);
164+
}
165+
return mx - mi + 1 === nums.length;
166+
}
167+
```
168+
169+
#### JavaScript
170+
171+
```js
172+
/**
173+
* @param {number[]} nums
174+
* @return {boolean}
175+
*/
176+
var isConsecutive = function (nums) {
177+
let [mi, mx] = [nums[0], 0];
178+
const s = new Set();
179+
for (const x of nums) {
180+
if (s.has(x)) {
181+
return false;
182+
}
183+
s.add(x);
184+
mi = Math.min(mi, x);
185+
mx = Math.max(mx, x);
186+
}
187+
return mx - mi + 1 === nums.length;
188+
};
189+
```
190+
135191
<!-- tabs:end -->
136192

137193
<!-- solution:end -->

solution/2200-2299/2229.Check if an Array Is Consecutive/README_EN.md

Lines changed: 74 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ Therefore, nums is consecutive.
7070

7171
<!-- solution:start -->
7272

73-
### Solution 1
73+
### Solution 1: Hash Table
74+
75+
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.
76+
77+
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}$.
78+
79+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$.
7480

7581
<!-- tabs:start -->
7682

@@ -80,25 +86,24 @@ Therefore, nums is consecutive.
8086
class Solution:
8187
def isConsecutive(self, nums: List[int]) -> bool:
8288
mi, mx = min(nums), max(nums)
83-
n = len(nums)
84-
return len(set(nums)) == n and mx == mi + n - 1
89+
return len(set(nums)) == mx - mi + 1 == len(nums)
8590
```
8691

8792
#### Java
8893

8994
```java
9095
class Solution {
9196
public boolean isConsecutive(int[] nums) {
92-
int mi = nums[0];
93-
int mx = nums[0];
97+
int mi = nums[0], mx = 0;
9498
Set<Integer> s = new HashSet<>();
95-
for (int v : nums) {
96-
mi = Math.min(mi, v);
97-
mx = Math.max(mx, v);
98-
s.add(v);
99+
for (int x : nums) {
100+
if (!s.add(x)) {
101+
return false;
102+
}
103+
mi = Math.min(mi, x);
104+
mx = Math.max(mx, x);
99105
}
100-
int n = nums.length;
101-
return s.size() == n && mx == mi + n - 1;
106+
return mx - mi + 1 == nums.length;
102107
}
103108
}
104109
```
@@ -109,11 +114,17 @@ class Solution {
109114
class Solution {
110115
public:
111116
bool isConsecutive(vector<int>& nums) {
112-
unordered_set<int> s(nums.begin(), nums.end());
113-
int mi = *min_element(nums.begin(), nums.end());
114-
int mx = *max_element(nums.begin(), nums.end());
115-
int n = nums.size();
116-
return s.size() == n && mx == mi + n - 1;
117+
unordered_set<int> s;
118+
int mi = nums[0], mx = 0;
119+
for (int x : nums) {
120+
if (s.contains(x)) {
121+
return false;
122+
}
123+
s.insert(x);
124+
mi = min(mi, x);
125+
mx = max(mx, x);
126+
}
127+
return mx - mi + 1 == nums.size();
117128
}
118129
};
119130
```
@@ -123,14 +134,59 @@ public:
123134
```go
124135
func isConsecutive(nums []int) bool {
125136
s := map[int]bool{}
126-
mi, mx := slices.Min(nums), slices.Max(nums)
137+
mi, mx := nums[0], 0
127138
for _, x := range nums {
139+
if s[x] {
140+
return false
141+
}
128142
s[x] = true
143+
mi = min(mi, x)
144+
mx = max(mx, x)
129145
}
130-
return len(s) == len(nums) && mx == mi+len(nums)-1
146+
return mx-mi+1 == len(nums)
131147
}
132148
```
133149

150+
#### TypeScript
151+
152+
```ts
153+
function isConsecutive(nums: number[]): boolean {
154+
let [mi, mx] = [nums[0], 0];
155+
const s = new Set<number>();
156+
for (const x of nums) {
157+
if (s.has(x)) {
158+
return false;
159+
}
160+
s.add(x);
161+
mi = Math.min(mi, x);
162+
mx = Math.max(mx, x);
163+
}
164+
return mx - mi + 1 === nums.length;
165+
}
166+
```
167+
168+
#### JavaScript
169+
170+
```js
171+
/**
172+
* @param {number[]} nums
173+
* @return {boolean}
174+
*/
175+
var isConsecutive = function (nums) {
176+
let [mi, mx] = [nums[0], 0];
177+
const s = new Set();
178+
for (const x of nums) {
179+
if (s.has(x)) {
180+
return false;
181+
}
182+
s.add(x);
183+
mi = Math.min(mi, x);
184+
mx = Math.max(mx, x);
185+
}
186+
return mx - mi + 1 === nums.length;
187+
};
188+
```
189+
134190
<!-- tabs:end -->
135191

136192
<!-- solution:end -->
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
class Solution {
22
public:
33
bool isConsecutive(vector<int>& nums) {
4-
unordered_set<int> s(nums.begin(), nums.end());
5-
int mi = *min_element(nums.begin(), nums.end());
6-
int mx = *max_element(nums.begin(), nums.end());
7-
int n = nums.size();
8-
return s.size() == n && mx == mi + n - 1;
4+
unordered_set<int> s;
5+
int mi = nums[0], mx = 0;
6+
for (int x : nums) {
7+
if (s.contains(x)) {
8+
return false;
9+
}
10+
s.insert(x);
11+
mi = min(mi, x);
12+
mx = max(mx, x);
13+
}
14+
return mx - mi + 1 == nums.size();
915
}
10-
};
16+
};
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
func isConsecutive(nums []int) bool {
22
s := map[int]bool{}
3-
mi, mx := slices.Min(nums), slices.Max(nums)
3+
mi, mx := nums[0], 0
44
for _, x := range nums {
5+
if s[x] {
6+
return false
7+
}
58
s[x] = true
9+
mi = min(mi, x)
10+
mx = max(mx, x)
611
}
7-
return len(s) == len(nums) && mx == mi+len(nums)-1
8-
}
12+
return mx-mi+1 == len(nums)
13+
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class Solution {
22
public boolean isConsecutive(int[] nums) {
3-
int mi = nums[0];
4-
int mx = nums[0];
3+
int mi = nums[0], mx = 0;
54
Set<Integer> s = new HashSet<>();
6-
for (int v : nums) {
7-
mi = Math.min(mi, v);
8-
mx = Math.max(mx, v);
9-
s.add(v);
5+
for (int x : nums) {
6+
if (!s.add(x)) {
7+
return false;
8+
}
9+
mi = Math.min(mi, x);
10+
mx = Math.max(mx, x);
1011
}
11-
int n = nums.length;
12-
return s.size() == n && mx == mi + n - 1;
12+
return mx - mi + 1 == nums.length;
1313
}
14-
}
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var isConsecutive = function (nums) {
6+
let [mi, mx] = [nums[0], 0];
7+
const s = new Set();
8+
for (const x of nums) {
9+
if (s.has(x)) {
10+
return false;
11+
}
12+
s.add(x);
13+
mi = Math.min(mi, x);
14+
mx = Math.max(mx, x);
15+
}
16+
return mx - mi + 1 === nums.length;
17+
};
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class Solution:
22
def isConsecutive(self, nums: List[int]) -> bool:
33
mi, mx = min(nums), max(nums)
4-
n = len(nums)
5-
return len(set(nums)) == n and mx == mi + n - 1
4+
return len(set(nums)) == mx - mi + 1 == len(nums)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function isConsecutive(nums: number[]): boolean {
2+
let [mi, mx] = [nums[0], 0];
3+
const s = new Set<number>();
4+
for (const x of nums) {
5+
if (s.has(x)) {
6+
return false;
7+
}
8+
s.add(x);
9+
mi = Math.min(mi, x);
10+
mx = Math.max(mx, x);
11+
}
12+
return mx - mi + 1 === nums.length;
13+
}

0 commit comments

Comments
 (0)