class Solution {
public boolean isStraight(int[] nums) {
Set<Integer> set = new HashSet<>();
int max = -1, min = 20;
for(int i = 0; i < nums.length; i++){
if(nums[i] == 0) continue; //大小王
if(set.contains(nums[i]))return false;//判断是否有重复
set.add(nums[i]);//加进来
max = Math.max(nums[i], max);
min = Math.min(nums[i], min); //统计set里面的最大值和最小值
}
return max - min < 5;
}
}
class Solution {
public boolean checkDynasty(int[] places) {
Set<Integer> set = new HashSet<>();
int max=-1;
int min=100;
for(int i=0; i < places.length;i++){
if(places[i]==0) continue;
if(set.contains(places[i])) return false;
set.add(places[i]);
max=Math.max(max,places[i]);
min=Math.min(min,places[i]);
}
return max-min<5;
}
}
class Solution {
public boolean checkDynasty(int[] places) {
Set set= new HashSet<>();
int min = 14;
int max = -1;
for(int place : places){
if(place 0)
continue;
else if(set.contains(place))
return false;
else{
set.add(place);
min = Math.min(min,place);
max = Math.max(max,place);
}
}
System.out.println(max – min);
return max – min < 5;
class Solution:
def checkDynasty(self, places: List[int]) -> bool:
repeat = set()
mi = 14
ma = 0
for place in places:
if place 0: continue
mi = min(mi,place)
ma = max(ma,place)
if place in repeat:return False
else: repeat.add(place)
return ma- mi <5
时间0 1 空间 01
java
class Solution {
public boolean checkDynasty(int[] places) {
Set repeat = new HashSet<>();
int max = 0, min = 14;
for(int place : places){
if(place 0) continue;
max = Math.max(max, place);
min = Math.min(min, place);
if(repeat.contains(place)) return false;
repeat.add(place);
}
return max – min < 5;
}
}
提交
刷地的最优解
class Solution {
public boolean checkDynasty(int[] places) {
Set<Integer> set = new HashSet<>();
int max = -1;
int min = 20;
for(int i=0;i<places.length;i++){
if(places[i]==0){
continue;
}
if(set.contains(places[i])){
return false;
}else{
set.add(places[i]);
max = Math.max(places[i],max);
min = Math.min(places[i],min);
}
}
return max-min<5;
}
}
共以下 15 个回答
class Solution {& places) {
public:
bool checkDynasty(vector
//max-min < 5,即满足条件。因为总共5张牌,13个号码。若是
int unknown = 0;
sort(places.begin(), places.end()); // 数组排序
for(int i = 0; i < 4; i++) {
if(places[i] 0) unknown++; // 统计未知朝代数量
else if(places[i] places[i + 1]) return false; // 若有重复,提前返回 false
}
return places[4] – places[unknown] < 5; // 最大编号朝代 – 最小编号朝代 < 5 则连续
}
};
class Solution {& places) { s;
public:
bool checkDynasty(vector
set
int min_num = 14;
int max_num = -1;
for(int i = 0; i < places.size(); i++)
{
if(places[i] 0)
continue;
if(s.count(places[i]))
return false;
s.insert(places[i]);
min_num = min(min_num, places[i]);
max_num = max(max_num, places[i]);
}
return max_num – min_num < 5;
}
};
时间和空间复杂度都是O(n)
如果要成为一个顺子,则需要 满足两个条件 (充要条件)
1、不存在有重复的数,大小王除外
2、最大值 – 最小值 < 5,大小王除外
时间复杂度:O(n)
空间复杂度:O(n)
// 集合 n,n// 如果要成为一个顺子,则需要 满足两个条件
// 1、不存在有重复的数,大小王除外
// 2、最大值 – 最小值 < 5,大小王除外
时间复杂度:O(n)
额外空间复杂度:O(n)
先排序再判断
时间复杂度O(5)
空间复杂度O(1)
“`C++& places) {
class Solution {
public:
bool checkDynasty(vector
int unknown = 0;
sort(places.begin(), places.end());
for(int i = 0; i < places.size() - 1; i++) { if(places[i] == 0) { unknown++; continue; } else if(places[i] == places[i + 1]) { return false; } } return places[4] - places[unknown] < 5; } }; ```
class Solution { set= new HashSet<>();
public boolean checkDynasty(int[] places) {
Set
int min = 14;
int max = -1;
for(int place : places){
if(place 0)
continue;
else if(set.contains(place))
return false;
else{
set.add(place);
min = Math.min(min,place);
max = Math.max(max,place);
}
}
System.out.println(max – min);
return max – min < 5;
}
时间O1
空间O1
打卡
时间和空间复杂度都是$O(1)$
class Solution:
def checkDynasty(self, places: List[int]) -> bool:
repeat = set()
mi = 14
ma = 0
for place in places:
if place 0: continue
mi = min(mi,place)
ma = max(ma,place)
if place in repeat:return False
else: repeat.add(place)
return ma- mi <5
时间0 1 空间 01
java repeat = new HashSet<>();
class Solution {
public boolean checkDynasty(int[] places) {
Set
int max = 0, min = 14;
for(int place : places){
if(place 0) continue;
max = Math.max(max, place);
min = Math.min(min, place);
if(repeat.contains(place)) return false;
repeat.add(place);
}
return max – min < 5;
}
}
提交
思路:给定数组中的数字需要满足两个条件:
1. 不包含重复数字,用set查询
2. 除了0之外,最大值与最小值的差值小于5,则可以组成顺子,否则不能。
“`c++& places) { s;
class Solution {
public:
bool checkDynasty(vector
set
int mini = 14, maxi = -1, count = 0;
for (int i=0; i
时间复杂度:O(n)
空间复杂度:O(n)