[LeetCode] Merge问题

来源:互联网 发布:in 软件好玩吗 编辑:程序博客网 时间:2024/06/01 10:18

88.Merge Sorted Array:点击打开链接

public class Solution {    public void merge(int[] nums1, int m, int[] nums2, int n) {   //从后往前放比较过程中的较大值        int i=m-1,j=n-1,index=m+n-1;        while(i>=0 && j>=0){            if(nums1[i]>nums2[j]){                nums1[index--]=nums1[i--];            }else{                nums1[index--]=nums2[j--];            }        }                while(i>=0){                                              //两个数组不是一样长,一个数组的剩余的直接放在前面            nums1[index--]=nums1[i--];        }        while(j>=0){            nums1[index--]=nums2[j--];        }    }}

21.Merge Two Sorted List:点击打开链接

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution {    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {       if(l1==null){           return l2;       }       if(l2==null){           return l1;       }              ListNode dummy=new ListNode(-1);       ListNode head=dummy;              while(l1!=null && l2!=null){           if(l1.val<l2.val){               head.next=l1;               l1=l1.next;           }else{               head.next=l2;               l2=l2.next;           }           head=head.next;       }       if(l1!=null){           head.next=l1;       }       if(l2!=null){           head.next=l2;       }       return dummy.next;    }}

617.Merge Two BInary Trees:点击打开链接

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {        if(t1==null && t2==null){            return null;        }else if(t1==null){            return t2;        }else if(t2==null){            return t1;        }                TreeNode root=new TreeNode(t1.val+t2.val);        root.left=mergeTrees(t1.left, t2.left);        root.right=mergeTrees(t1.right, t2.right);                return root;    }}

Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list.

Analyze and describe its complexity.

Example

Given lists:

[  2->4->null,  null,  -1->null],

return -1->2->4->null.

思路:遍历list两两合并,如果list.size()为奇数,最后一个再放入list,进行下一轮的合并,直到list.size()为1

/** * Definition for ListNode. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int val) { *         this.val = val; *         this.next = null; *     } * } */ public class Solution {    /**     * @param lists: a list of ListNode     * @return: The head of one sorted list.     */        public ListNode mergeKLists(List<ListNode> lists) {        if(lists==null || lists.size()==0){            return null;        }                while(lists.size()>1){            List<ListNode> newLists=new ArrayList<>();               //每一次都要有一个新的newLists            for(int i=0;i<lists.size()-1;i=i+2){                ListNode newOne=merge(lists.get(i),lists.get(i+1));                newLists.add(newOne);            }            if(lists.size()%2==1){                newLists.add(lists.get(lists.size()-1));            }            lists=newLists;                                          //使得一轮合并完newLists成为lists        }        return lists.get(0);    }        private ListNode merge(ListNode head1,ListNode head2){        ListNode dummy=new ListNode(-1);        ListNode head=dummy;        while(head1!=null && head2!=null){            if(head1.val<head2.val){                head.next=head1;                head1=head1.next;            }else{                head.next=head2;                head2=head2.next;            }            head=head.next;        }        if(head1!=null){            head.next=head1;        }        if(head2!=null){            head.next=head2;        }                return dummy.next;    }}




原创粉丝点击