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]

本题在一维数组中找到按顺序缺失的数字 按照思维 首先最容易想到的是哈希 那样子的话 很容易得到重复的数字和缺失的数字 可是本题中不允许使用额外的空间同时 如果简单的使用哈希 也并没有应用到本题中题目的特性  本题题目中 一维数组大小为n 其中的数字也都是1~n的  其实这些数字减一即为数组的下标0~n-1

要好好利用数组中数字和数组下标的关系  在一次按照下标遍历中 既可以按当前下标中的数字为下标 操作对应的数字 标定这个数字已经出现 多次出现 标定一次就好  当然 不同的标定也可以

就例子而言:从数组的第一个下表中的数字开始 4,找到其对应下表4-1:3,  数组中下标为3的地方标定一下  表示数组中(3+1)已经出现过了       当以这样的方式标定为完整个数组时  那么出现过的数字对应的下标的位置都被标定  没出现过的即没有被标定  遍历一次就可以了

代码:vector<int> findDisappearedNumbers(vector<int>& nums) {        vector<int> res;        if (nums.size()==0)            return res;        for (int i=0; i<nums.size();++i)        {            int m=abs(nums[i])-1;            if (nums[m]>0)                nums[m] = -nums[m];        }        for (int i=0; i<nums.size();++i)        {            if (nums[i]>0)                res.push_back(i+1);        }        return res;    }
时间: 2024-08-06 07:47:26

Find All Numbers Disappeared in an Array的相关文章

Leetcode 448. Find All Numbers Disappeared in an Array

Leetcode  448. Find All Numbers Disappeared in an Array Add to List Description Submission Solutions Total Accepted: 31266 Total Submissions: 58997 Difficulty: Easy Contributors: yuhaowang001 Given an array of integers where 1 ≤ a[i] ≤ n (n = size of

leetcode之Find All Numbers Disappeared in an Array

问题来源:Find All Numbers Disappeared in an Array 很久没有刷题了,感觉大脑开始迟钝,所以决定重拾刷题的乐趣.一开始不要太难,选一些通过率高的题目做,然后就看到了这个题目.我有些吃惊,这个题我虽然知道两种解法,但本身还是有难度的,居然通过率这么高.然后就搜索相关网页,看到一个和它很接近的题目<Find All Duplicates in an Array>,然后就释然了.这两个题目有相同的题干,只是问题略微不同,解法有相似之处.估计是因为题号太接近了,会

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) runti

LeetCode-448. Find All Numbers Disappeared in an Array C#

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?

算法(2) Find All Numbers Disappeared in an Array

题目:整数数组满足1<=a[i]<=n(n是数组的长度),某些元素出现一次,某些元素出现两次,在数组a[i]中找到[1,n]区间中未出现的数字.比如输入[4,3,2,7,8,2,3,1],输出[5,6].时间复杂度要求是O(n),空间复杂度要求O(1) 思路:看许多网友说用set数据结构去做,这就违反了空间复杂度的要求,所以排序还是要本地做的.遍历数组,把这个数字放到它该待的地方:比如a[0]=4,那么我们就把4放到a[3]处,然后把a[3]处的7放到a[6]处,把a[6]处3放在a[2]处,

[Array]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?

leetcode448-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) runt

LeetCode 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) run

Leetcode 448. Find All Numbers Disappeared in an Array JAVA语言151. Reverse Words in a String

Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Update (2015-02-12): For C programmers: Try to solve it in-place in O(1) space. 题意:反转字符串中的单词,注意空格的处理!!! public