Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.2229 (#3978)
Browse files Browse the repository at this point in the history
No.2229.Check if an Array Is Consecutive
  • Loading branch information
yanglbme authored Jan 21, 2025
1 parent 2ec66da commit 82e2e3e
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 59 deletions.
98 changes: 77 additions & 21 deletions solution/2200-2299/2229.Check if an Array Is Consecutive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ tags:
<strong>输入:</strong>nums = [1,3]
<strong>输出:</strong>false
<strong>解释:
</strong>最小值是 1 ,数组长度为 2 。
范围 [x, x + n - 1] 中的所有值没有都出现在 nums 中:[1, 1 + 2 - 1] = [1, 2] = (1, 2) 。
因此,nums 不是一个连贯数组。
</strong>最小值是 1 ,数组长度为 2 。
范围 [x, x + n - 1] 中的所有值没有都出现在 nums 中:[1, 1 + 2 - 1] = [1, 2] = (1, 2) 。
因此,nums 不是一个连贯数组。
</pre>

<p><strong>示例 3:</strong></p>
Expand Down Expand Up @@ -71,7 +71,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:哈希表

我们可以用一个哈希表 $\textit{s}$ 来存储数组 $\textit{nums}$ 中的所有元素,用两个变量 $\textit{mi}$ 和 $\textit{mx}$ 分别表示数组中的最小值和最大值。

如果数组中的所有元素都不相同,且数组的长度等于最大值和最小值之间的差值加 $1$,那么数组就是连贯数组,返回 $\textit{true}$;否则返回 $\textit{false}$。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。

<!-- tabs:start -->

Expand All @@ -81,25 +87,24 @@ 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

```java
class Solution {
public boolean isConsecutive(int[] nums) {
int mi = nums[0];
int mx = nums[0];
int mi = nums[0], mx = 0;
Set<Integer> 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;
}
}
```
Expand All @@ -110,11 +115,17 @@ class Solution {
class Solution {
public:
bool isConsecutive(vector<int>& nums) {
unordered_set<int> 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<int> 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();
}
};
```
Expand All @@ -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<number>();
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;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ Therefore, nums is consecutive.

<!-- solution:start -->

### 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}$.

<!-- tabs:start -->

Expand All @@ -80,25 +86,24 @@ 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

```java
class Solution {
public boolean isConsecutive(int[] nums) {
int mi = nums[0];
int mx = nums[0];
int mi = nums[0], mx = 0;
Set<Integer> 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;
}
}
```
Expand All @@ -109,11 +114,17 @@ class Solution {
class Solution {
public:
bool isConsecutive(vector<int>& nums) {
unordered_set<int> 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<int> 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();
}
};
```
Expand All @@ -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<number>();
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;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
class Solution {
public:
bool isConsecutive(vector<int>& nums) {
unordered_set<int> 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<int> 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();
}
};
};
Original file line number Diff line number Diff line change
@@ -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
}
return mx-mi+1 == len(nums)
}
Original file line number Diff line number Diff line change
@@ -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<Integer> 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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function isConsecutive(nums: number[]): boolean {
let [mi, mx] = [nums[0], 0];
const s = new Set<number>();
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;
}

0 comments on commit 82e2e3e

Please sign in to comment.