leetcode:First Missing Positive 【Java】
2016-03-03 19:37
399 查看
一、问题描述
Given an unsorted integer array, find the first missing positive integer.
For example,
Given
and
Your algorithm should run in O(n) time and uses constant space.
二、问题分析
利用BitSet集合,BitSet讲解见博文Java
BitSet笔记。
三、算法代码
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.
二、问题分析
利用BitSet集合,BitSet讲解见博文Java
BitSet笔记。
三、算法代码
public class Solution { public int firstMissingPositive(int[] nums) { if(nums.length == 0){ return 1; } int index = -1; BitSet bs = new BitSet(); for(int i = 0; i < nums.length; i++){ if(nums[i] >= 0){ bs.set(nums[i]); } } for(int j = 0; j < bs.length(); j++){ if(!bs.get(j)){ if(j != 0){//0不算正数,所以要剔除 index = j; break; } } if(j == bs.length() - 1){ index = j + 1; } } return index; } }
相关文章推荐
- Eclipse中jsp、js文件编辑时,卡死现象解决汇总
- Leetcode:110. Balanced Binary Tree(JAVA)
- spring+mybatis事务管理(声明式+编码式)
- 2016蓝桥杯假期任务之《身份证升级》
- javaweb2.2 DOM解析之增删改查
- 《JAVA与模式》之桥梁模式
- Java基础语法
- intellij Idea 报jdk错误
- JAVA正则表达式 Pattern和Matcher
- Java删除文件和目录
- 深入研究java.lang.ThreadLocal类
- 【实例解析】在Java语言中对文件操作大全
- java并发编程-Executor框架
- java 多线程总结
- Java7中使用try-with-resource语法 实现两种复制文件方法
- 如何让eclipse进行智能提示?
- Leetcode:21. Merge Two Sorted Lists(JAVA)
- Java内存分配
- TIJ-第三章:操作符
- [JAVA · 初级]:5.以我微观看变量