448. Find All Numbers Disappeared in an Array
来源:互联网 发布:龙腾会计软件 编辑:程序博客网 时间:2024/06/04 18:46
题目
448. Find All Numbers Disappeared in an Array
解题思路
题目描述是给一串长度为n,大小在[1,n]的乱序数字,有的数字出现2次,有的数字出现1次,计算出有多少个数字没出现过。
没出现的数字的位置是被出现了2次的数字站了,我的思路是让这些乱序的数字归位,出现1次的数字正常归位,出现2次的数字多出来的一个令其等于-1,最后统计一下-1出现在哪个位置即可。
举个栗子:
[4,3,2,7,8,2,3,1]
1:第一个数字4位置错误,与第4个数字7交换位置:
7,3,2,4,8,2,3,1
2:第一个数字7位置错误,与第7个数字3交换位置:
3,3,2,4,8,2,7,1
3:第一个数字3位置错误,与第3个数字2交换位置:
2,3,3,4,8,2,7,1
4:第一个数字2位置错误,与第2个数字3交换位置:
3,2,3,4,8,2,7,1
5:第一个数字3位置错误,与第3个数字3交……欸不对这里都是3,所以出现多余的数字就设为-1,把第一个数字3设为-1后遍历下一个数字:
-1,2,3,4,8,2,7,1
6:第2,3,4个数字位置正确,到第5个数字8位置错误,与第8个数字1交换位置:
-1,2,3,4,8,2,7,1
7:第2,3,4个数字位置正确,到第5个数字8位置错误,与第8个数字1交换位置:
-1,2,3,4,1,2,7,8
8:第5个数字1位置错误,与第1个数字-1交换位置:
1,2,3,4,-1,2,7,8
9:第5个数字是-1,没有原来位置,跳过继续下一个,第6个数字2位置错误,但是原来第2个数字也是2,所以这里的2是多余的设为-1:
1,2,3,4,-1,-1,7,8
10:第7,8个数字正确,遍历完成,最后再遍历一下哪个位置是-1,得到的结果是[5,6],答案正确,收工~
具体代码
class Solution {public: void swap(vector<int>& nums, int a, int b) { int t = nums[a]; nums[a] = nums[b]; nums[b] = t; } vector<int> findDisappearedNumbers(vector<int>& nums) { if (nums.empty()) return nums; for (int i = 0; i < nums.size(); i++) { // 第i个数字位置错误&&第i个数字不是多余的数字&&第i个数字不是-1 while (nums[i] - 1 != i && nums[i] != nums[nums[i] - 1] && nums[i] != -1) { swap(nums, i, nums[i] - 1); // 与正确位置的数字进行交换 } // 如果位置错误并且是多余是数字,则设为-1 if (nums[i] == nums[nums[i] - 1] && i != nums[i] - 1) { nums[i] = -1; } } // 数一下有多少个数字 vector<int> tmp; for (int i = 0; i < nums.size(); i++) if (nums[i] == -1) tmp.push_back(i + 1); return tmp; }};
PS
去看了眼leetcode的discussion,有更简单的方法:
遍历,假设数字i出现过,则把第i个位置的nums[i-1]的数字变成负数,最后遍历哪个位置k的数字不是负数,则说明k没有出现在这串数字中。
class Solution {public: vector<int> findDisappearedNumbers(vector<int>& nums) { int len = nums.size(); for(int i=0; i<len; i++) { int m = abs(nums[i])-1; // index start from 0 nums[m] = nums[m]>0 ? -nums[m] : nums[m]; } vector<int> res; for(int i = 0; i<len; i++) { if(nums[i] > 0) res.push_back(i+1); } return res; }};
- 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
- Find All Numbers Disappeared in an Array
- 448. Find All Numbers Disappeared in an Array
- 448. Find All Numbers Disappeared in an Array
- 448. Find All Numbers Disappeared in an Array
- leetcode:448. Find All Numbers Disappeared in an Array
- JavaSE_8系列博客——Java语言的特性(六)--泛型(4)--Java中泛型实现的原理
- Android主题切换(Theme)实现日夜间功能
- 天天写业务代码的程序员,怎么成为技术大牛,开始写技术代码?
- POJ 2396:Budget (有流量上下界的网络流)
- Mysql系列——数据库设计(4)——实体表之间的关系
- 448. Find All Numbers Disappeared in an Array
- 用了下itchat接口。
- java中jdk api等概念的解释
- Oracle12c 安装教程
- 从零搭建React全家桶框架教程
- markdown对图片进行居中、缩放
- Retrofit 源码解析
- python总结(五):__get__、__getattr__、__getitem__、__getattribute__之间的差异与联系
- 常见行内元素和块级元素