Leetcode First Missing Positive
Given an unsorted integer array, find the first missing positive integer.
For example,
Given [1,2,0]
return 3
,
and [3,4,-1,1]
return 2
.
Your algorithm should run in O(n) time and uses constant space.
给定无序整型数组,找出第一个不在数组里的正整数,要求时间复杂度为O(n),空间复杂度为O(1)
本题使用数组下标来存储相应的值,比如第k个元素(对应的下标是k-1)存储数字k,也就是A[k-1] = k。
对于大于数组长度的数字或者小于1的数字直接抛弃
一旦有了新数组,就从头开始扫描,遇到第一个A[k-1]不等于k时,输出k,如果没有遇到,那结果只能是数组长度的下一个数
class Solution {
public:
int firstMissingPositive(int A[], int n) {
for(int i = ; i < n; ++ i){
if(A[i] > && A[i]<=n){
if(A[i]- != i && A[A[i]-]!=A[i]) {
swap(A[i],A[A[i]-]);
i--;
}
}
}
for(int i = ; i < n; ++ i)
if(A[i]- != i) return i+;
return n+;
}
};
Leetcode First Missing Positive的更多相关文章
- [LeetCode] First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- LeetCode: First Missing Positive 解题报告
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- LeetCode – First Missing Positive
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- LeetCode OJ-- First Missing Positive
https://oj.leetcode.com/problems/first-missing-positive/ 给一列数,找出缺失的第一个正数.要求时间复杂度 O(n) 第一步遍历一遍,找出最大的数 ...
- leetcode First Missing Positive hashset简单应用
public class Solution { public int firstMissingPositive(int[] A) { HashSet<Integer> hash=new H ...
- leetcode First Missing Positive python
class Solution(object): def firstMissingPositive(self, nums): """ :type nums: List[in ...
- leetcode:First Missing Positive分析和实现
题目大意: 传入整数数组nums,求nums中未出现的正整数中的最小值.要求算法在O(n)时间复杂度内实现,并且只能分配常量空间. 分析: 一般碰到这种问题,都先对数组进行排序,再遍历数组就可以找到最 ...
- [LeetCode]题解(python):041-First Missing Positive
题目来源 https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the ...
- leetcode 41 First Missing Positive ---java
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
随机推荐
- nltk27_NLTK聚类分析
http://www.pythontip.com/blog/post/10044/ Python自然语言处理(三) -- 利用NLTK进行聚类 这篇文章介绍如何利用NLTK进行聚类,和上两篇文章Pyt ...
- epoll ET模式陷阱分析
0. 前言 这篇文章主要记录在使用epoll实现NIO接入时所遇到的问题. 1. epoll简介 epoll是Linux下提供的NIO,其主要有两种模式,ET(Edge trige)和LT(Level ...
- github提交代码时,报permission denied publickey
在像github提交代码时,报permission denied publickey. 查找了一下,可能是因为github的key失效了. 按照以下步骤,重新生成key. ssh-keygen 一路默 ...
- PHP数组函数: array_walk()
定义和用法 array_walk() 函数对数组中的每个元素应用回调函数.如果成功则返回 TRUE,否则返回 FALSE. 典型情况下 function 接受两个参数.array 参数的值作为第一个, ...
- PHP求余函数fmod()
定义和用法 fmod() 函数返回除法的浮点数余数. 语法 fmod(x,y) 参数 描述 x 必需.一个数. y 必需.一个数. 说明 返回被除数(x)除以除数(y)所得的浮点数余数.余数(r)的定 ...
- Android 设置启动界面
启动界面的意义是为了让后台处理耗时的复杂工作,当工作处理完成后,即可进入主界面.相比让用户等待布局加载完成,使用一张图片作为启动背景,会带来更好的体验. 首先,需要建立一个简单的布局: <?xm ...
- 【转载总结】jQuery和HTML5全屏焦点图
选项设置与说明 Slider Revolution提供了很多参数选项设置: delay: 滑动内容停留时间.默认9000毫秒 startheight: 滑动内容高度,默认490像素. startwid ...
- Xcode 此证书签发者无效
1.https://developer.apple.com/certificationauthority/AppleWWDRCA.cer 安装此证书 2.在keychains里选择login,然后点选 ...
- C/C++:C++中static,extern和extern "C"关键字
1. extern 变量 extern 表明该变量在别的地方已经定义过了,在这里要使用那个变量. 当extern不与"C"在一起修饰变量或函数时,如在头文件中: extern in ...
- asp.net ajax 调用后台方法
js代码 <form id="form1" runat="server"> <script language=javascript type= ...