算法_01_分治

题号难度掌握
归并排序经典
leetcode 23hard

原理

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bmx2o8AZ-1628768309557)(.\img.png)]
如图所示、分治就是将大问题分成两个小问题再将两个小问题再分,只到能解决为止,然后再然返回结果。

  • 技巧

    T Partition(T[] t,0,n){ //将T[]分成两个
    	if (left==right) return T[lect];//说明分到了最小单位
    	else if (left>right) return null;//没有这个范围
    	else return merge(p(T[],0,n/2),p(T[],n/2,n));//合并结果返回
    } 
    T marge(T a,T b){ //合并的方法
    //具体方法
    }
    

实例

  • 归并排序

    img

    public class sort {
        public static void main(String[] args) {
            int[] a={12,2,31,2,3133,2};
            a=sort(a);
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i]+";");
            }
        }
        public static int[] sort(int[] l){
            return partition(l,0,l.length-1);
        }
        //分解
        public static int[] partition(int[] l,int left,int right){
            int[] ans;
            if(left==right){
                ans=new int[]{l[right]};
            }else if(left>right){
                ans=new int[0];
            }else {
                int mid=(left+right)/2;
                //分解成两个再合并
                return merge(partition(l,left,mid),partition(l,mid+1,right));
            }
            return ans;
        }
        //合并
        public static int[] merge(int[] a,int[] b){
            int i=0,j=0,p=0;
            int[] ans=new int[a.length+b.length];
            while(i<a.length||j<b.length){
                if(j>=b.length){ans[p++]=a[i++];continue;}
                if(i>=a.length){ans[p++]=b[j++];continue;}
                if(a[i]<b[j]){
                    ans[p++]=a[i++];
                }else {
                    ans[p++]=b[j++];
                }
            }
            return ans;
        }
    }
    
  • Leetcode 23

    • 问题:给你一个链表数组,每个链表都已经按升序排列。 请你将所有链表合并到一个升序链表中,返回合并后的链表。

    • 示例:

      输入:lists = [[1,4,5],[1,3,4],[2,6]]
      输出:[1,1,2,3,4,4,5,6]
      解释:链表数组如下:
      [
      1->4->5,
      1->3->4,
      2->6
      ]
      将它们合并到一个有序链表中得到。
      1->1->2->3->4->4->5->6
      
    • 解决

      class Solution {
          public ListNode mergeKLists(ListNode[] lists) {
              if(lists.length==0)return null;
              return Partition(lists,0,lists.length-1);
          }
      
          public ListNode Partition(ListNode[] lists,int left,int right){
              if(left==right){
                  return lists[left];
              }
              if(left>right){
                  return null;
              }
              int mid=(left+right)/2;
              //分解成两个子问题,再用marge合并
              return merge(Partition(lists,left,mid),Partition(lists,mid+1,right));
          }
      
          public ListNode merge(ListNode a,ListNode b){//合并
              if (a == null || b == null) {
                  return a != null ? a : b;
              }
              ListNode head = new ListNode(0);
              ListNode ap=a,bp=b,p=head;
              while(a!=null && b!=null){
                  if(a.val<b.val){
                      p.next=a;
                      a=a.next;
                  }else{
                      p.next=b;
                      b=b.next;
                  }
                  p=p.next;
              }
              p.next=(a!=null?a:b);
              return head.next;
          }
      }
      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值