打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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,问1-n里哪些数没出现在数组里。
on时间和o1空间,还蛮费脑力的这个题。
把值作为index,然后去把a[index - 1] = -abs(a[index - 1]),最后判断下下标对应的值不是负数就行语言方法
90615h17n
L8V0k樱心美「私照曝光」不可不阅「精品图集」
15232008.02.04 23-55-43
class Solution(object): def findDisappearedNumbers(self, nums): """ :type nums: List[int] :rtype: List[int] """ for value in nums: value = abs(value) if nums[value - 1] > 0: nums[value - 1] = -nums[value - 1] ans = [] for i in range(0, len(nums), 1): if nums[i] > 0: ans.append(i + 1) return ans
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
LeetCode之Find All Numbers Disappeared in an Array
448. Find All Numbers Disappeared in an Array
448. Find All Numbers Disappeared in an Array ----------------java
CakePHP 获取数据find() list 和 neighbors
怎么能同时给ComboBox1赋值并执行
两种找出SAP UI Code List配置位置的小技巧
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服