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