448. Find All Numbers Disappeared in an Array && 442. Find All Duplicates in an Array

给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 有些元素时重复出现的。找出重复出现的元素或者找出缺失的元素,这其实是同一个问题,只是换个说法而已。所以把这两题放到一起整理。

448. Find All Numbers Disappeared in an Array

1. 题目
448. Find All Numbers Disappeared in an Array

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements of [1, n] inclusive that do not appear in this array.
Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
Example:
Input:
[4,3,2,7,8,2,3,1]
Output:
[5,6]

2. 题目分析
给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次。
找到所有在 [1, n] 范围之间没有出现在数组中的数字。
您能在不使用额外空间且时间复杂度为O(n)的情况下完成这个任务吗? 你可以假定返回的数组不算在额外空间内。

3. 解题思路
因为要求时间复杂度和空间复杂度都为O(n),所以不能借助hashmap,也不能进行先排序,也不能通过统计元素出现的次数,只能在原数组中做文章。
因为所有元素都满足条件1 ≤ a[i] ≤ n (n为数组长度),最大的元素值不超过数组长度,所以可以考虑重排数组,将每个元素排在相应的位置上,例如若元素为2,则放在数组的第二个位置上,即索引为1处。比较i+1和nums[i]的值是否相等,若不相等输出i+1。

4. 代码实现(java)

package com.algorithm.leetcode.array;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 凌 on 2019/2/19.
 * 描述:448. 找到所有数组中消失的数字
 */
public class FindDisappearedNumbers {
    public List<Integer> findDisappearedNumbers(int[] nums) {
        if (nums == null || nums.length == 0){
            return new ArrayList<>();
        }
        List<Integer> result = new ArrayList<>();
        int index = 0;
        int temp;
        while (index < nums.length){
            //注意,在数组下标中,要先减一,如nums[index]-1才表示第index个数
            if (nums[nums[index]-1] != nums[index]){
                temp = nums[nums[index]-1];
                nums[nums[index]-1] = nums[index];
                nums[index] = temp;
            }else{
                index++;
            }

        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != i+1){
                result.add(i+1);
            }
        }
        return result;
    }
}

442. Find All Duplicates in an Array

1. 题目
442. Find All Duplicates in an Array

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements that appear twice in this array.
Could you do it without extra space and in O(n) runtime?
Example:
Input:
[4,3,2,7,8,2,3,1]
Output:
[2,3]

2. 题目分析
给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次。
找到所有出现两次的元素。
你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗?

3. 解题思路
同上题是一样的思路,先重排数组,然后判断元素是否跟下标相同,如果不同,则说明是重复的元素。

4. 代码实现(java)

package com.algorithm.leetcode.array;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 凌 on 2019/2/19.
 * 描述:442. 数组中重复的数据
 */
public class FindDuplicates {
    public List<Integer> findDuplicates(int[] nums) {
        if (nums == null || nums.length == 0){
            return new ArrayList<>();
        }
        List<Integer> result = new ArrayList<>();
        int index = 0;
        int temp;
        while (index < nums.length){
            //注意,在数组下标中,要先减一,如nums[index]-1才表示第index个数
            if (nums[nums[index]-1] != nums[index]){
                temp = nums[nums[index]-1];
                nums[nums[index]-1] = nums[index];
                nums[index] = temp;
            }else{
                index++;
            }

        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != i+1){
                result.add(nums[i]);
            }
        }
        return result;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值