当前位置:网站首页>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边栏推荐
- Sap-abap- how to find a table and what related tables the fields have
- 智龄语音+php
- 这不会又是一个Go的BUG吧?
- Ffmpeg converts AMR format to MP3 format
- Precautions for upgrading php8 of diyun CMS
- AcWing第54场周赛
- 基于can总线的A2L文件解析(1)
- 关于 GIN 的路由树
- MySQL_ Create and manage tables
- Tianyi cloud explores a new idea of cloud native and edge computing integration
猜你喜欢

老王说系列第六期:PHP程序员要建立自己的自信心

MySQL_ Query of data processing

JAXB element details

关于 GIN 的路由树

轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷

2022-6-21os review group linking method

A2L file analysis based on CAN bus (1)

Shutter & flame - tankcombat game console development (I)

Lao Wang said that the sixth issue of the series: PHP programmers should build their own self-confidence

动作捕捉系统用于地下隧道移动机器人定位与建图
随机推荐
AcWing第53场周赛
Fast establishment of tensorflow2 model optimization environment
SAP-ABAP-BAPI_GOODSMVT_CREATE创建物料凭证bapi的各种情况如何赋值
Sequoiadb distributed database may 2022 issue
Is the dynamic table of Flink created in this way? I use the flick CDC to read MySQL data, write the flick dynamic table, and send
关于 GIN 的路由树
Maui uses Masa blazor component library
[Jishu reading] arm64 learning experience sharing by running bar community
[QT] QT get standard system path
记录阿里云ECS实例重启之后无法登录解决方法(亲身实践)
Heavyweight live | bizdevops: the way to break the technology situation under the tide of digital transformation
信创之下:国产数据库群星闪耀时
老王说系列第六期:PHP程序员要建立自己的自信心
MySQL_数据处理之增删改
leetcode 第 297 场周赛
BSS应用程序云原生部署的8大挑战
SAP-abap-OLE核心代码
企业官网如何提高客户转化率
leetcode 968.监控二叉树
Sap-abap-se14 how to recover lost data