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個(gè)數(shù),其中1~n中有些數(shù)沒(méi)出現(xiàn),求這些沒(méi)出現(xiàn)的數(shù)。
我們可以將數(shù)值和數(shù)組的標(biāo)關(guān)聯(lián),運(yùn)用負(fù)號(hào)巧妙的解決了這個(gè)問(wèn)題。確保每個(gè)出現(xiàn)過(guò)的數(shù)都變成負(fù)數(shù)
class Solution { public:vector<int> findDisappearedNumbers(vector<int>& nums) {vector<int> v;for (int i = 0; i < nums.size(); ++i) {int index = abs(nums[i]) - 1;nums[index] = -abs(nums[index]);}for (int i = 0; i < nums.size(); ++i) {if (nums[i] > 0) v.push_back(i + 1);}return v;} };?
轉(zhuǎn)載于:https://www.cnblogs.com/pk28/p/7250740.html
總結(jié)
以上是生活随笔為你收集整理的448. Find All Numbers Disappeared in an Array的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Dbvisualizer9.0.6 解决
- 下一篇: 5.5 关于数据的问题