File tree 6 files changed +177
-2
lines changed
solution/2200-2299/2229.Check if an Array Is Consecutive
6 files changed +177
-2
lines changed Original file line number Diff line number Diff line change @@ -63,15 +63,76 @@ Therefore, nums is consecutive.
63
63
<!-- 这里可写当前语言的特殊实现逻辑 -->
64
64
65
65
``` 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
67
71
```
68
72
69
73
### ** Java**
70
74
71
75
<!-- 这里可写当前语言的特殊实现逻辑 -->
72
76
73
77
``` 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
+ ```
74
108
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
+ }
75
136
```
76
137
77
138
### ** TypeScript**
Original file line number Diff line number Diff line change @@ -57,13 +57,74 @@ Therefore, nums is consecutive.
57
57
### ** Python3**
58
58
59
59
``` 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
61
65
```
62
66
63
67
### ** Java**
64
68
65
69
``` 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
+ ```
66
100
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
+ }
67
128
```
68
129
69
130
### ** TypeScript**
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments