双重缺失数
Description
给出一个数组nums,数组本来应该包含1到n的所有整数,但现在缺少了两个,请你找出两个缺失数(从小到大的顺序)。
Input
输入n - 2个整数,1<=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("\\s");
int n = s.length;
boolean[] boolArray = new boolean[n+3];
for(int i = 0;i<n;i++){
boolArray[Integer.parseInt(s[i])] = true;
}
int c = 0;
for(int j = 1;j<n+3;j++){
if(boolArray[j]){
continue;
}else if(c==1) {
System.out.println(j);
break;
}else {
System.out.print(j);
System.out.print(" ");
c++;
}
}
}
}
思路
仿照一次缺失数,设置个触发器保证找到两次缺失数