题解 | #数组中未出现的最小正整数#
数组中未出现的最小正整数
http://www.nowcoder.com/practice/8cc4f31432724b1f88201f7b721aa391
A过的第一道难题,哈哈哈
import java.util.*; public class Solution { /** * return the min number * @param arr int整型一维数组 the array * @return int整型 */ public int minNumberdisappered (int[] arr) { int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; int sum1 = 0; int sum2 = 0; for(int i=0;i<arr.length;i++){ sum1 += arr[i]; max = Math.max(max,arr[i]); min = Math.min(min,arr[i]); } for(int i=min;i<=max;i++) sum2 += i; if(sum1==sum2) return min-1<=0? max+1:min-1; else return sum2-sum1; } }