算法练习,链表二分最大n个
2017-06-07 17:54
190 查看
import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class BinarySearch { public static void main(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],10 GetLeastNumbers_Solution(new int[] { 4, 5, 1, 6, 2, 7, 3, 8 }, 10); } public static ArrayList<Integer> GetLeastNumbers_Solution(int[] input, int k) { ArrayList<Integer> list = new ArrayList<Integer>(); int cnt = 0; if (k > input.length || k == 0) { return list; } while (cnt < k) { list.add(input[cnt]); cnt++; } Collections.sort(list); for (int i = k; i < input.length; i++) { int num = input[i]; // 如果超出最大,则不用管,如果没有超出最大,则需要加入并踢出最大 if (num < list.get(k - 1)) { list.remove(k - 1); list.add(num); Collections.sort(list); } } System.out.println(list); return null; } /** * 插入排序 head 1 -> 7 -> 2 -> 6 -> 9 1 -> 2 -> 6 -> 7 -> 9 */ public static ListNode insertionSortList(ListNode head) { ListNode pstart = head; ListNode pcurr = head.next; if (pstart == null) { return null; } // //1 7 2 6 9 3 在7后面插入888 // 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 在7前面插入888 do { int currVal = pcurr.val; if (currVal == 7) { ListNode newNode = new ListNode(888, null); newNode.setNext(pcurr); pstart.setNext(newNode); } } while ((pcurr = pcurr.next) != null); return head; } public static char findFirstRepeat(String A, int n) { HashSet<Character> hs = new HashSet<Character>(); for (char c : A.toCharArray()) { if (hs.contains(c)) { return c; } else { hs.add(c); } } return ' '; } public static int getPos(int[] A, int n, int val) { int start = 0; int end = n - 1; int mid = (end - start) / 2; while (start < end) { if (A[mid] == val) { return mid; } else if (A[mid] > val) { end = mid; } else if (A[mid] < val) { start = mid; } mid = start + (end - start) / 2; } return 0; } }
相关文章推荐
- 蛙蛙推荐:算法练习:最大间隙问题
- 最大流,最大权二分匹配,二分匹配算法模板
- POJ 1698 Alice's Chance 最大流(两种算法)or二分匹配
- 最大流练习:Parking,二分匹配
- HDU 2444 The Accomodation of Students(最大二分匹配(匈牙利算法)+二分图判断->模板题目)
- 算法练习一:最大公约数与最小公倍数
- 匈牙利算法-最大二分匹配
- 利用匈牙利算法&Hopcroft-Karp算法解决二分图中的最大二分匹配问题 例poj 1469 COURSES
- “用最小堆将k个已排序链表合并为一个排序链表”(算法导论 练习6.5-9)
- hdu 2063 最大二分匹配,匈牙利算法
- 最大二分匹配 匈牙利算法模板&&POJ 1469 COURSES
- 解题报告-HDOJ-2063(最大二分匹配-匈牙利算法)
- 算法练习1(二元查找树转换成双向链表)
- POJ3041《Asteroids》方法:匈牙利算法(最小点覆盖数==最大二分匹配数)
- 蛙蛙推荐:算法练习:最大间隙问题
- Poj1469_匈牙利算法_最大二分匹配
- 图算法之:匈牙利算法(最大二分匹配)
- 二分匹配总结(匈牙利算法+最大权+最小权)
- 【算法导论】最大二分匹配
- 最大流练习:Graduation,二分匹配