leetcode 数组 (python)
1.题目描述
给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次。
找到所有在 [1, n] 范围之间没有出现在数组中的数字。
您能在不使用额外空间且时间复杂度为O(n)的情况下完成这个任务吗? 你可以假定返回的数组不算在额外空间内。
示例:
输入:
  [4,3,2,7,8,2,3,1]
输出:
  [5,6]
2. 思路
题目中给定n个元素的数组其元素范围在1~n之间,可以考虑数组元素值和其下标的关系。比较元素nums[i]和nums[nums[i]-1],如果不等,则把它俩交换。把元素值与下标对应上。最后比较i+1和nums[i]的值。如果不等,则输出i+1.
3.代码
class Solution(object):
def findDisappearedNumbers(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
i =
while i < len(nums):
if nums[i] != nums[nums[i] - ]:
nums[nums[i]-],nums[i] = nums[i],nums[nums[i]-]
else:
i +=
res = []
for i in range(len(nums)):
if i + != nums[i]:
res.append(i+)
return res
类似的题目还有:
leetcode 287. 寻找重复数
1. 题目描述
给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数。假设只有一个重复的整数,找出这个重复的数。
示例 1:
输入: [1,3,4,2,2]
输出: 2
示例 2:
输入: [3,1,3,4,2]
输出: 3
说明:
不能更改原数组(假设数组是只读的)。
只能使用额外的 O(1) 的空间。
时间复杂度小于 O(n2) 。
数组中只有一个重复的数字,但它可能不止重复出现一次。
2. 思路
和上一题一样,找出下标和元素的对应关系,元素和下标不等的即为重复的元素
3.代码
class Solution(object):
def findDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
i =
while i<len(nums):
if nums[i]!=nums[nums[i]-]:
nums[nums[i] - ], nums[i] = nums[i], nums[nums[i] - ]
else:
i = i+ for i in range(len(nums)):
if i+ != nums[i]:
return nums[i]
leetcode 41. 缺失的第一个正数
1. 题目描述
给定一个未排序的整数数组,找出其中没有出现的最小的正整数。
示例 1:
输入: [1,2,0]
输出: 3
示例 2:
输入: [3,4,-1,1]
输出: 2
示例 3:
输入: [7,8,9,11,12]
输出: 1
2. 思路描述
参考 https://blog.csdn.net/weixin_40449300/article/details/82532996,
3.代码
class Solution(object):
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
for i in range(len(nums)):
while <= nums[i] <= len(nums) and nums[i] != nums[nums[i] - ]:
nums[nums[i] - ], nums[i] = nums[i], nums[nums[i] - ] for i, x in enumerate(nums):
if x != i + :
return i + return len(nums) +
leetcode 268. 缺失数字
1.题目描述
给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。
示例 1:
输入: [3,0,1]
输出: 2
示例 2:
输入: [9,6,4,2,3,5,7,0,1]
输出: 8
2. 思路
直接计算序列的和,然后计算0~n的和,计算这两者的差值即为缺失的数字
3.代码
class Solution:
def missingNumber(self, nums: List[int]) -> int:
N = len(nums)
total_sum = N*(N+)/
sum =
for i in range(N):
sum+=nums[i] return int(total_sum - sum)
leetcode 数组 (python)的更多相关文章
- LeetCode专题-Python实现之第27题:Remove Element
		
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
 - LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
		
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
 - LeetCode专题-Python实现之第14题:Longest Common Prefix
		
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
 - LeetCode专题-Python实现之第1题:Two Sum
		
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
 - LeetCode数组中重复的数字
		
LeetCode 数组中重复的数字 题目描述 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. ...
 - LeetCode专题-Python实现之第28题: Implement strStr()
		
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
 - LeetCode专题-Python实现之第21题:Merge Two Sorted Lists
		
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
 - LeetCode专题-Python实现之第20题:Valid Parentheses
		
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
 - LeetCode专题-Python实现之第9题:Palindrome Number
		
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
 
随机推荐
- python3:tuple元组
			
https://www.runoob.com/python3/python3-tuple.html 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. Py ...
 - Git入门指南九:远程仓库的使用【转】
			
转自:http://blog.csdn.net/wirelessqa/article/details/20152651 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 十三 ...
 - 设备树里面#address-cells 、#size-cells、reg三者的关系
			
栗子1: cpus { #address-cells = <>; #size-cells = <>; cpu@ { compatible = "arm,cortex- ...
 - 机器学习聚类算法之DBSCAN
			
一.概念 DBSCAN是一种基于密度的聚类算法,DBSCAN需要两个参数,一个是以P为中心的邻域半径:另一个是以P为中心的邻域内的最低门限点的数量,即密度. 优点: 1.不需要提前设定分类簇数量,分类 ...
 - nasm
			
sudo apt install nasm 64bit: nasm -f elf64 test.asm ld -s -o test test.o --------------------------- ...
 - web.xml中url-pattern中/和/*的区别(来自网络)
			
其中/和/*的区别: < url-pattern > / </ url-pattern > 不会匹配到*.jsp,即:*.jsp不会进入spring的 Dispatcher ...
 - jumpserver部署1.0版本
			
A. jumpserver概述 跳板机概述: 跳板机就是一台服务器,开发或运维人员在维护过程中首先要统一登录到这台服务器,然后再登录到目标设备进行维护和操作: 跳板机缺点:没有实现对运维人员操作行为的 ...
 - WPF界面控件Telerik UI for WPF发布R2 2019 SP1|实现新的属性
			
Telerik UI for WPF拥有超过100个控件来创建美观.高性能的桌面应用程序,同时还能快速构建企业级办公WPF应用程序.UI for WPF支持MVVM.触摸等,创建的应用程序可靠且结构良 ...
 - 在centos7.5使用DockerFile构建镜像时报错“Error parsing reference: "microsoft/dotnet:2.2-aspnetcore-runtime AS base" is not a valid repository/tag: invalid reference format”
			
运行dockerfile时报出的错误 FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base Error parsing reference: &qu ...
 - noip模拟题 Market
			
题面描述: 数据范围: Solution: 我们发现\(v\)很小,但是\(M\)很大,考虑转化一下一般的背包 我们用\(f[v]\)来表示拿到价值为\(v\)的物品需要付出的最少代价,特别的,当\( ...