The 2nd chapter of Digital Charm--looking for the post "Water king"

Source: Internet
Author: User

Find post "Water King" problem description

Tango is a pilot project in Microsoft Research Asia. The staff and interns at the Institute are very fond of exchanging water on tango. Legend has it that Tango has a big "water king", and he not only likes to post, but also replies to each of the posts sent by other IDs. Rumor has it that the "water king" posted more than half the total number of posts. If you have a list of all posts (including replies) on the current forum, and the ID of the post author is also in the table, can you quickly find out the legendary tango water king?

Solution

Use map to store each ID and the number of times it appears, then traverse through the map to find the "Water king", the time complexity is O (n) (n is the total number of ID stickers), the code is as follows:

1  PackageChapter2shuzizhimei.findshuiwang;2 3 ImportJava.util.HashMap;4 ImportJava.util.Iterator;5 ImportJava.util.Map;6 7 /**8 * Find a post "water king"9  * @authorDELLTen  * One  */ A  Public classFindshuiwang { -     /** - * Find the ID of "water king" posted the      * @paramID An array of all post IDs -      * @returnThe ID of "Water king" -      */ -      Public Static LongFindLongid[]) { +map<long,integer> map =NewHashmap<long,integer> ();//the storage ID and the number of times it occurred -         intTemp,max=0; +         Longking=0;//"Water King" ID A         LongID; at         //Save the ID and the number of occurrences of it in the map -          for(inti=0;i<id.length;i++){ -             if(Map.containskey (Id[i])) { -temp = Map.get (Id[i]) +1; - Map.put (id[i], temp); -}Else{ inMap.put (Id[i], 1); -             } to         } +iterator<long> Iterator = Map.keyset (). Iterator ();//ID iterator -          while(Iterator.hasnext ()) { theid = iterator.next ();//Take out an ID *temp = Map.get (ID);//Occurrences of the ID corresponding to the check out $ //if (Temp>max) {Panax Notoginseng //max = temp; - //king = ID; the //            } +             if(TEMP&GT;ID.LENGTH/2){ AKing =ID; the                  Break; +             } -         } $         returnKing; $     } -      Public Static voidMain (string[] args) { -         LongId[] = {123456,12401645,1324667,123456,123456}; theSystem.out.println ("\" Water king \ "ID:" +find (ID)); -     }Wuyi  the}

The results of the program run as follows:

"Water King" ID: 123456
Scaling Issues

With the development of Tango, the administrator found that "super Water King" did not. Statistical results show that there are 3 posts a lot of IDs, their number of postings are more than the total number of post n 1/4. Can you quickly find their ID from the list of post IDs?

Answer the same idea, the code is as follows:

1  PackageChapter2shuzizhimei.findshuiwang;2 3 ImportJava.util.HashMap;4 ImportJava.util.Iterator;5 ImportJava.util.Map;6 7 /**8 * Find a post "water king"9 * Extension IssuesTen  * @authorDELL One  * A  */ -  Public classFindShuiWang1 { -     /** the * Find the ID of "water king" posted -      * @paramID An array of all post IDs -      * @returnThe ID of "Water king" -      */ +      Public Static Long[] Find (Longid[]) { -map<long,integer> map =NewHashmap<long,integer> ();//the storage ID and the number of times it occurred +         intTemp,j=0; A         LongKing[];//"Water King" ID atKing =New Long[3]; -         LongID; -         //Save the ID and the number of occurrences of it in the map -          for(inti=0;i<id.length;i++){ -             if(Map.containskey (Id[i])) { -temp = Map.get (Id[i]) +1; in Map.put (id[i], temp); -}Else{ toMap.put (Id[i], 1); +             } -         } theiterator<long> Iterator = Map.keyset (). Iterator ();//ID iterator *          while(Iterator.hasnext ()) { $id = iterator.next ();//Take out an IDPanax Notoginsengtemp = Map.get (ID);//Occurrences of the ID corresponding to the check out -             if(TEMP&GT;ID.LENGTH/4){ theKING[J] =ID; +J + +; A             } the             if(j==3) +                  Break; -         } $         returnKing; $     } -      Public Static voidMain (string[] args) { -         LongId[] = {123456,12401645,1324667,123456,12401645,1324667,123}; the         LongKing[] =find (ID); -System.out.print ("Three \" Water king \ "ID:");Wuyi          for(inti=0;i<3;i++){ theSystem.out.print (king[i]+ ""); -         } Wu System.out.println (); -     } About  $}

The results of the program run as follows:

The 2nd chapter of Digital Charm--looking for the post "Water king"

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.