Algorithm exercise, List of two points

Source: Internet
Author: User

Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.HashSet; Public classBinarySearch { Public Static voidMain (string[] args) {int[] A = {11, 27, 28, 33 }; //System.out.println (Findfirstrepeat ("QYWYER23TDD", 11)); //ListNode head = linkedlistreverse.initiallist (); //Linkedlistreverse.printlist (Insertionsortlist (head)); [4,5,1,6,2,7,3,8],10Getleastnumbers_solution (New int[] {4, 5, 1, 6, 2, 7, 3, 8}, 10); }     Public StaticArraylist<integer> Getleastnumbers_solution (int[] Input,intk) {ArrayList<Integer> list =NewArraylist<integer>(); intCNT = 0; if(k > Input.length | | k = = 0) {            returnlist; }         while(CNT <k) {List.add (input[cnt]); CNT++;                } collections.sort (list);  for(inti = k; i < input.length; i++) {            intnum =Input[i]; //if the maximum is exceeded, then no tube, if not exceeded the maximum, you need to join and kick the maximum            if(Num < List.get (k-1) {list.remove (k-1);                List.add (num);            Collections.sort (list);        }} System.out.println (list); return NULL; }    /*** Insertion Sort head 1, 7, 2, 6 , 9 1, 2, 6, 7, 9*/     Public StaticListNode insertionsortlist (ListNode head) {ListNode Pstart=Head; ListNode Pcurr=Head.next; if(Pstart = =NULL) {            return NULL; }        // //1 7 2 6 9 3 insert 888 after 7//Do {//int currval = Pstart.val; //if (currval = = 7) {//ListNode newNode = new ListNode (888, NULL); //Newnode.setnext (Pstart.next); //Pstart.setnext (NewNode); // }        //} while ((Pstart = pstart.next) = null); //1 7 2 6 9 3 Insert 888 in front of 7         Do {            intCurrval =Pcurr.val; if(Currval = = 7) {ListNode NewNode=NewListNode (888,NULL);                Newnode.setnext (Pcurr);            Pstart.setnext (NewNode); }        }  while((Pcurr = pcurr.next)! =NULL); returnHead; }     Public Static CharFindfirstrepeat (String A,intN) {HashSet<Character> HS =NewHashset<character>();  for(CharC:a.tochararray ()) {            if(Hs.contains (c)) {returnC; } Else{Hs.add (c); }        }        return‘ ‘; }     Public Static intGetPos (int[] A,intNintval) {        intStart = 0; intEnd = N-1; intMid = (End-start)/2;  while(Start <end) {            if(A[mid] = =val) {                returnmid; } Else if(A[mid] >val) {End=mid; } Else if(A[mid] <val) {Start=mid; } Mid= start + (End-start)/2; }        return0; }}

Algorithm exercise, List of two points

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.