[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? 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的数组,遍历所有元素,找出没有出现过的元素。基本思想是我们将元素值作为下标遍历输入数组并标记为负值num[i] = -num[i]。
vector<int> finddisappear(vector<int>& nums){
vector<int>tmp;
size_t n = nums.size();
for(size_t i = 0; i < n; i++){
int val = abs(nums[i]) - 1;//nums[i]是所有元素值,val是下标,将元素值出现的下标的元素标记为负值,减1的意思是下标比个数少1
if(nums[val] > 0)
nums[val] = -nums[val];
}

for(size_t i = 0; i < n; i++){
if(nums[i] > 0)
tmp.push_back(i + 1);
}
return tmp;
}
比如4个元素的数组里出现了2,3,那么下标为2,3的元素标为负值,剩下的1,4没有出现,那么仍为正值,找到正值的下标也就是结果了。
时间: 2024-10-11 19:48:38

[Array]448. 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

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 解题报告

题目要求 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 

448. Find All Numbers Disappeared in an Array Add to List

题目描述 题目分析 有个[1,n]的条件要充分利用起来. 题目代码 public class Solution { public List<Integer> findDisappearedNumbers(int[] nums) { List<Integer> list=new ArrayList<Integer>(); if(nums==null || nums.length==0) return list; if(nums.length == 1 ){ list.ad

leetcode之Find All Numbers Disappeared in an Array

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

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]处,