448. Find All Numbers Disappeared in an Array&&645. Set Mismatch

來源:互聯網
上載者:User

標籤:time   signed   body   result   form   duplicate   integer   data   match   

題目:

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.

 

645. Set Mismatch

The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in the set, which results in repetition of one number and loss of another number.

Given an array nums representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array.

 

思路:

448和645具有相似的思路。兩道題的共同點在於:元素的大小均為 [1,n]。448要求找出未出現的數字,645要求找出出現了兩次的數字和未出現的數字。

由於元素的下標為[0,n-1],則元素的大小減去1得到的即為某個元素的下標,因此可以利用元素大小與下標之間的關係尋找特殊的數字。

對於448,遍曆整個數組,通過元素的大小減去1得到下標。若該下標對應的元素為正,則將其乘以-1,變為負數;若該下標對應的元素為負,證明該下標之前已經出現過了一次,不作處理。通過這一次的遍曆,仍然為正的元素所對應的下標再加1即為未出現過的元素。

對於645,遍曆整個數組,通過元素的大小減去1得到下標。若該下標對應的元素為正,則將其乘以-1,變為負數;若該下標對應的元素為負,證明該下標之前已經出現過了一次,將該下標+1加入result中。通過這一次的遍曆,仍然為正的元素所對應的下標再加1即為未出現過的元素。

 

代碼:

448.

 1 class Solution { 2 public: 3     vector<int> findDisappearedNumbers(vector<int>& nums) { 4         vector<int> ans; 5         for (int i = 0; i < (signed) nums.size(); i++) { 6             int index = abs(nums[i]) - 1; 7             if (nums[index] > 0) 8                 nums[index] *= -1; 9             else10                 continue;11         }12         for (int i = 0; i < (signed) nums.size(); i++)13             if (nums[i] > 0)14                 ans.push_back(i + 1);15         return ans;16     }17 };

645.

 1 class Solution { 2 public: 3     vector<int> findErrorNums(vector<int>& nums) { 4         vector<int> ans; 5         for (int i = 0; i < (signed) nums.size(); i++) { 6             int index = abs(nums[i]) - 1; 7             if (nums[index] > 0) { 8                 nums[index] *= -1; 9             } else {10                 ans.push_back(index + 1);11             }12         }13         for (int i = 0; i < (signed) nums.size(); i++) {14             if (nums[i] > 0)15                 ans.push_back(i + 1);16         }17         return ans;18     }19 };

 

448. Find All Numbers Disappeared in an Array&&645. Set Mismatch

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.