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 ...
随机推荐
- CreateMutex实现只能打开一个客户端
#include "stdafx.h" #include <Windows.h> #include <iostream> using namespace s ...
- SpringMVC工作原理图解
SpringMVC的工作原理图: SpringMVC流程 1. 用户发送请求至前端控制器DispatcherServlet. 2. DispatcherServlet收到请求调用HandlerMa ...
- ORA-00904: 标识符无效——解决方案
转自:https://blog.csdn.net/jajavaja/article/details/49122639 建表时列名用双引号引着(用Navicat工具建表默认是加上双引号的),java连接 ...
- 用JS 循环做一个表格
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- resin容器更改JDK
更改resin的jdk版本,找到resin的配置文件:Resin\contrib\init.resin文件,找到 JAVA_HOME=@JAVA_HOME@ RESIN_HOME=@resin_hom ...
- springmvc 注解式开发 解决中文乱码问题
- session详解&和cookie的区别
session简介 1. 定义 session用来保存会话数据, 将数据保存到服务器中. 2. 作用 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),一个浏览器独占一 ...
- ArcGIS Field Type /esriFieldTypeDate(转)
ArcGIS Field Type The following table outlines the equivalent field data types in ArcCatalog, ArcO ...
- ROS探索总结(六)——使用smartcar进行仿真
转自:https://www.ncnynl.com/archives/201609/843.html 总结: 一.机器人描述文件三个: 机器人主体body文件: gazebo属性文件: 主文件 sma ...
- c# 类的初步认识
这里我们把类分为三种: String类(字符串类):Math类(数学类):DateTime类(时间日期类). 在使用类时注意 在输入的过程中代码前面会出现一些符号(紫色立方体代表方法,函数和黑色扳手 ...