![](https://img-blog.csdnimg.cn/20201014180756919.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
排序
蛋卷在月球烤面包
这个作者很懒,什么都没留下…
展开
-
力扣406 根据身高建队列
思路:根据题目中数组中数对的定义,数对中第一个元素应该按降序排列,因为每个元素前面都有高于等于自己的;数对的第二个元素应该按升序排列,因为定义了前面有多少高于等于自己的数对。 先排好顺序后再利用linkedlist的add方法进一步排序。 linkedlist的add方法如下: public void add(int index, E element) { checkPositionIndex(index); if (index == size)原创 2022-03-02 17:21:38 · 177 阅读 · 0 评论 -
力扣15 三数之和
public List<List<Integer>> threeSum(int[] nums){ List<List<Integer>> res=new ArrayList<>(); int len=nums.length; if(nums==null||len<3) return res; Arrays.sort(nums); for(int i=0;i<.原创 2022-03-01 17:14:23 · 137 阅读 · 0 评论 -
剑指offer41 数据流中的中位数
题目要求获取一个数据流排序后的中位数,那么可以使用两个优先队列(堆)实现。 本题使用一个大顶堆和一个小顶堆:大顶堆用来存放较小的那一半值,小顶堆用来存放较大的那一半值。输入数组元素中,需要保持两个堆的长度相等,如果数组长度为奇数,就使小顶堆为长度更大的那一个,最后中位数就是小顶堆的堆顶。 如果直接把值插入到堆里,因为无法确定这个值是较大的那一半还是较小的那一半,如果是较大的那一半,直接放入小顶堆不会破坏顺序,反之如果是较小的那一半,会破坏堆内的顺序,所以: 当两个堆长度一样时,先放入大顶堆,大顶堆重新原创 2022-03-01 16:01:38 · 49 阅读 · 0 评论 -
归并排序总结
归并排序采用分治思想,将问题划分为一些小问题然后递归化解,在治的阶段整理合并。 public class MergeSort { public static void main(String[] args) { int[] array={9,8,7,6,5,4,3,2,1}; sort(array); System.out.println(Arrays.toString(array)); } public stat..原创 2022-02-28 15:39:07 · 77 阅读 · 0 评论 -
剑指offer3 数组中重复的数字
public int findRepeatNumber(int[] nums) { int index=0; while(index<nums.length){ if(nums[index]==index){ index++; continue; } if(nums[index]==nums[nums[index]]) retur...原创 2022-02-28 12:54:55 · 50 阅读 · 0 评论