题目

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驱动之内存访问

    <背景> 内存会以分页方式组织内存,而且每页大小和计算机体系结构有关系,Linux中每个页都有对应的struct page{} 与之对应.                         ...

  2. JS AngualrJs 指令

    本文基于 AngularJs 1.x 版本 内置指令 布尔属性 根据HTML标准的定义,布尔属性代表一个 true 或 false 值. 当这个属性出现时,这个属性的值就是 true (无论实际定义的 ...

  3. pygame系列_pygame的各模块叙述

    在pygame中,有很多模块,每个模块对应着不同的功能,如果我们知道这些模块是做什么的,那么,对我们的游戏开发会起到关键性的作用. 我们就说说pygame中的各个模块吧!!! #pygame modu ...

  4. SPOJ 10234. Here Be Dragons

    The Triwizard Tournament's third task is to negotiate a corridor of many segments, and reach the oth ...

  5. bestcoder#23 1001 Sequence

    Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. ACdream 速攻组~

    1007 a + b /*这题就是一个快速幂,但是十分猥琐的是,模是1e10 + 7,不是1e9 + 7,这就产生了一个爆long long的问题.所以要对快速幂中的乘法操作进行一下改造.请教了BIT ...

  7. JQ中get()与eq()的区别

    .eq() : 减少匹配元素的集合,根据index索引值,精确指定索引对象. .get() : 通过检索匹配jQuery对象得到对应的DOM元素. 同样是返回元素,那么eq与get有什么区别呢? eq ...

  8. Notepad++源代码阅读——窗口封装与继承

    引言 近期在看Notepad++的源代码,学习学习Win32 原生API的开发技巧. 本文以Notepad++ 1.0版本的源代码为例讲解如何封装windows窗口,实现面向对象开发,如何通过窗口的继 ...

  9. [Node.js]Path模块

    摘要 path模块提供了一些处理文件路径问题的工具. path模块 引入模块 var path=require("path"); 方法 1 path.normalize(p)规范化 ...

  10. Access-Control-Allow-Origin,跨域

    1.浏览器的同源安全策略 浏览器只允许请求当前域的资源,而对其他域的资源表示不信任.那怎么才算跨域呢? 请求协议http,https的不同 域domain的不同 端口port的不同 好好好,大概就是这 ...