1 /** 2 * 找出未出现的最小正整数 3 * @param A 4 * @param n 5 * @date 2016-10-7 6 * @author shaobn 7 */ 8 public static int findArrayMex(int[] a,int n){ 9 int count = n; 10 int temp = 0; 11 int dir = 1; 12 int num = 0; 13 for(int i = 0;i<count-1;i++){ 14 if(a[i]>a[i+1]){ 15 temp = a[i]; 16 a[i]= a[i+1]; 17 a[i+1] = temp; 18 } 19 if(i==count-2){ 20 count--; 21 i=-1; 22 } 23 } 24 if(a[0]>1 || a[n-1]<1){ 25 return 1; 26 }else { 27 for(int i =0;i<n-1;i++){ 28 if(a[i]>0){ 29 num = i; 30 break; 31 } 32 } 33 if(a[num]>1){ 34 return 1; 35 }else if(a[num]==1) { 36 for(int i =num+1;i<n-1;i++){ 37 if(a[i]-a[num]>dir){ 38 return a[num]+dir; 39 }else { 40 dir++; 41 } 42 } 43 } 44 } 45 return a[num]+dir; 46 }
时间: 2024-09-29 23:20:37