题目

Given two sorted integer arrays A and B, merge B into A as one sorted array.

Note:
You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.

题解:

这道题是说让B merge到 A 里面。

先复习下原本我们在MergeSort里面怎么利用一个新建的数量来merge two array:

代码如下:

 1 public int[] mergeTwoList(int[] A, int[] B) {
 2     int[] C = new int[A.length + B.length];
 3     int k = 0;
 4     int i = 0;
 5     int j = 0;
 6     while(i < A.length && j < B.length) {
 7         if (A[i] < B[j])
 8             C[k++] = A[i++];
 9         else
             C[k++] = B[j++];
     }
     while (i < A.length) 
         C[k++] = A[i++];
     while (j < B.length) 
         C[k++] = B[j++];
     return C;
 }

然后我们再顺便复习下,怎么merge two linked list,代码如下:

 1     public ListNode mergeTwoLists(ListNode leftlist, ListNode rightlist){
 2         if(rightlist == null)
 3             return leftlist;
 4         if(leftlist == null)
 5             return rightlist;
 6         
 7         ListNode fakehead = new ListNode(-1);
 8         ListNode ptr = fakehead;
 9         while(rightlist!=null&&leftlist!=null){
             if(rightlist.val<leftlist.val){
                 ptr.next = rightlist;
                 ptr = ptr.next;
                 rightlist = rightlist.next;
             }else{
                 ptr.next = leftlist;
                 ptr = ptr.next;
                 leftlist = leftlist.next;
             }
         }
         
         if(rightlist!=null)
             ptr.next = rightlist;
         if(leftlist!=null)
             ptr.next = leftlist;
         
         return fakehead.next;
     }

可以看出merge的思路都是在从头比较两个list的value,用两个指针分别指向当前要比较的node上面。而且最后都会处理下剩下的元素。

而这道题是不能借助一个新的array的,那么我们就不好从前往后比了(不好插入位置)。方便的方法是从后往前比,然后最后处理剩下的元素。

代码如下:

     public void merge(int A[], int m, int B[], int n) {
        while(m > 0 && n > 0){
            if(A[m-1] > B[n-1]){
                A[m+n-1] = A[m-1];
                m--;
            }else{
                A[m+n-1] = B[n-1];
                n--;
            }
        }
 
        while(n > 0){
            A[m+n-1] = B[n-1];
            n--;
        }
    }

Merge Sorted Array leetcode java(回顾MergeTwoArray和MergeTwoLinkedList)的更多相关文章

  1. Merge Sorted Array——LeetCode

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

  2. Merge Sorted Array [LeetCode]

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

  3. Find Minimum in Rotated Sorted Array leetcode java

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...

  4. Remove Duplicates From Sorted Array leetcode java

    算法描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...

  5. Search in Rotated Sorted Array leetcode java

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...

  6. Median of Two Sorted Array leetcode java

    题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sor ...

  7. LeetCode算法题-Merge Sorted Array(Java实现)

    这是悦乐书的第161次更新,第163篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第20题(顺位题号是88).给定两个排序的整数数组nums1和nums2,将nums2中 ...

  8. [LeetCode] Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

  9. [LeetCode] 88. Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

随机推荐

  1. CSUOJ 1162 Balls in the Boxes 快速幂

    Description Mr. Mindless has many balls and many boxes,he wants to put all the balls into some of th ...

  2. 在静态方法中应用spring注入的类

    最近在一次项目的重构中,原项目需要在静态方法中调用service,现在需要更换框架,service需要自动注入,无法再静态方法中调用 解决思路: 创建一个当前类的静态变量,创建一个方法,使用@Post ...

  3. 【BZOJ 3470】3470: Freda’s Walk 期望

    3470: Freda’s Walk Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 42  Solved: 22 Description 雨后的Poet ...

  4. BZOJ 2115: [Wc2011] Xor 线性基 dfs

    https://www.lydsy.com/JudgeOnline/problem.php?id=2115 每一条从1到n的道路都可以表示为一条从1到n的道路异或若干个环的异或值. 那么把全部的环丢到 ...

  5. BZOJ 2466 [中山市选2009]树(高斯消元)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2466 [题目大意] 给定一棵树,每个节点有一盏指示灯和一个按钮.如果节点的按扭被按了, ...

  6. flex sqlite 操作blog 二进制数据

    1,              通常的操作方式: 首先我们建立表:CREATE TABLE "pages" ("id" varchar, "data& ...

  7. ZeptoLab Code Rush 2015 C. Om Nom and Candies 暴力

    C. Om Nom and Candies Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/526 ...

  8. css3实现卷页效果http://jingyan.baidu.com/article/73c3ce2806aef9e50343d93a.html

    css3实现卷页效果 | 浏览:31 | 更新:2015-01-08 13:30 1 2 3 4 5 6 7 分步阅读 百度经验:jingyan.baidu.com 页面上经常会看到鼠标移动上去,对象 ...

  9. html div 宽度随着浏览器自动适应

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. linux下mysql自动备份脚本

    脚本放在 /home/user/mysql_backup.sh crontab # crontab -l # m h  dom mon dow   command 28 16 * * * /home/ ...