- 题目描述:
- 题目链接:
https://www.nowcoder.com/practice/762836f4d43d43ca9deb273b3de8e1f4?tpId=188&&tqId=38620&rp=1&ru=/activity/oj&qru=/ta/job-code-high-week/question-ranking
-视频讲解链接B站视频讲解
- 设计思想:
- 复杂度分析:
- 代码:
c++版本:
//时间复杂度最优 class Solution { public: bool IsContinuous( vector<int> numbers ) { set<int>se;///用于进行相同数字的判断,这里面不存储0 int Max = -1,Min = 15; for(int i = 0;i < numbers.size();i ++){ if(numbers[i] == 0) continue;//遇见大小王忽略 Max = max(Max,numbers[i]);//更新最大值 Min = min(Min,numbers[i]);//更新最小值 if (se.count(numbers[i]) > 0) return false;//若出现重复数字返回false se.insert(numbers[i]); } return Max - Min < 5; } }; //空间复杂度最优 class Solution { public: bool IsContinuous( vector<int> numbers ) { int index = 0;//找最小值位置 sort(numbers.begin(),numbers.end());//数组排序 for(int i = 0;i < 4;i ++){ if(numbers[i] == 0) index ++; else if(numbers[i] == numbers[i + 1]) return false;//若有重复,返回false } return numbers[4] - numbers[index] < 5;//最大牌 - 最小牌 < 5 则可构成顺子 } };
Java版本:
//时间复杂度最优 import java.util.*; public class Solution { public boolean IsContinuous(int [] numbers) { HashSet<Integer> set = new HashSet<>();///用于进行相同数字的判断,这里面不存储0 int Max = -1,Min = 15; for(int i = 0;i < numbers.length;i ++){ if(numbers[i] == 0) continue;//遇见大小王忽略 Max = Math.max(Max,numbers[i]);//更新最大值 Min = Math.min(Min,numbers[i]);//更新最小值 if(set.contains(numbers[i])) return false;//若出现重复数字返回false set.add(numbers[i]); } return Max - Min < 5; } } //空间复杂度最优 import java.util.*; public class Solution { public boolean IsContinuous(int [] numbers) { int index = 0;//找最小值位置 Arrays.sort(numbers);//数组排序 for(int i = 0;i < 4;i ++){ if(numbers[i] == 0) index ++; else if(numbers[i] == numbers[i + 1]) return false;//若有重复,返回false } return numbers[4] - numbers[index] < 5;//最大牌 - 最小牌 < 5 则可构成顺子 } }
Python版本:
# -*- coding:utf-8 -*- #时间复杂度最优 class Solution: def IsContinuous(self, numbers): # write code here se = set()#用于进行相同数字的判断,这里面不存储0 Max, Min = -1,15 for i in numbers: if i == 0: continue #遇见大小王忽略 Max = max(Max,i) #更新最大值 Min = min(Min,i) #更新最小值 if i in se:return False#若出现重复数字返回false se.add(i) # 添加牌至 Set return Max - Min < 5 #最大牌 - 最小牌 < 5 则可构成顺子 # -*- coding:utf-8 -*- #空间复杂度最优 class Solution: def IsContinuous(self, numbers): # write code here index = 0#找最小值位置 numbers.sort() # 数组排序 for i in range(4): if numbers[i] == 0: index += 1 elif numbers[i] == numbers[i + 1]: return False # 若有重复,返回false return numbers[4] -numbers[index] < 5 # 最大牌 - 最小牌 < 5 则可构成顺子
JavaScript版本:
//时间复杂度最优 function IsContinuous(numbers) { // write code here let se = new Set();///用于进行相同数字的判断,这里面不存储0 let Max = -1,Min = 15; for(let i = 0;i < numbers.length;i ++){ if(numbers[i] == 0) continue;//遇见大小王忽略 Max = Math.max(Max,numbers[i]);//更新最大值 Min = Math.min(Min,numbers[i]);//更新最小值 if(se.has(numbers[i])) return false;//若出现重复数字返回false se.add(numbers[i]); } return Max - Min < 5; } module.exports = { IsContinuous : IsContinuous }; //空间复杂度最优 function IsContinuous(numbers) { // write code here let index = 0;//找最小值位置 numbers.sort((a,b) => a-b);//数组排序 for(let i = 0;i < 4;i ++){ if(numbers[i] == 0) index ++; else if(numbers[i] == numbers[i + 1]) return false;//若有重复,返回false } return numbers[4] - numbers[index] < 5;//最大牌 - 最小牌 < 5 则可构成顺子 } module.exports = { IsContinuous : IsContinuous };