BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #35680同步于 2014/10/28
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

分享一道leetcode的题

EMyuan
2014/10/28镜像同步21 回复
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.
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
peakmuma机器人#1 · 2014/10/28
分享?leetcode难道不是公开的么?
ab机器人#2 · 2014/10/29
楼主是不是没有复制全题目?贴个题目链接? 【 在 EMyuan 的大作中提到: 】 Given an unsorted integer ...
icybee机器人#3 · 2014/10/29
【 在 EMyuan 的大作中提到: 】 : Given an unsorted integer array, find the first missing positive integer. : For example, : Given [1,2,0] return 3, : .................. 题目没看懂,但是私以为找负数,希望我不要误导群众 For(i = 0;i < = n && Given[i] >= 0 ;i++); Return I;
laoguo机器人#4 · 2014/10/29
7个月之前ac的代码。。我自己都看不懂了= = public int firstMissingPositive(int[] A) { if(A.length == 0) return 1; int i = 0; while(i < A.length) { if(A[i] == i) { i++; } else { if(A[i] > 0 && A[i] < A.length && A[i] != A[A[i]]) { int tmp = A[i]; A[i] = A[tmp]; A[tmp] = tmp; } else { i++; } } } for(i = 1; i < A.length; i++) { if(A[i] != i) return i; } return A[0] ==A.length ? A.length + 1 : A.length; }
sun111机器人#5 · 2014/10/29
【 在 EMyuan 的大作中提到: 】 : Given an unsorted integer array, find the first missing positive integer. : For example, : Given [1,2,0] return 3, : ................... 还是别用Java来ACM了。。。真心不好使啊。。。
chinapds机器人#6 · 2014/10/29
真心不是,是找第一个缺少的正数。第一个有012,缺3,第二个有134,缺2 【 在 icybee 的大作中提到: 】 : 题目没看懂,但是私以为找负数,希望我不要误导群众 : For(i = 0;i < = n && Given[i] >= 0 ;i++); : Return I;
buptxrc机器人#7 · 2014/10/29
基本就是 尽量把 i 放到A[i] 上去,然后遍历一遍吧。。
llyzltz机器人#8 · 2014/10/29
原地hash
HB0318机器人#9 · 2014/10/29
求教一下怎么算复杂度,我这个的复杂度是多少呀,多谢啦。 import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; public class Main { public static void main(String[] args) { int[] a={1,1,2,4,5,8,-1,-5,3,6,6}; //找出数组所包含的正整数 List<Integer> b=new ArrayList<Integer>(); for(int i=0;i<a.length;i++){ if(a[i]>0){ b.add(a[i]); } } //去重并排序 HashSet h = new HashSet(b); b.clear(); b.addAll(h); Collections.sort(b); //找到第一个丢失的正整数 int j=0; while((j<b.size())&&(b.get(j)==(j+1))){ j++; } System.out.println(j+1); } } 【 在 laoguo 的大作中提到: 】 : 7个月之前ac的代码。。我自己都看不懂了= = : public int firstMissingPositive(int[] A) { : if(A.length == 0) return 1; : ...................