-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.2229 (#3978)
No.2229.Check if an Array Is Consecutive
- Loading branch information
Showing
8 changed files
with
211 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 12 additions & 6 deletions
18
solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; | ||
}; |
11 changes: 8 additions & 3 deletions
11
solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
18 changes: 9 additions & 9 deletions
18
solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
3 changes: 1 addition & 2 deletions
3
solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
13 changes: 13 additions & 0 deletions
13
solution/2200-2299/2229.Check if an Array Is Consecutive/Solution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |