缺失数
Description
给出一个数组nums,数组本来应该包含0到n的所有整数,但现在缺少了一个,请你找出那个缺失数。
Input
输入数组nums中的n个整数,0<=n<=1000
Output
输出缺失数
Sample
代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s[] = reader.readLine().split(" ");
int n = s.length;
boolean[] boolArray = new boolean[n+1];
for(int i = 0;i<n;i++){
boolArray[Integer.parseInt(s[i])] = true;
}
for(int j = 0;j<n+1;j++){
if(boolArray[j]){
continue;
}else {
System.out.println(j);
break;
}
}
}
}
思路
使用空间换时间的思想,创建数组保存对应数字是否缺失的信息。