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相关推荐
- LeetCode 448. Find All Numbers Disappeared in an Array 442. Find All Duplicates in an Array
这两道题很有意思,由于元素为1~n,因此每个元素的值-1(映射到0~n-1)就可以直接当做下标.这样将 nums 中对应下标的元素 *-1 以i表示 index+1 这个元素出现过了,能节省存储的空间 ...
- Leetcode 448. Find All Numbers Disappeared in an Array
Leetcode 448. Find All Numbers Disappeared in an Array Add to List Description Submission Solutions ...
- leetcode 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 ot ...
- 448. Find All Numbers Disappeared in an Array 寻找有界数组[1,n]中的缺失数
[抄题]: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice ...
- [swift] LeetCode 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 ot ...
- 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 ot ...
- leetcode 448. Find All Numbers Disappeared in an Array | 448. 找到所有数组中消失的数字(原地,位运算)
题目 https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ 题解 遍历数组,将数组中每个数字 n 作为下标,将 ...
- 448. Find All Numbers Disappeared in an Array645. Set Mismatch
题目: 448. Find All Numbers Disappeared in an Array Given an array of integers where 1 ≤ a[i] ≤ n (n = ...
- Find All Numbers Disappeared in an Array
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...
最新文章
- ThickBox 3.1参数详解(转)
- Hyper-v 3.0虚拟化平台群集共享磁盘无法failover的故障
- 什么是安全测试?哪些阶段需要安全测试?
- 计算机网络的定义分类性能指标,第1章 计算机网络基础
- 爬取某东600多本书籍,用数据帮你分析哪些Python书籍值得选择(上)
- 制作linux usb安裝,如何在Linux上安装和使用Etcher来制作Linux临场USB
- webkit内核浏览器的CSS写法
- 2018-04-26java实习面试记录
- centos rpm安装mysql5.6_CentOS 7下使用RPM安装MySQL 5.6
- DAS Tool 介绍
- springboot配置微信公众号获取openid
- 计算机制图基本知识6,机械制图及计算机绘图教案.doc
- java程序员电脑内存配置_学习JAVA对电脑配置有要求吗
- RAID磁盘阵列数据恢复
- 全球及中国智能手机过滤器行业销售动态及投资盈利预测报告(2022-2027)
- PhoneApplicationFrame以及设置Obscured/Unobscured的event handler
- 【统计学笔记】如何判断变量间相关关系,并建立一元线性回归模型?
- ¥1-3 SWUST oj 942: 逆置顺序表
- c语言字符串输入的陷阱
- 网络资源下载方式:http/https、ftp/sftp、BT种子、磁力下载、ed2k下载等的区别