首页 > 移动平台 > 详细

448. 数组中缺少的元素 Find All Numbers Disappeared in an Array

时间:2017-01-10 23:42:37      阅读:640      评论:0      收藏:0      [点我收藏+]

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 IList<int> FindDisappearedNumbers(int[] nums) {
  3. List<int> list = new List<int>();
  4. int length = nums.Length;
  5. int[] arr = new int[length];
  6. for (int i = 0; i < length; i++) {
  7. arr[nums[i]-1] += 1;
  8. }
  9. for (int i = 0; i < length; i++){
  10. if (arr[i] == 0) {
  11. list.Add(i + 1);
  12. }
  13. }
  14. return list;
  15. }
  16. }





448. 数组中缺少的元素 Find All Numbers Disappeared in an Array

原文:http://www.cnblogs.com/xiejunzhao/p/4152703f5901f110c0dd4f5bb641527b.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!