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] ...
随机推荐
- php学习备注笔记
一: PHP内核相关 http://blog.csdn.net/ywh147/article/details/40188411 [深入PHP内核(二)——SAPI探究] http://www.nowa ...
- python模块:csv
""" csv.py - read/write/investigate CSV files """ import re from _csv ...
- JAVA主流日志梳理
JAVA主流日志梳理 引入 历史故事 Log4j - JDK1.3及以前 JUL - JDK1.4 JCL - 日志门面commons-logging的出现 SLF4j - 可能是最好的日志框架 lo ...
- Github使用: 本地上传, 与之同步
很久不用Github 又忘记了怎么同步了, Git桌面前年已经部署好了. 1. 打开GitHub Desktop -- file -- clone repository --- 操作其中想要的一个文件 ...
- linux sshd 登录不需要密码
ssh 安全外壳协议 协议22 linux 默认放行了 22 号接口 ssh 默认安装 自行安装 应该是 openssh-server ssh命令是 openssh ssh-keygen 生成密钥 生 ...
- Hive数据倾斜解决方案
https://blog.csdn.net/yu0_zhang0/article/details/81776459 https://blog.csdn.net/lxpbs8851/article/de ...
- Hive(一)
1. HIVE概念: Hive:由Facebook开源用于解决海量结构化日志的数据统计. Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张表,并提供类SQL查询功能. 本 ...
- 常用maven仓库
常用Maven仓库网址:http://mvnrepository.com/http://search.maven.org/http://repository.sonatype.org/content/ ...
- Hadoop集群及基本组件搭建
本人采用一个master和两个slave的网络结构,具体搭建如下 1.准备安装包 1.下载安装包 http://pan.baidu.com/s/1jIoZulw 2.安装包清单 scala-2.12. ...
- [转] kaldi中FST的可视化-以yesno为例
http://blog.csdn.net/u013677156/article/details/77893661 1.kaldi解码过程 kaldi识别解码一段语音的过程是:首先提取特征,然后过声学模 ...