leetcode — first-missing-positive
/**
*
* Source : https://oj.leetcode.com/problems/first-missing-positive/
*
* Created by lverpeng on 2017/7/15.
*
* 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.
*
*/
public class FindFirstMissingPositive {
/**
* 找到第一个确实的正整数
* 题目特点:
* 数组是一个整数数组
* 数组中的正整数中除了缺失的一个正整数,其他都是连续的,也就是说一个长度为n的数组,数组中的数是1-n(最多,因为可能有0或者负数)
*
* 可以把每一个数房放在其下标减1的位置
* 然后遍历数组,发现num[i] != num[i-1]或说明就找到了缺失的数
*
* @param num
* @return
*/
public int fistMissingPositive (int[] num) {
for (int i = 0; i < num.length;) {
if (num[i] > 0 && num[i] != num[num[i] - 1]) {
int temp = num[num[i]-1];
num[num[i]-1] = num[i];
num[i] = temp;
} else {
i ++;
}
}
for (int i = 1; i < num.length; i++) {
if (num[i] != (num[i-1] + 1)) {
return num[i - 1] + 1;
}
}
return -1;
}
public static void main(String[] args) {
FindFirstMissingPositive findFirstMissingPositive = new FindFirstMissingPositive();
int[] arr = new int[]{1,2,0};
int[] arr1 = new int[]{3,4,-1,1};
int[] arr2 = new int[]{3,4,1,1};
int[] arr3 = new int[]{3,4,1,1};
System.out.println(findFirstMissingPositive.fistMissingPositive(arr));
System.out.println(findFirstMissingPositive.fistMissingPositive(arr1));
System.out.println(findFirstMissingPositive.fistMissingPositive(arr2));
System.out.println(findFirstMissingPositive.fistMissingPositive(arr3));
}
}
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
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] ...
随机推荐
- js html标签select 中option 删除除了第一行外的其他行
背景:共两个下拉框,第一个下拉框选择完之后,以第一个选定的值为条件返回第二个下拉框中的内容,用js中的createElement()创建,并利用appendChild()来添加进父标签.出现意外:每次 ...
- C#实现简单的RPC
demo地址:https://pan.baidu.com/s/1PeTdV2V9DF87jZTHdz4CyA 提取码:n2qm 参考地址:https://github.com/neuecc/Magic ...
- linux nfs远程挂载和卸载
一.nfs远程挂载 1.首先确定服务端(实体挂载节点)的IP 2.通过cat /etc/hosts 查看服务端的server name 3.mount -t nfs servername:/挂载文件 ...
- Document.write和 getElementById(ID)
在javascript中,document.write()方法:常用来网页向文档中输出内容. 示例:通过document.write()方法,向网页文档中输出了一段文字. document.write ...
- sql批量新增,修改
<insert id="insertExtDocList" parameterType="map"> INSERT INTO extprjdoc ( ...
- Ubuntu 16.04上安装Global阅读源代码工具
参照10年前写的文档 (Linux源码阅读工具lxr和glimpse的安装与配置),想重新搭建一个源代码阅读工具,发现源里面都没有相关的工具了. 然后看到有更简单的安装工具Global可以使用,所以果 ...
- [swarthmore cs75] Compiler 6 – Garbage Snake
课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第9次大作业. 赋值的副作用:循环元组 下面的代码展示了Python3是如何处理循环列表(pri ...
- python 类成员知识点学习的一个坑(初学者,大神请绕行)
先来段小程序class Foo: name = "abc" def __init__(self,age): self.age = age print(Foo.name)Foo.na ...
- linux vg lv pv
= pv由物理卷或者分区组成 pv可以组成一个或者多个vg vg可以分成多个lv 方便扩展 pvs vgs lvs 可以查看当前存在的pv vg lv 我的centos硬盘20g 使用了一 ...
- Java中main方法参数String[ ] args的使用。
我们刚开始学习java时都会被要求记住主方法(main)的写法,就像这样: public static void main(String[] args){ } public static void m ...