class Solution {
public boolean checkDynasty(int[] places) {
int min = 14;
int max = 0;
HashSet<Integer> map = new HashSet<>();
for (int place : places) {
if (place == 0)
continue;
else {
if (map.contains(place))
return false;
else
map.add(place);
if (place < min)
min = place;
if (place > max)
max = place;
}
}
if ((max - min) <= 4)
return true;
else
return false;
}
}
class Solution {
public boolean checkDynasty(int[] places) {
// 1、不能重复(除了0)
// 2、最大值和最小值不能超过4(除了0)
// 先排序+再遍历
Arrays.sort(places);
int min = 0;
int max = 0;
for (int i = 0; i < places.length; i++) {
if (places[i] != 0) {
// 找到第一个不为0的
if (min == 0 || max == 0) {
min = places[i];
max = places[i];
}
// 不能重复(必须排序后才好比较)
if (i < places.length - 1 && places[i] == places[i + 1]) {
return false;
}
min = Math.min(places[i], min);
max = Math.max(places[i], max);
// 最大值和最小值不能超过4(除了0)
if (max - min >= 5) {
return false;
}
}
}
return true;
}
}
【题解2】排序+遍历(完全利用好排序特性的)
class Solution {
public boolean checkDynasty(int[] places) {
// 1、不能重复(除了0)
// 2、最大值和最小值不能超过4(除了0)
// 先排序+再遍历
Arrays.sort(places);
int zeroCount = 0;
// 遍历到第4位即可
for (int i = 0; i < places.length - 1; i++) {
if (places[i] == 0) {
// 利用排序后从小到大的特性
zeroCount++;
} else {
// 不能重复(必须排序后才好比较),因为遍历到i < places.length-1,所以不用判断边界问题
if (places[i] == places[i + 1]) {
return false;
}
}
}
// 利用排序特性
// places[zeroCount] 一定是最小值
// (places[places.length -1] 一定是最大值
if (places[places.length - 1] - places[zeroCount] >= 5) {
return false;
}
return true;
}
}
【题解3】用Hash表存数字用于判断重复性,用min和max来比较极限大小
class Solution {
public boolean checkDynasty(int[] places) {
// 1、不能重复(除了0)
// 2、最大值和最小值不能超过4(除了0)
// 使用Set来存储数据判断是否重复,使用min和max来存储最大和最小值
Set<Integer> set = new HashSet();
int min = 0;
int max = 0;
for (int i = 0; i < places.length; i++) {
int current = places[i];
if (current == 0) {
continue;
}
if (set.contains(current)) {
return false;
}
set.add(current);
if (min == 0 || max == 0) {
// 第一次需要进行赋值
min = current;
max = current;
} else {
min = Math.min(min, current);
max = Math.max(max, current);
}
}
return max - min <= 4;
}
}
class Solution {
public boolean checkDynasty(int[] places) {
HashSet<Integer> set = new HashSet<>();
int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
for (int a : places) {
// 不能重复
if (a == 0)
continue;
if (set.contains(a))
return false;
set.add(a);
max = Math.max(a, max);
min = Math.min(a, min);
}
// 最大最小值差值小于5
return max - min < 5;
}
}
共以下 9 个回答
时间空间复杂度O(1)
解题思路
– 能组成连续序列的数组应当满足2个条件
– 1、里面的任一元素不能重复(除了0)
– 2、最大值和最小值不能超过4(除了0)
【题解1】排序+遍历(未完全利用好排序特性的)
【题解2】排序+遍历(完全利用好排序特性的)
【题解3】用Hash表存数字用于判断重复性,用min和max来比较极限大小
排序后统计第一个不为零的元素与最大元素的差值,若小于5,则为顺子。
时间复杂度O(Nlogn),空间复杂度O(1)。
class Solution { repeat = new HashSet<>();
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; // 若有重复,提前返回 false
repeat.add(place); // 添加此朝代至 Set
}
return max – min < 5; // 最大编号朝代 – 最小编号朝代 < 5 则连续
}
实现两个条件
1.无重复元素
2.最大朝代-最小朝代<5
O1 O1
思路:连续的五个朝代,说明这一排的文物无重复,去掉 0 后,
max - min < 5即可。时间复杂度:O(n) | O(n)
空间复杂度:O(n) | O(1)