题解 | #没有出现的编号#
没有出现的编号
https://www.nowcoder.com/practice/875d705df65c401a905f574070e09320
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @return int整型一维数组
*/
public int[] findMissingAndMaxNegative (int[] nums) {
// write code here
Set<Integer> set = new TreeSet<>();
for(int i=0;i<nums.length;i++){
set.add(nums[i]);
}
int negative = 0;
for(int value:set){
if(value>=0){
break;
}else{
negative = value;
}
}
int positive = 1;
while (set.contains(positive)){
positive++;
}
return new int[]{positive,negative};
}
}
本题考察的知识点是数组元素的查找,所用编程语言是java.
我利用treeSet去重从小到大排序的特点,查找数组中最大的负整数和缺失的最小正整数。set.contains函数可以判断某数是否存在,可以很方便的判断是否缺失某个正整数。我们从1开始枚举然后看缺失的最小正整数是多少
