zoukankan      html  css  js  c++  java
  • Leetcode: 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 public class Solution {
     2     public List<Integer> findDisappearedNumbers(int[] A) {
     3         List<Integer> res = new ArrayList<Integer>();
     4         for (int i=0; i<A.length; i++) {
     5             if (A[i]!=i+1 && A[i]!=A[A[i]-1]) {
     6                 int temp = A[A[i]-1];
     7                 A[A[i]-1] = A[i];
     8                 A[i] = temp;
     9                 i--;
    10             }
    11         }
    12         for (int i=0; i<A.length; i++) {
    13             if (A[i] != i+1) res.add(i+1);
    14         }
    15         return res;
    16     }
    17 }
  • 相关阅读:
    CF375D Tree and Queries
    进制转换
    贪心问题
    next_permutation函数
    C++ STL
    一些排序总结
    KMP算法
    围圈报数
    车辆调度—模拟栈的操作
    搜索题
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6169396.html
Copyright © 2011-2022 走看看