【LeetCode练习题】First Missing Positive
First Missing Positive
Given an unsorted integer array, find the first missing positive integer.
For example,
Given[1,2,0]return3,
and[3,4,-1,1]return2.Your algorithm should run in O(n) time and uses constant space.
题目意思:
返回第一个缺失的正数。
时间复杂度应该在O(n)且只能使用常数的额外空间。
解题思路:
遍历两遍数组,第一遍的目的是使数组下标为 i 的数值设置为 i+1,如没有 i+1 就是其他值(不在1~n范围的值)。
第二遍遍历的目的是返回第一个不满足下标为 i ,值为 i+1 的地方,return i + 1;
代码如下:
class Solution {
public:
int firstMissingPositive(int A[], int n) {
int i = ;
while(i < n){
if(A[i] != i+ && A[i]> && A[i]<=n && A[A[i]-] != A[i])
swap(A[i],A[A[i]-]);
else
i++;
}
for(i = ; i < n; i++){
if(A[i] != (i+))
return i+;
}
return n+;
}
};
【LeetCode练习题】First Missing Positive的更多相关文章
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
- leetcode 41 First Missing Positive ---java
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- [LeetCode] 41. First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- 【leetcode】First Missing Positive
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- 【leetcode】First Missing Positive(hard) ☆
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- LeetCode - 41. First Missing Positive
41. First Missing Positive Problem's Link ---------------------------------------------------------- ...
- LeetCode题解-----First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- Java for LeetCode 041 First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
- Java [Leetcode 41]First Missing Positive
题目描述: Given an unsorted integer array, find the first missing positive integer. For example,Given [1 ...
随机推荐
- CentOS安装错误:no default or ui configuration
靠,以后再也不用浏览器自带的下载工具下载镜像文件了,原来是下载的不完整,但是显示完全下载完毕了,真特么误导人.文件的checksum不对. references: https://www.centos ...
- java实现xml-rpc客户端和服务端
客户端代码: package test_xmlrpc.test; import java.net.URL;import java.util.ArrayList;import java.util.Lis ...
- UESTC_韩爷的梦 2015 UESTC Training for Search Algorithm & String<Problem N>
N - 韩爷的梦 Time Limit: 200/100MS (Java/Others) Memory Limit: 1300/1300KB (Java/Others) Submit Stat ...
- UVA - 10129 Play on Words(欧拉回路+并查集)
2.解题思路:本题利用欧拉回路存在条件解决.可以将所有的单词看做边,26个字母看做端点,那么本题其实就是问是否存在一条路径,可以到达所有出现过的字符端点.由于本题还要求了两个单词拼在一起的条件是前一个 ...
- linux虚拟主机管理系统wdcp系列教程之三
我们安装了网站服务管理系统wdcp之后,在使用过程中可能会出现这样或那样的疑问,下面给大家整理几点出来,方便大家学习.还有不懂的可以到wdlinux论坛寻找相关教程. 1.wdcp后台访问安全设置即限 ...
- c-version:null]] could not deserialize the servlet-context scoped attribute with name: "MENU_LIST"
<Jul 26, 2013 10:45:02 AM CST> <Error> <HTTP> <BEA-101362> <[ServletConte ...
- linux查看用户登录信息-w命令
NAME w - Show who is logged on and what they are doing. SYNOPSIS w - [husfV] [user] DESCRIPTION w di ...
- C# winfrom 模拟ftp文件管理
从网上找到的非常好用的模拟ftp管理代码,整理了一下,希望对需要的人有帮助 using System; using System.Collections.Generic; using System.T ...
- 简单随笔——如何在gridview的页脚显示信息。。。。
我是超级大菜鸟...哈哈 先上图看看是不是你需要的 第一步,右击gridview,在事件中,单击RowdataBond事件. 在这之前一定要记得在gridview属性中的ShowFooter设置为“t ...
- easyui placeholder 解决方案
最近,再用easyui的时候,发现easyui的input标签不支持h5的placeholder,为了实现这个效果,提供以下解决方案: 1.给input标签设置placeholder. <td& ...