public class MissingNumberUtil {
/**
* 寻找一串自然数中缺失的数字
*
* @param src 数组
* @param maxElement 数组中最大元素
* @return
*/
public static String getMissingNumber(int[] src, int maxElement) {
StringBuffer result = new StringBuffer();
int[] toolArray = new int[maxElement + 1];
toolArray[0] = 1;
for (int i = 0; i < src.length; i++) {
int num = src[i];
toolArray[num] = 1;
}
for (int i = 0; i < toolArray.length; i++) {
int num = toolArray[i];
if (num != 1) {
result.append(i + ",");
}
}
if (result.length() > 0) {
result.deleteCharAt(result.length() - 1);
}
return result.toString();
}
public static void main(String[] args) {
int[] arr = new int[] {1,10,7,4};
//System.out.println(Arrays.stream(arr).max().getAsInt());
//System.out.println(Arrays.stream(arr).min().getAsInt());
System.out.println(getMissingNumber(arr, Arrays.stream(arr).max().getAsInt()));
}
}
网友评论