Java algorithm--array

Source: Internet
Author: User

* 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.