Find All Numbers Disappeared in an Array(leetcode)
2017-12-03 19:46
435 查看
Find All Numbers Disappeared in an Array
Find All Numbers Disappeared in an Array题目
解决
bool数组
不利用额外的空间
题目
leetcode题目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]
解决
1. bool数组
利用bool数组记录数是否出现。若bool[i]值为
false,证明该数
i不存在;若
bool[i]值为
true,证明该数
i存在。
时间复杂度为
O(n)(因为只需遍历数组和bool数组各一遍)
空间复杂度为
O(n)(因为需要开一个大小为
n的bool数组)。
class Solution { public: vector<int> findDisappearedNumbers(vector<int>& nums) { vector<int> result; int num = nums.size(); bool exist[num + 1] = {false}; // 初始化为都不存在 for (int i = 0; i < num; i++) { exist[nums[i]] = true; } for (int i = 1; i < num + 1; i++) { if (!exist[i]) result.push_back(i); } return result; } };
2. 不利用额外的空间
这种方法是不利用额外的空间,原有的空间进行判断。将出现的数作为下标,使该下标对应的数变为负数。若
abs(array[i]) == temp(预防之前的操作将
array[i]变为负数):
若
array[temp - 1] > 0,则表明此时
temp是第一次出现,将
array[temp - 1]置为相反数。
若
array[temp - 1] < 0,则表明此时
temp在之前已经出现过了,不作任何处理。
PS:注意的是,数组下标是从
0开始的。
时间复杂度为
O(n)(因为需要遍历数组一遍)
空间复杂度为
O(1)(没有用额外的空间)。
class Solution { public: vector<int> findDisappearedNumbers(vector<int>& nums) { vector<int> result; int num = nums.size(); for (int i = 0; i < num; i++) { int temp = abs(nums[i]); if (nums[temp - 1] > 0) { nums[temp - 1] = 0 - nums[temp - 1]; } } for (int i = 0; i < num; i++) { if (nums[i] > 0) { result.push_back(i + 1); } } return result; } };
相关文章推荐
- leetcode448~Find All Numbers Disappeared in an Array
- 【LeetCode-448】 Find All Numbers Disappeared in an Array(C++)
- [Leetcode,python] 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
- [leetcode 448]Find All Numbers Disappeared in an Array
- [Leetcode,python] Find All Numbers Disappeared in an Array 寻找数组中消失的数字
- Leetcode 448 Find All Numbers Disappeared in an Array
- LeetCode之Find All Numbers Disappeared in an Array
- [LeetCode]Find All Numbers Disappeared in an Array
- [LeetCode] Find All Numbers Disappeared in an Array
- [Leetcode,python] Find All Numbers Disappeared in an Array 寻找数组中消失的数字
- leetcode 448 Find All Numbers Disappeared in an Array
- leetcode: Find All Numbers Disappeared in an Array
- LeetCode--Find All Numbers Disappeared in an Array(找到数组中未出现的数字)
- Leetcode: Find All Numbers Disappeared in an Array
- [Leetcode,python] Find All Numbers Disappeared in an Array 寻找数组中消失的数字
- LeetCode No.448 Find All Numbers Disappeared in an Array
- [Leetcode,python] Find All Numbers Disappeared in an Array 寻找数组中消失的数字
- leetcode_448 Find All Numbers Disappeared in an Array