剑指 Offer 61. 扑克牌中的顺子
思路:除0以外的最大值减最小值小于数组长度(说明数组中的数都在最大值与最小值之间),可能为12345,或者12225,结果都为fasle,而10005结果为true,说明只要数组中没有重复元素即可。
class Solution {
public boolean isStraight(int[] nums) {
HashSet<Integer> set = new HashSet<Integer>();
int min = 14, max = 0;
for(int i = 0; i < nums.length; i++) {
if(nums[i] == 0) continue;
if(set.contains(nums[i])) return false;
else set.add(nums[i]);
if(nums[i] < min) min = nums[i];
if(nums[i] > max) max = nums[i];
}
if(max - min < nums.length) return true;
else return false;
}
}
时间复杂度O(N)
空间复杂度O(N)