11.Find All Numbers Disappeared in an Array(找出数组中缺失的数)
Level:
Easy
题目描述:
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。算法的时间复杂度要求在O(n),我们可以对数组进行一个原地排序,如果nums[i]不等于i+1,并且nums[i]!=nums[nums[i]-1],那么我们就交换num[i]和nums[nums[i]-1]。对整个数组遍历完成后,我们重新遍历数组,如果nums[i]不等于i+1,那么i+1就是缺失的数字。
代码:
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
ArrayList<Integer>res=new ArrayList<>();
for(int i=0;i<nums.length;i++){
while(nums[i]!=i+1&&nums[nums[i]-1]!=nums[i]){
int temp=nums[i];
nums[i]=nums[temp-1];
nums[temp-1]=temp;
}
}
for(int i=0;i<nums.length;i++){
if(nums[i]!=i+1)
res.add(i+1);
}
return res;
}
}
11.Find All Numbers Disappeared in an Array(找出数组中缺失的数)的更多相关文章
- [LeetCode] 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 ot ...
- [LeetCode] Find All Duplicates in an Array 找出数组中所有重复项
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...
- LeetCode 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 ot ...
- 448 Find All Numbers Disappeared in an Array 找到所有数组中消失的数字
给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次.找到所有在 [1, n] 范围之间没有出现在数组中的数字.您能在不使用 ...
- LeetCode 448. Find All Numbers Disappeared in an Array找到所有数组中消失的元素
题目 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您能 ...
- Leetcode448.Find All Numbers Disappeared in an Array找到所有数组中消失的数字
给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您能在不 ...
- 442. Find All Duplicates in an Array找出数组中所有重复了两次的元素
[抄题]: Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and o ...
- 215. Kth Largest Element in an Array找出数组中第k大的值
堆排序做的,没有全部排序,找到第k个就结束 public int findKthLargest(int[] nums, int k) { int num = 0; if (nums.length &l ...
- 448. Find All Numbers Disappeared in an Array 寻找有界数组[1,n]中的缺失数
[抄题]: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice ...
随机推荐
- Even uploading a JPG file can lead to Cross-Site Content Hijacking (client-side attack)!
Introduction: This post is going to introduce a new technique that has not been covered previously i ...
- 2010.1.1 CLR 无法从 COM 上下文
今天做一个程序,sql操作,但是记录数太多,而且sql语句有复杂,就报了这样的错误: CLR 无法从 COM 上下文 0x645e18 转换为 COM 上下文 0x645f88,这种状态已持续 60 ...
- 什么是SPU、SKU、SKC、ARPU
首先,搞清楚商品与单品的区别.例如,iphone是一个单品,但是在淘宝上当很多商家同时出售这个产品的时候,iphone就是一个商品了. 商品:淘宝叫item,京东叫product,商品特指与商家有关的 ...
- C语言学习笔记--指针和数组的关系
1.数组的本质 (1)数组是一段连续的内存空间 (2)数组的空间大小:sizeof(array_type)*array_size; (3)数组名可看做指向数组第一个元素的常量指针 (4)数组声明时编译 ...
- jumpserver跳板机的搭建
搭建的跳板机基于0.3.2,别问我为什么不用0.5版本的,我能说我没有搭建成功么,步骤贼多,功能不完善,不建议生产环境使用 步骤其实很简单: github wiki :https://github.c ...
- 一些API的用法
//1.init初始化 NSString * str1 = [[NSString alloc] init]; NSLog(@"str1 = %@",str1); //2.initW ...
- Hibernate 执行sql语句返回yntax error: syntax error, expect LPAREN, actual NOT not
hibernate自动创建表时提示 : ERROR: sql injection violation, syntax error: syntax error, expect LPAREN, actu ...
- go语言linux环境配置
linux的设置方法:有4个环境变量需要设置:GOROOT.GOPATH.GOBIN以及PATH.需要设置到某一个profile文件中(~/.bash_profile(单一用户)或/etc/profi ...
- c#基础;初步学习循环语句
循环语句就是 在满足循环条件的情况下会有顺序的执行循环体 循环语句:for : while : foreach:三种. 循环语句 必须具备四要素:初始条件.循环条件.循环体.状 ...
- matlab 修改文件夹下所有文件名大写为小写
1. path = './DIR/';Files = dir(fullfile(path,'*.m'));LengthFiles = length(Files);for count_i = 1 : L ...