448. Find All Numbers Disappeared in an Array 难度:easy
来源:互联网 发布:大神小的知错了txt下载 编辑:程序博客网 时间:2024/06/05 20:55
题目:
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) { vector<int> res; for(int i = 0;i < nums.size();i++) { if(nums[i] != i + 1) { int j = nums[i]; while(nums[j - 1] != j) { int temp = nums[j - 1]; nums[j - 1] = j; j = temp; } } } for(int i = 0;i < nums.size();i++) if(i + 1 != nums[i]) res.push_back(i + 1); return res; }};
0 0
- 448. Find All Numbers Disappeared in an Array 难度:easy
- LeetCode [448. Find All Numbers Disappeared in an Array] 难度[easy]
- LeetCode 448. Find All Numbers Disappeared in an Array (Easy)
- 【Leetcode-easy-448】Find All Numbers Disappeared in an Array
- 【leetcode】【Easy】【448. Find All Numbers Disappeared in an Array】【正负位标记】【Array】
- LeetCode-Array-448. Find All Numbers Disappeared in an Array
- [LC][Array] 448. Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Redis应用学习总结(二)-sharejedis源码分析
- Md5加密
- PAT乙级1041
- C++实现txt文件传输 上
- 排序算法(4):基数排序
- 448. Find All Numbers Disappeared in an Array 难度:easy
- 【Unity3D入门教程】鼠标和键盘输入与控制
- 二叉树前中后序遍历相互求法
- 在 Linux 虚拟机中手动安装或升级 VMware Tools
- C++笔记之函数新特性
- spring的restTemplate注意点
- java虚拟机中的对象
- android调试工具--dumpsys
- HDU-1253 胜利大逃亡(简单三维广搜BFS)