* Known an array int[98], the array stored 0~99 a total of 100 number of 98, the number is not duplicated, please use the algorithm to calculate the 0~99 missing 2 number is which two?
* Requirements: The array itself is generated by the program, the value is between 0~99, do not repeat each other
Ideas:
1. Generating an array containing 0~99 through a set set
2. After removing the 2 elements, transfer the set set to the set of ARR
3. The array is traversed to compare, the flag flag to determine the missing 2 numbers, no word false.
4. Define a temporary list to store the missing 2 numbers
The code is as follows:
PackageCom.itheima.bbs;Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.Set;ImportJava.util.TreeSet; Public classRandomarraydemo { Public Static voidMain (string[] args) {//generating an array of arr by a set setSet<integer> TreeSet =NewTreeset<integer>(); for(Integer i = 0; i < i++;) {treeset.add (i); } treeset.remove (0); Treeset.remove (99); Integer[] Arr=Newinteger[treeset.size ()]; Treeset.toarray (arr); //to iterate over an array and compare it to the number of 0~99List List =NewArrayList ();//temporary container, storing the missing 2 numbers for(inti = 0; I < 100; i++) { BooleanFlag =false; for(intj = 0; J < Arr.length; J + +) { if(i = =Arr[j]) {Flag=true; Break; } } if(!flag) {List.add (i); }} System.out.println ("The missing two numbers are:" +list); }}
Java algorithm--array