|
亲!马上注册或者登录会查看更多内容!
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
- int[] missing2Numbers(int [] a) {
- if(a.length==0)
- return new int[]{1,2};
-
- //counting sort, place elements in their rightful indexes
- for(int i=0;i<a.length;i++){
- if(a【i】>0 && a【i】<a.length && a[a【i】-1] != a【i】){
- int temp = a【i】;
- a【i】 = a[a【i】-1];
- a[temp-1] = temp;
- i--;
- }
- }
-
- int[] res = new int[2];
- int place = 0;//index to place the missing elements
- int max=0;
- for(int i=0;i<a.length;i++){
- if(a【i】!=i+1){
- res[place] = i+1;
- place++;
- }
- max = Math.max(a【i】,max);
- }
-
- if(place == 2){
- return res;
- } else if(place==0){
- return new int[]{a.length+1,a.length+2};
- } else {
- if(max==a.length+2){
- res[place] = a.length+1;
- } else {
- res[place] = a.length+2;
- }
-
- return res;
- }
- }
复制代码 主要的思路来自于lc的first missing positive,利用counting sort方法将下标与数字值对应起来,接下来找出missing positive,但是一直wa,不知道可能出现错误的地方在哪里?
|
|