Skip to content

Commit 9659f27

Browse files
authored
Create check-if-an-array-is-consecutive.py
1 parent 2e1e378 commit 9659f27

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
# hash table
5+
class Solution(object):
6+
def isConsecutive(self, nums):
7+
"""
8+
:type nums: List[int]
9+
:rtype: bool
10+
"""
11+
return max(nums)-min(nums)+1 == len(nums) == len(set(nums))
12+
13+
14+
# Time: O(nlogn)
15+
# Space: O(1)
16+
# sort
17+
class Solution2(object):
18+
def isConsecutive(self, nums):
19+
"""
20+
:type nums: List[int]
21+
:rtype: bool
22+
"""
23+
nums.sort()
24+
return all(nums[i]+1 == nums[i+1] for i in xrange(len(nums)-1))

0 commit comments

Comments
 (0)