缺失的第一个正数
给你一个未排序的整数数组 nums ,请你找出其中没有出现的最小的正整数。
请你实现时间复杂度为O(n)并且只使用常数级别额外空间的解决方案。
示例 1:
输入:nums = [1,2,0]
输出:3
解释:范围 [1,2] 中的数字都在数组中。示例 2:
输入:nums = [3,4,-1,1]
输出:2
解释:1 在数组中,但 2 没有。示例 3:
输入:nums = [7,8,9,11,12]
输出:1
解释:最小的正数 1 没有出现。
提示:
1 <= nums.length <= 105-231 <= nums[i] <= 231 - 1
解题思路
遍历查找
排序数组:先对数组进行排序(
Arrays.sort(nums)),使得所有正整数按升序排列,便于后续处理。遍历查找缺失值:
初始化
exceptInt = 1,表示当前期望的最小正整数。遍历排序后的数组:
跳过非正整数(
nums[i] < 1)。跳过重复值(
nums[i] == nums[i-1])。检查是否匹配期望值:
如果
nums[i] != exceptInt,说明exceptInt缺失,直接返回。如果匹配,则更新
exceptInt = nums[i] + 1,继续检查下一个数。
如果遍历完仍未找到缺失值,则返回
exceptInt(即数组最大值 + 1)。
class Solution {
public int firstMissingPositive(int[] nums) {
Arrays.sort(nums);
int exceptInt = 1;
for (int i = 0; i < nums.length; i++) {
if (nums[i] < 1)
continue;
if(i>0 && nums[i] == nums[i-1]){
continue;
}
if (nums[i] != exceptInt) {
return exceptInt;
} else {
exceptInt = nums[i] + 1;
continue;
}
}
return exceptInt;
}
}三数之和
给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != j、i != k 且 j != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请你返回所有和为 0 且不重复的三元组。
注意:答案中不可以包含重复的三元组。
示例 1:
输入:nums = [-1,0,1,2,-1,-4]
输出:[[-1,-1,2],[-1,0,1]]
解释:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0 。
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0 。
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0 。
不同的三元组是 [-1,0,1] 和 [-1,-1,2] 。
注意,输出的顺序和三元组的顺序并不重要。
示例 2:
输入:nums = [0,1,1]
输出:[]
解释:唯一可能的三元组和不为 0 。
示例 3:
输入:nums = [0,0,0]
输出:[[0,0,0]]
解释:唯一可能的三元组和为 0 。解题思路
排序+三重循环
三重循环是最容易想到的方法,当然也是效率比较低的做法。
排序数组:排序后,相同的数字会相邻,便于后续跳过重复的组合。
三重循环遍历
检查三数之和是否为 0:如果
nums[x] + nums[y] + nums[z] == 0,则记录这个三元组。
尝试跳过重复值
尝试在 y 和 z 的循环中跳过重复值
if (y == x + 1 || nums[y] != nums[y - 1]) {
// ...
}
if (z == y + 1 || nums[z] != nums[z - 1]) {
// ...
}完整代码
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
doThreeSum(i, nums, result);
}
return result;
}
public void doThreeSum(int startIdx, int[] nums, List<List<Integer>> result) {
int x = startIdx;
int y = x + 1;
int z = y + 1;
while (x < nums.length - 2) {
while (y < nums.length - 1) {
if (y == x + 1 || nums[y] != nums[y - 1]) {
while (z < nums.length) {
if (z == y + 1 || nums[z] != nums[z - 1]) {
if (nums[x] + nums[y] + nums[z] == 0) {
result.add(Arrays.asList(nums[x], nums[y], nums[z]));
}
}
z++;
}
}
y++;
}
x++;
}
}排序+双指针
跟之前一篇 盛最多水的容器 类似,有多重循环的要想办法优化成单次循环,然后左右指针都要根据情况向中间逼近。具体思路和代码如下:
排序数组(便于双指针法)。
固定
nums[x],并用双指针left和right查找剩余两数。去重:
跳过重复的
x(外层循环)。跳过重复的
left和right(内层循环)。
调整指针:
sum > 0→right--sum < 0→left++sum == 0→ 记录结果,并移动指针继续查找。
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
if(i == 0 || nums[i] != nums[i-1]){
doThreeSum(i, nums, result);
}
}
return result;
}
public void doThreeSum(int startIdx, int[] nums, List<List<Integer>> result) {
int x = startIdx;
int left = x + 1;
int right = nums.length - 1;
while (left < right) {
if (nums[x] + nums[left] + nums[right] > 0) {
right--;
while (left < right && nums[right + 1] == nums[right]) {
right--;
}
} else if (left < right && nums[x] + nums[left] + nums[right] < 0) {
left++;
while (left < right && nums[left - 1] == nums[left]) {
left++;
}
} else {
result.add(Arrays.asList(nums[x], nums[left], nums[right]));
left++;
right--;
while (left < right && nums[left - 1] == nums[left] && nums[right + 1] == nums[right]) {
left++;
right--;
}
}
}
}
}