zoukankan      html  css  js  c++  java
  • Find All Numbers Disappeared in an Array

    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]
    
    
    
    solution1:
    方法一
    把list转成set 直接就去重了,然后从1到n,挨个判断是否在set里
    2,解题思路:
    正负号标记法
    遍历数组nums,记当前元素为n,令nums[abs(n) - 1] = -abs(nums[abs(n) - 1])
    再次遍历nums,将正数对应的下标+1返回即为答案,因为正数对应的元素没有被上一步骤标记过。
    
    class Solution{
    public:
        vector<int> findDisappearNumbers(vector<int>& nums){
            int len = nums.size();
            for(int i = 0; i < len; i++){
                int m = abs(nums[i])-1;//index start from 0
                nums[m] = nums[m] > 0 ? -nums[m]: nums[m];
            }
    
            vector<int> res;
            for(int i = 0; i < len; i++){
                if(nums[i] > 0)
                    res.push_back(i+1);
            }
            return res;
        }
    };
    
    方法3:
    第二种方法是将nums[i]置换到其对应的位置nums[nums[i]-1]上去,比如对于没有缺失项的正确的顺序应该是[1, 2, 3, 4, 5, 6, 7, 8],而我们现在却是[4,3,2,7,8,2,3,1],我们需要把数字移动到正确的位置上去,比如第一个4就应该和7先交换个位置,以此类推,最后得到的顺序应该是[1, 2, 3, 4, 3, 2, 7, 8],我们最后在对应位置检验,如果nums[i]和i+1不等,那么我们将i+1存入结果res中即可,参见代码如下:
    
    class Solution{
    public:
        vector<int> findDisappearedNumbers(vector<int>& nums){
            vector<int> res;
            for(int i = 0; i < nums.size(); i++){
                if(nums[i] != nums[nums[i]-1]){
                    swap(nums[i],nums[nums[i]-1]);
                    --i;
                }
            }
    
            for(int i = 0; i < nums.size(); i++){
                if(nums[i] != i+1){
                    res.push_back(i+1);
                }
            }
            return res;
        }
    };
    
    方法4:下面这种方法是在nums[nums[i]-1]位置累加数组长度n,注意nums[i]-1有可能越界,所以我们需要对n取余
    ,最后要找出缺失的数只需要看nums[i]的值是否小于等于n即可,最后遍历完nums[i]数组为[12, 19, 18, 15, 8, 2, 11, 9],
    我们发现有两个数字8和2小于等于n,那么就可以通过i+1来得到正确的结果5和6了,参见代码如下:
    //此解法待进一步理解
    class Solution{
    public:
        vector<int> findDisappearedNumbers(vector<int>& nums){
            vector<int> res;
            int n = nums.size();
            for(int i = 0; i < n; ++i){
                nums[(nums[i]-1)%n] += n;
            }
    
            for(int i = 0; i < n; ++i){
                if(nums[i] <= n){
                    res.push_back(i+1);
                }
            }
        }
    };
    
    
    
    
    
    references: 
    https://blog.csdn.net/qjh5606/article/details/81355329?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
    http://bookshadow.com/weblog/2016/11/01/leetcode-find-all-numbers-disappeared-in-an-array/
    https://blog.csdn.net/sinat_31790817/article/details/78695249
    https://www.cnblogs.com/grandyang/p/6222149.html
    

      

    怕什么真理无穷,进一寸有一寸的欢喜。---胡适
  • 相关阅读:
    matrix_2015_1 138
    2014ACM/ICPC亚洲区广州站 北大命题
    无向图的联通分量
    5.1 基础题目选讲
    URAL
    Codeforces Round #274 (Div. 2)
    后缀数组
    poj 1661 help jimmy dp
    hdu 1676 Full Tank? 限制最短路 dp 蛮有技巧的~
    hdu 1023 Train Problem II 双向广搜
  • 原文地址:https://www.cnblogs.com/hujianglang/p/12467399.html
Copyright © 2011-2022 走看看