当前位置:网站首页>448. Find All Numbers Disappeared in an Array
448. Find All Numbers Disappeared in an Array
2022-06-22 12:26:00 【Sterben_Da】
448. Find All Numbers Disappeared in an Array
Easy
6685390Add to ListShare
Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.
Example 1:
Input: nums = [4,3,2,7,8,2,3,1] Output: [5,6]
Example 2:
Input: nums = [1,1] Output: [2]
Constraints:
n == nums.length1 <= n <= 1051 <= nums[i] <= n
Follow up: Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
"""
利用数组下标标记对应数字是否出现
时间复杂度:O(n) 空间复杂度:O(1)
"""
# 这里时间复杂度最坏为2n
# for i in range(len(nums)):
# num = nums[i]
# while num > 0 and nums[num - 1] != 0:
# indexNum = nums[num - 1]
# nums[num - 1] = 0
# num = indexNum
# result = []
# for i in range(len(nums)):
# if nums[i] != 0:
# result.append(i + 1)
# return result
# 参考别人优化时间为n
for num in nums:
index = abs(num) - 1
if nums[index] > 0:
# 取巧:为了不丢失该位置上的原数据,只取反代表该位置出现过数字,每次取值的时候用绝对值仍能获取原数据
nums[index] = -nums[index]
result = []
for i in range(len(nums)):
if nums[i] > 0:
result.append(i + 1)
return result边栏推荐
- 老王说系列第六期:PHP程序员要建立自己的自信心
- universaldependencies依存关系标签解释
- Jushan database won two honors of China's information innovation industry in 2022 by AI media consulting
- 助力金融信息化创新,巨杉数据库近期持续中标50余家金融客户
- Sap-abap- how to call an external interface by webapi
- SAP development keys application SSCR keys application
- SAP fi financial statement version setting
- ffmpeg将amr格式转成mp3格式
- SAP system license viewing application and import
- Isn't this another go bug?
猜你喜欢

天翼云探索云原生、边缘计算融合新思路

关于 GIN 的路由树

Final of the 11th Blue Bridge Cup embedded design and development project

CVPR 2022 | 针对场景文本检测的视觉语言模型预训练

动作捕捉系统用于地下隧道移动机器人定位与建图

AcWing 241 楼兰图腾(树状数组详解)

重磅直播|BizDevOps:数字化转型浪潮下的技术破局之路

SAP 系统License查看申请及导入

Tis tutorial 02 model

CVPR 2022 | visual language model pre training for scene text detection
随机推荐
SiCf batch activation service node
通过 postgis 制作 按照米制的矩形边框
Redis
leetcode 11. 盛最多水的容器
SAP system cancels user setting ALV global layout
剑指 Offer II 114. 外星文字典
Wisdom age voice +php
AcWing第55场周赛
2022-6-21os review group linking method
SAP-abap-OLE核心代码
Final of the 13th Blue Bridge Cup embedded design and development project
The Chinese display of SAP client is garbled
gradle笔记
docker安装postgresql
Fluentd is easy to get started. Combined with the rainbow plug-in market, log collection is faster
Tis tutorial 04 client
Detailed explanation of rules and ideas for advance sale of deposit
Wechat payment QR code generation
leetcode 854. 相似度为 K 的字符串
CVPR 2022 | visual language model pre training for scene text detection