041 First Missing Positive 第一个缺失的正数
给一个未排序的数组,找出第一个缺失的正整数。
例如,
[1,2,0] 返回 3,
[3,4,-1,1] 返回 2。
你的算法应该在 O(n) 的时间复杂度内完成并且使用常数量的空间。
详见:https://leetcode.com/problems/first-missing-positive/description/
Java实现:
class Solution {
public int firstMissingPositive(int[] nums) {
int n=nums.length;
if(n<1||nums==null){
return 1;
}
for(int i=0;i<n;++i){
while(nums[i]>0&&nums[i]<=n&&nums[i]!=nums[nums[i]-1]){
int tmp=nums[nums[i]-1];
nums[nums[i]-1]=nums[i];
nums[i]=tmp;
}
}
for(int i=0;i<n;++i){
if(nums[i]!=i+1){
return i+1;
}
}
return n+1;
}
}
参考:https://www.cnblogs.com/grandyang/p/4395963.html
041 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] 41. First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- [LeetCode] 41. First Missing Positive ☆☆☆☆☆(第一个丢失的正数)
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法
First Missing Positive Given an unsorted integer array, find the first missing positive integer. Fo ...
- LeetCode OJ:First Missing Positive (第一个丢失的正数)
在leetCode上做的第一个难度是hard的题,题目如下: Given an unsorted integer array, find the first missing positive inte ...
- MissingNumber缺失的数字,FirstMissingPositive第一个缺失的正数
MissingNumber问题描述:给定一个数组,数组数字范围是0-n,找到缺失的数字.例如nums={0,1,3},return2. 算法分析:第一种方法,对数组进行排序,然后找到和下标不一致的数字 ...
- [leetcode]41. First Missing Positive第一个未出现的正数
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- Java for LeetCode 041 First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
- LeetCode 041 First Missing Positive
题目要求:First Missing Positive Given an unsorted integer array, find the first missing positive integer ...
随机推荐
- Android sdk 搭建
下载安装 http://pan.baidu.com/wap/share/home?uk=67915989&third=0 搭建Android环境时,无论使用的Eclipse还是Android ...
- 无言以队Alpha阶段项目复审
小组的名字和链接 优点 缺点,bug报告 (至少140字) 最终名次 (无并列) 甜美女孩 http://www.cnblogs.com/serendipity-zeng/p/9937832.html ...
- linux命令学习笔记(45):free 命令
free命令可以显示Linux系统中空闲的.已用的物理内存及swap内存,及被内核使用的buffer.在Linux系统监控的 工具中,free命令是最经常使用的命令之一. .命令格式: free [参 ...
- CodeForces - 1005E2:Median on Segments (General Case Edition) (函数的思想)
You are given an integer sequence a1,a2,…,ana1,a2,…,an. Find the number of pairs of indices (l,r)(l, ...
- MySQL-计算当月重新激活客户_20161013
13号的草稿 12号的明天补充更新,最近太忙了. 客户留存率是衡量客户价值经常用的指标,可以反映客户的活跃程度,在互联网企业,尤其是现在手机端流量已经超过PC端流量,在安卓和IOS设备上在线时长的数据 ...
- UVA1389 Hard Life[二分答案+最小割]
我真菜啊←地址 求最大密度子图方案.密度=边数/点数 假设E,V为最大密度子图的边数点数.则$\forall \rho$有$\rho \leqslant \frac{E}{V}$即$E- \rho V ...
- Ubuntu环境下对拍
何为对拍 假设我在考场上写了一个能过样例的算法.然后它也能过大样例但是我觉得有些担心某些细节会出错,或者是它连大样例都过不了但是大样例过大无法肉眼差错,这个时候我们就需要对拍了. 所谓对拍,就是对着拍 ...
- BZOJ1382:[Baltic2001]Mars Maps
浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://www.lydsy.com/JudgeOnline/prob ...
- 动态库*.so制作
转自:http://www.2cto.com/os/201308/238936.html 在linux下制作动态库*.so. 1.linux下动态库的制作 //so_test.h #include ...
- git学习 7 git每次push都输入用户名 密码
用如下命令改成SSH的方式 git remote rm origin git remote add origin git@github.com:username/repository.git git ...