leetcode之Find All Numbers Disappeared in an Array(448)
题目:
给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次。
找到所有在 [1, n] 范围之间没有出现在数组中的数字。
您能在不使用额外空间且时间复杂度为O(n)的情况下完成这个任务吗? 你可以假定返回的数组不算在额外空间内。
示例:
输入: [4,3,2,7,8,2,3,1]输出: [5,6]
Python代码1:
思路:注意到数组元素个数跟元素范围是一样的,所以我们可以把出现过的元素对应下标位置的数字变成负数,最后把所有正数对应的下标拿出来,就是缺失的数字。
class Solution(object):def findDisappearedNumbers(self, nums):for i in range(len(nums)):index = abs(nums[i]) - 1nums[index] = -abs(nums[index])return [i + 1 for i in range(len(nums)) if nums[i] > 0]
python代码1升级版:
class Solution(object):def findDisappearedNumbers(self, nums):for n in nums:nums[abs(n) - 1] = -abs(nums[abs(n) - 1])return [i + 1 for i, n in enumerate(nums) if n > 0]
leetcode之Find All Numbers Disappeared in an Array(448)相关推荐
- leetcode 448. Find All Numbers Disappeared in an Array | 448. 找到所有数组中消失的数字(原地,位运算)
题目 https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ 题解 遍历数组,将数组中每个数字 n 作为下标,将 ...
- LeetCode之Find All Numbers Disappeared in an Array
1.题目 Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice a ...
- 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 442. Find All Duplicates in an Array
这两道题很有意思,由于元素为1~n,因此每个元素的值-1(映射到0~n-1)就可以直接当做下标.这样将 nums 中对应下标的元素 *-1 以i表示 index+1 这个元素出现过了,能节省存储的空间 ...
- 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 ...
- [LeetCode] 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 ...
- C#LeetCode刷题之#448-找到所有数组中消失的数字(Find All Numbers Disappeared in an Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3712 访问. 给定一个范围在 1 ≤ a[i] ≤ n ( n ...
- 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 ...
- LeetCode Find All Numbers Disappeared in an Array
题意:给出长度为n的数组,数组的数为 1-n,找出没有出现的数 思路:在遍历数组时,将对应的索引的数设置为负数.第二次遍历数组时,索引所在的数为正数,说明是没有出现的 代码如下: public cla ...
- [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 ...
最新文章
- ionic2.0消息订阅监听机制
- Struts2 自定义拦截器(方法拦截器)
- python 柱状图 居中_python matplotlib模块: bar(柱状图)
- ios::app与ios::ate打开方式有什么不同??
- Sys和system用户、sysdba 和sysoper系统权限、dba角色的区别
- uva 10954——Add All
- Oracle 用户,角色,权限等
- 到天宫做客(洛谷P1178题题解,Java语言描述)
- SHA-3的获胜者:keccak - 在 3GPP TS 35.231、FIPS 202 和 SP 800-185 中标准化
- 2:url有规律的多页面爬取
- 初尝Mcafee之CEE企业版概述【01】
- 会声会影x4素材_怎么给视频打马赛克?运用会声会影2019
- AXI5 new feature: support atomic transaction
- Android 12没显示出app的微件问题
- 微信公众平台实现天气预报功能
- python读取歌词文本,并显示在图片上,制作视频
- 夫妻卖盲盒、年入16亿,揭秘泡泡玛特的暴利生意
- 苹果最早明年在Apple Watch中引入microLED显示屏
- 改进YOLOv5系列:10.最新HorNet结合YOLO应用首发! | 多种搭配,即插即用 | Backbone主干、递归门控卷积的高效高阶空间交互高效
- matplotlib只显示部分横坐标刻度,隐藏部分横坐标刻度