题解 | #缺失的第一个正整数#
缺失的第一个正整数
https://www.nowcoder.com/practice/50ec6a5b0e4e45348544348278cdcee5
最小正整数是1,先把无序数组排序,再定义变量为1,对排序后的数组进行遍历,相等说明存在则加1,遍历完成后temp值即为缺少的最小正整数
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @return int整型
*/
public int minNumberDisappeared (int[] nums) {
// write code here
Arrays.sort(nums);
int temp = 1;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == temp) {
temp = nums[i] + 1;
}
}
return temp;
}
}
腾讯云智研发成长空间 5050人发布