Skip to content

Commit b3a35fa

Browse files
committed
feat: add solutions to lc problem: No.2229
No.2229.Check if an Array Is Consecutive
1 parent cd3a929 commit b3a35fa

File tree

6 files changed

+177
-2
lines changed

6 files changed

+177
-2
lines changed

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

+62-1
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,76 @@ Therefore, nums is consecutive.
6363
<!-- 这里可写当前语言的特殊实现逻辑 -->
6464

6565
```python
66-
66+
class Solution:
67+
def isConsecutive(self, nums: List[int]) -> bool:
68+
mi, mx = min(nums), max(nums)
69+
n = len(nums)
70+
return len(set(nums)) == n and mx == mi + n - 1
6771
```
6872

6973
### **Java**
7074

7175
<!-- 这里可写当前语言的特殊实现逻辑 -->
7276

7377
```java
78+
class Solution {
79+
public boolean isConsecutive(int[] nums) {
80+
int mi = nums[0];
81+
int mx = nums[0];
82+
Set<Integer> s = new HashSet<>();
83+
for (int v : nums) {
84+
mi = Math.min(mi, v);
85+
mx = Math.max(mx, v);
86+
s.add(v);
87+
}
88+
int n = nums.length;
89+
return s.size() == n && mx == mi + n - 1;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
bool isConsecutive(vector<int>& nums) {
100+
unordered_set<int> s(nums.begin(), nums.end());
101+
int mi = *min_element(nums.begin(), nums.end());
102+
int mx = *max_element(nums.begin(), nums.end());
103+
int n = nums.size();
104+
return s.size() == n && mx == mi + n - 1;
105+
}
106+
};
107+
```
74108
109+
### **Go**
110+
111+
```go
112+
func isConsecutive(nums []int) bool {
113+
s := make(map[int]bool)
114+
mi, mx := nums[0], nums[0]
115+
for _, v := range nums {
116+
s[v] = true
117+
mi = min(mi, v)
118+
mx = max(mx, v)
119+
}
120+
return len(s) == len(nums) && mx == mi+len(nums)-1
121+
}
122+
123+
func max(a, b int) int {
124+
if a > b {
125+
return a
126+
}
127+
return b
128+
}
129+
130+
func min(a, b int) int {
131+
if a < b {
132+
return a
133+
}
134+
return b
135+
}
75136
```
76137

77138
### **TypeScript**

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

+62-1
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,74 @@ Therefore, nums is consecutive.
5757
### **Python3**
5858

5959
```python
60-
60+
class Solution:
61+
def isConsecutive(self, nums: List[int]) -> bool:
62+
mi, mx = min(nums), max(nums)
63+
n = len(nums)
64+
return len(set(nums)) == n and mx == mi + n - 1
6165
```
6266

6367
### **Java**
6468

6569
```java
70+
class Solution {
71+
public boolean isConsecutive(int[] nums) {
72+
int mi = nums[0];
73+
int mx = nums[0];
74+
Set<Integer> s = new HashSet<>();
75+
for (int v : nums) {
76+
mi = Math.min(mi, v);
77+
mx = Math.max(mx, v);
78+
s.add(v);
79+
}
80+
int n = nums.length;
81+
return s.size() == n && mx == mi + n - 1;
82+
}
83+
}
84+
```
85+
86+
### **C++**
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
bool isConsecutive(vector<int>& nums) {
92+
unordered_set<int> s(nums.begin(), nums.end());
93+
int mi = *min_element(nums.begin(), nums.end());
94+
int mx = *max_element(nums.begin(), nums.end());
95+
int n = nums.size();
96+
return s.size() == n && mx == mi + n - 1;
97+
}
98+
};
99+
```
66100
101+
### **Go**
102+
103+
```go
104+
func isConsecutive(nums []int) bool {
105+
s := make(map[int]bool)
106+
mi, mx := nums[0], nums[0]
107+
for _, v := range nums {
108+
s[v] = true
109+
mi = min(mi, v)
110+
mx = max(mx, v)
111+
}
112+
return len(s) == len(nums) && mx == mi+len(nums)-1
113+
}
114+
115+
func max(a, b int) int {
116+
if a > b {
117+
return a
118+
}
119+
return b
120+
}
121+
122+
func min(a, b int) int {
123+
if a < b {
124+
return a
125+
}
126+
return b
127+
}
67128
```
68129

69130
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
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;
9+
}
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func isConsecutive(nums []int) bool {
2+
s := make(map[int]bool)
3+
mi, mx := nums[0], nums[0]
4+
for _, v := range nums {
5+
s[v] = true
6+
mi = min(mi, v)
7+
mx = max(mx, v)
8+
}
9+
return len(s) == len(nums) && mx == mi+len(nums)-1
10+
}
11+
12+
func max(a, b int) int {
13+
if a > b {
14+
return a
15+
}
16+
return b
17+
}
18+
19+
func min(a, b int) int {
20+
if a < b {
21+
return a
22+
}
23+
return b
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public boolean isConsecutive(int[] nums) {
3+
int mi = nums[0];
4+
int mx = nums[0];
5+
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);
10+
}
11+
int n = nums.length;
12+
return s.size() == n && mx == mi + n - 1;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def isConsecutive(self, nums: List[int]) -> bool:
3+
mi, mx = min(nums), max(nums)
4+
n = len(nums)
5+
return len(set(nums)) == n and mx == mi + n - 1

0 commit comments

Comments
 (0)