• 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]

    解题思路:

    1. class Solution {  
    2. public:  
    3.     vector<int> findDisappearedNumbers(vector<int>& nums) {  
    4.           
    5.         unordered_map<int,int> temp;  
    6.         vector<int> ret;  
    7.         for(int i=0;i<nums.size();i++)  
    8.             temp[nums[i]]++;  
    9.           
    10.         for(int i=1;i<=nums.size();i++)  
    11.             if(temp.count(i) == 0)  
    12.                 ret.push_back(i);  
    13.           
    14.         return ret;  
    15.     }  
    16. };  
  • 相关阅读:
    Largest Rectangle in Histogram
    Valid Sudoku
    Set Matrix Zeroes
    Unique Paths
    Binary Tree Level Order Traversal II
    Binary Tree Level Order Traversal
    Path Sum II
    Path Sum
    Validate Binary Search Tree
    新手程序员 e
  • 原文地址:https://www.cnblogs.com/liangyc/p/8793982.html
Copyright © 2020-2023  润新知