448. Find All Numbers Disappeared 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) 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中有些數沒出現,求這些沒出現的數。
我們可以將數值和數組的標關聯,運用負號巧妙的解決了這個問題。確保每個出現過的數都變成負數
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;} };?
轉載于:https://www.cnblogs.com/pk28/p/7250740.html
總結
以上是生活随笔為你收集整理的448. Find All Numbers Disappeared in an Array的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Dbvisualizer9.0.6 解决
- 下一篇: 5.5 关于数据的问题