当前位置:网站首页>448. Find All Numbers Disappeared in an Array
448. Find All Numbers Disappeared in an Array
2022-06-22 13:17: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]:
"""
Use the array subscript to mark whether the corresponding number appears
Time complexity :O(n) Spatial complexity :O(1)
"""
# The worst time complexity here is 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
# Reference others' optimization time is n
for num in nums:
index = abs(num) - 1
if nums[index] > 0:
# Clever : In order not to lose the original data at this location , Only negative means that there has been a number in this position , The original data can still be obtained by using the absolute value each time
nums[index] = -nums[index]
result = []
for i in range(len(nums)):
if nums[i] > 0:
result.append(i + 1)
return result边栏推荐
猜你喜欢

SICF批量激活服务节点

Reconstruction practice of complex C-end project of acquisition technology

MySQL 5.7 + Navicat download and installation tutorial (with installation package)

File download vulnerability & file read vulnerability & file delete vulnerability

Set up your own website (5)

Sap-mm-migo 311 intra factory transfer inventory

JAXB元素详解

Maui uses Masa blazor component library

MySQL_数据处理之查询

CVPR 2022 | 针对场景文本检测的视觉语言模型预训练
随机推荐
Windows下MySQL 8.0.29的详细安装教程,解决找不到VCRUNTIME140_1.dll、plugin caching_sha2_password could not be loaded
Leetcode 297 match de la semaine
文件下载漏洞&文件读取漏洞&文件删除漏洞
Rf5.0 new content quick view
Alicloud disk performance analysis
Tips of setup in robotframework
重磅直播|BizDevOps:数字化转型浪潮下的技术破局之路
RobotFramework中setUp的小技巧
Wechat payment QR code generation
leetcode 1130. 叶值的最小代价生成树
In C # development, the third-party components lambdaparser, dynamicexpresso and z.expressions are used to dynamically parse / evaluate string expressions
Ffmpeg converts AMR format to MP3 format
Pycharm shell script cannot be run
redis修改密码,及启动、查看等操作
318. Maximum Product of Word Lengths
338. Counting Bits
leetcode 第 297 場周賽
RobotFramework二次开发——实时日志
338. Counting Bits
SAP 系统取消用户设置ALV全局布局