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 ...
随机推荐
- L89
His voice was hoarse after several hours' speech.Attributive adjectives precede the noun.I gave the ...
- ubuntu的root权限设置
Linux操作系统有root权限用户和普通权限用户两种模式. 在执行一些需要权限才能执行的任务时,我们需要转化到root权限用户条件下才能执行. 1.普通用户权限转临时root权限: Linux中,通 ...
- 使用TortoiseGit同步代码到github远程仓库
1.clone github上的代码仓库的URL 可以用HTTPS,SSH, or Subversion 2.同步push 到远程仓库时 要用 SSH地址,同生成SSH private key ,在g ...
- wingide 显示中文 及 配色方案
http://lihuipeng.blog.51cto.com/3064864/923231 网上收集的方法: 显示中文: 任意文本编辑器打开:x:\Wing IDE\bin\gtk-bin\etc\ ...
- 【LeetCode】051. N-Queens
题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...
- 主库报 Error 12154 received logging on to the standby PING[ARC2]
主备网络配置存在问题 一系列报错 [root@node1 bin]# ./srvctl start database -d devdbPRCR-1079 : Failed to start reso ...
- selectedIndex 属性可设置或返回下拉列表中被选选项的索引号。
转自:https://blog.csdn.net/xxj19950917/article/details/73002046
- [poj2151]Check the difficulty of problems概率dp
解题关键:主要就是概率的推导以及至少的转化,至少的转化是需要有前提条件的. 转移方程:$dp[i][j][k] = dp[i][j - 1][k - 1]*p + dp[i][j - 1][k]*(1 ...
- 关于REST的一些想法
REST and RESTful 最近入手了REST,谈谈自己的体会. 所谓REST, 我觉得是一种网址的设计风格.过去我们用Struts 或Spring MVC 时从来没有考虑过URL的设计风格.所 ...
- Poll: Most Americans&n…
Most Americans support tough new measures to counter gun violence, including banning assault weapons ...