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]
我們把所有出現過的數的位置設為負值,最後沒有出現過的位置就是正值。用三目運算子保證我們不會覆蓋未遍歷的值,僅僅將其設定為負值。
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
for(int i=0; i<nums.size(); ++i){
int m = abs(nums[i]) - 1;
nums[m] = nums[m] > 0 ? -nums[m] : nums[m];
}
vector<int> res;
for(int i=0; i<nums.size(); ++i){
if(nums[i] > 0)
res.push_back(i+1);
}
return res;
}
};
相關文章
- LeetCode 448. Find All Numbers Disappeared in an ArrayLeetCodeAPP
- Find All Numbers Disappeared in an ArrayAPP
- Leetcode 442. Find All Duplicates in an ArrayLeetCode
- #442-Find All Duplicates in an Array-陣列中重複的數字陣列
- B. Find The Array
- 421-Maximum XOR of Two Numbers in an Array
- LeetCode Patching Array All In OneLeetCode
- LeetCode 438. Find All Anagrams in a StringLeetCode
- [leetcode] 1394. Find Lucky Integer in an ArrayLeetCode
- Find Minimum in Rotated Sorted Array I & II
- [LeetCode] 3011. Find if Array Can Be SortedLeetCode
- JavaScript object array sort by string bug All In OneJavaScriptObject
- LeetCode | 153. Find Minimum in Rotated Sorted ArrayLeetCode
- Leetcode 34 Find First and Last Position of Element in Sorted ArrayLeetCodeAST
- [Javascript] Find Items from the end of the JavaScript Array using at, findLast and findLastIndexJavaScriptASTIndex
- (轉)leetcode:Find All Anagrams in a String 滑動視窗方法總結LeetCode
- All mirror URLs are not using ftp, http[s] or file. Cannot find a valid baseurl for repo: baseFTPHTTP
- Reversed Numbers
- 400多種Numbers模板 DesiGN for Numbers Templates for macMac
- Collecting Numbers II
- Codeforces - Jzzhu and Numbers
- different random numbers generatorrandom
- 448. 找到所有陣列中消失的數字陣列
- LeetCode 2 Add Two NumbersLeetCode
- 165. Compare Version Numbers
- Leetcode 165 Compare Version NumbersLeetCode
- 201-Bitwise AND of Numbers Range
- 829. Consecutive Numbers Sum
- 【Lintcode】1267. Lexicographical Numbers
- Jenkins報錯'Gradle build daemon disappeared unexpectedly'的問題解決JenkinsGradleUIAPP
- 淦448. 找到所有陣列中消失的數字陣列
- BeautifulSoup4 find_all搜尋包含指定文字內容的標籤返回空list的問題
- Array.from和 Array.of
- array
- LeetCode-2 Add Two NumbersLeetCode
- Sum of Square Numbers 平方數之和
- Self Dividing Numbers 自除數
- LeetCode 2. Add Two NumbersLeetCode
- 129-Sum Root to Leaf Numbers