题目

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. 【原创】Linux常用脚本

    #1.启用停用VIP sudo /etc/ha.d/resource.d/IPaddr 10.10.10.10 start sudo /etc/ha.d/resource.d/IPaddr 10.10 ...

  2. rabbitmq学习(二) —— helloword!

    rabbitmq学习当然是跟着官网走最好了,官网的教程写的很好,跟着官网教程走一遍就会有个初步了解了 下面的教程转自http://cmsblogs.com/?p=2768,该博客对官网的翻译还不错 介 ...

  3. JavaScript ES6箭头函数指南

    前言 胖箭头函数(Fat arrow functions),又称箭头函数,是一个来自ECMAScript 2015(又称ES6)的全新特性.有传闻说,箭头函数的语法=>,是受到了CoffeeSc ...

  4. 模拟Djangoweb框架

    一.需求 1.访问127.0.0.1/login,访问到login页面 2.登陆成功,跳转到登陆后的页面 3.登陆失败,跳转到登陆失败的页面 4.用户账号密码验证 二.目录结构 三.代码 day01. ...

  5. 优客365 v2.9版本 后台存在SQL注入

    安装 打开后台登陆界面 http://localhost:9096/yk365/system/login.php 输入单引号报错 得到表名 经过跟踪后在\module\login.php文件出现错误 ...

  6. PHP运行时强制显示出错信息

    调试很重要 error_reporting(E_ALL); ini_set('display_errors', '1'); 将出错信息输出到一个文本文件 ini_set('error_log', di ...

  7. .NET面试宝典-高级(一)

    1. DateTime.Parse(myString); 这段代码有什么问题? A:区域信息即CultureInfo没有指定.如果不指定的话,它将采用默认的机器级的设置(见:控制面板->区域和语 ...

  8. Linux给目录创建软链接的技巧

    ln -s /home/fei/workspace /var/www 注意:源目录和目标目录都必须是绝对路径

  9. php 利用fsockopen GET/POST 提交表单及上传文件

    1.GET get.php <?php $host = 'demo.fdipzone.com'; $port = 80; $errno = ''; $errstr = ''; $timeout  ...

  10. max_binlog_cache_size

    http://blog.mimvp.com/2017/07/mysql-yi-ge-can-shu-yin-qi-de-dang-ji-xue-an/ max_binlog_cache_size 表示 ...