博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
算法练习,链表二分最大n个
阅读量:4347 次
发布时间:2019-06-07

本文共 2962 字,大约阅读时间需要 9 分钟。

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
GetLeastNumbers_Solution(int[] input, int k) { ArrayList
list = new ArrayList
(); 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
hs = new HashSet
(); 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; }}

 

转载于:https://www.cnblogs.com/it-worker365/p/6958364.html

你可能感兴趣的文章
机器分配
查看>>
php opcode缓存
查看>>
springcloud之Feign、ribbon设置超时时间和重试机制的总结
查看>>
观看杨老师(杨旭)Asp.Net Core MVC入门教程记录
查看>>
UIDynamic(物理仿真)
查看>>
Windows下安装Redis
查看>>
winform非常实用的程序退出方法!!!!!(转自博客园)
查看>>
centos安装vim
查看>>
linux工作调度(计划任务)
查看>>
NIO:与 Buffer 一起使用 Channel
查看>>
Android帧缓冲区(Frame Buffer)硬件抽象层(HAL)模块Gralloc的实现原理分析
查看>>
MFC接收ShellExecute多个参数
查看>>
volatile和synchronized的区别
查看>>
类中的静态函数和非静态函数的区别
查看>>
windows 下安装Apache
查看>>
Fedora14 mount出现错误时解决办法【亲测有效】
查看>>
ruby实现生产者和消费者
查看>>
node.js 之 http 架设
查看>>
MongoDB 备份与还原
查看>>
Oracle启动与关闭数据库实例
查看>>