The cycle, condition and probability of the JAVA Programmer's Interview (fourth edition)

Source: Internet
Author: User

Share content: about the use of collections

Page Number:77 pages

title: A String containing multiple characters in the a~z, if there are duplicates, such as string data = "AAVZCADFDSFSDHSHGWASDFASDFDDAAA", the most occurrences of the letter and the number of times, if there are multiple duplicates are found.

1 Importjava.util.ArrayList;2 Importjava.util.Collections;3 ImportJava.util.Iterator;4 ImportJava.util.TreeSet;5 6 7 8 9  Public classTest {Ten      One          Public Static  voidMain (string[] args) { AString input = "AAVZCADFDSFSDHSHGWASDFASDFDDAAA"; -             Newtest (). dostring (input); -     } the          -      Public voiddostring (String input) { -         Char[] chars =Input.tochararray (); -arraylist<string> lists =NewArraylist<string>(); +          -         //The first step is to pass the array to TreeSet, which automatically filters out the duplicate letters, and the collection is automatically sorted.  +Treeset<string> set =NewTreeset<string>(); A          for(inti=0;i<chars.length;i++){ at Lists.add (string.valueof (chars[i)); - Set.add (string.valueof (chars[i)); -         } -          -SYSTEM.OUT.PRINTLN (set);//output: [A, C, D, F, G, H, S, V, W, Z] -Collections.sort (lists);//Step Two: Use ArrayList for quick sorting inSYSTEM.OUT.PRINTLN (lists);//output: [A, a , A, a, a, a, a, a, C, D, D, D, D, D, D, D, F, F, F, F, G, H, H, S, S, S, S, S, V, W, Z] -          toStringBuffer SB =NewStringBuffer (); +          for(intI=0;i<lists.size (); i++){ - Sb.append (Lists.get (i)); the         } *          $input =sb.tostring ();Panax NotoginsengSYSTEM.OUT.PRINTLN (input);//Output: Aaaaaaaacdddddddffffghhsssssvwz -         intMax = 0; theString maxstring = ""; +Arraylist<string> maxlist =NewArraylist<string>(); A          theIterator its =set.iterator (); +          while(Its.hasnext ()) { -String OS =(String) Its.next (); $             intBegin =input.indexof (OS); $             intend = Input.lastindexof (OS);//Step three: Use IndexOf and lastIndexOf to find the maximum value.  -             intValue = End-begin +1; -             if(value>max) { theMax =value; -Maxstring =os;Wuyi maxlist.add (OS); the}Else if(value==max) { - maxlist.add (OS); Wu             } -         } About          $System.out.println ("maxstring:" +maxstring);//Output: Maxstring:a -System.out.println ("maxlist:" +maxlist);//output: Maxlist:[a] -          -         intindex = 0; A          for(intI=0;i<maxlist.size (); i++){ +             if(Maxlist.get (i). Equals (maxstring)) { theindex =i; -                  Break; $             } the         } the          theSystem.out.print ("Max data:");//output: Max data:a the          for(intI=index;i<maxlist.size (); i++){ -System.out.println (Maxlist.get (i) + ""); in         } the          the System.out.println (); AboutSystem.out.println ("Max:" +max);//Output: Max:8 the          the         }     the      +      -}

The reason for sharing This example is that it contains some examples of collections (TreeSet, ArrayList, IndexOf, lastIndexOf), and it feels more classic to share, and of course this article I would like to share some examples of recursion, But I feel that the understanding of recursion is relatively superficial, and so I take a time to study, then come out to share.

This post has not been continued ...

The cycle, condition and probability of the JAVA Programmer's Interview (fourth edition)

Related Article

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.