![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
归并排序
蛋卷在月球烤面包
这个作者很懒,什么都没留下…
展开
-
力扣315 计算右侧小于当前元素的个数
思路:可以看作是查找逆序对的个数。通过归并排序,可以在比较两个数大小的时候知道逆序对的个数,但是如果真的排序,会破坏掉原本数组的顺序,对后续的查找逆序对有影响,因此选择创建一个数组存放下标,每次排序都是对下标排序。public List<Integer> countSmaller(int[] nums){ List<Integer> list=new ArrayList<>(); int len=nums.length;..原创 2022-05-02 15:04:43 · 380 阅读 · 0 评论 -
力扣148 排序链表
思路:采用归并排序思想,先利用快慢指针把链表分成两半,然后再合并到一起。public ListNode sortList(ListNode head){ if(head==null||head.next==null) return head; ListNode slow=head; ListNode fast=head.next; while(fast!=null&&fast.next!=null){原创 2022-03-02 10:19:03 · 219 阅读 · 0 评论 -
剑指offer51 数组中的逆序对
public int reversePairs(int[] nums){ if(nums.length<2) return 0; int[] copy=new int[nums.length]; for(int i=0;i<nums.length;i++){ copy[i]=nums[i]; } int[] temp=new int[nums.length]; retu...原创 2022-02-28 18:51:28 · 55 阅读 · 0 评论 -
力扣88 合并两个有序数组
方法一:从前往后。public void merge(int[] nums1, int m, int[] nums2, int n){ int[] nums3=new int[m]; System.arraycopy(nums1,0,nums3,0,m); int i=0;//指向nums3 int j=0;//指向nums2 int len=m+n; for(int k=0;k<len;k++)..原创 2022-02-28 16:52:46 · 63 阅读 · 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 评论