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 nrespectively.

Solution1:新建ArrayList暂存merged list,后放回A

public class Solution {
public void merge(int A[], int m, int B[], int n) {
int length = m+n;
ArrayList<Integer> merge = new ArrayList<Integer>();
int posA =0, posB=0, i=0;
while( i<length){
if(posB>=n){merge.add(A[posA]); posA++; i++;}
else if(posA>=m){ merge.add(B[posB]); posB++; i++;}
else if(A[posA]<=B[posB]){merge.add(A[posA]); posA++; i++;}
else{merge.add(B[posB]); posB++; i++;}
}
for(i=0; i<length;i++){
A[i] = merge.get(i);
}
}
}

Solution2:可不能够不额外开辟空间?利用倒序。

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

Solution1:再简单点?

public class Solution {
public void merge(int A[], int m, int B[], int n) { for(int i=m+n-1; i>=0; i--) A[i]=((m>0)&&(n==0 || A[m-1]>=B[n-1]))?A[--m]:B[--n]; }
}

第十二题 Merge Sorted Array的更多相关文章

  1. [LC]88题 Merge Sorted Array (合并两个有序数组 )

    ①英文题目 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. N ...

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

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

  3. LeetCode练题——88. Merge Sorted Array

    1.题目 88. Merge Sorted Array——Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into  ...

  4. [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 ...

  5. 【LeetCode练习题】Merge Sorted Array

    Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...

  6. 【LeetCode】88. Merge Sorted Array (2 solutions)

    Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...

  7. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

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

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

  9. 43. Merge Sorted Array && LRU Cache

    Merge Sorted Array OJ: https://oj.leetcode.com/problems/merge-sorted-array/ Given two sorted integer ...

随机推荐

  1. POJ-1087 二分图匹配,最大流。

                                                      A Plug for UNIX 题意很迷,不过很水. 题意:一个房间有m个插座,每个插座有一个型号, ...

  2. selenium webdriver——元素定位

    元素定位: >>WebDriver提供了八种元素定位方法,在Python语言中,所对应的方法如下: >>id属性定位:有唯一性 find_element_by_id(" ...

  3. ruby 发送邮件

    产品构建.打包.部署完需要发送邮件通知相关人员进行验证.请看过程 #encoding:utf-8 require 'mail' #~ branch = ARGV.to_s.sub('[','').su ...

  4. Bolzano-Weierstrass 定理

    这个定理是从吴崇试老师的数学物理方法课里看到的,表述如下: 有界的无穷(复数)序列至少有一个聚点. 序列的聚点定义为 给定序列 $\{z_n\}$,若存在复数 $z$,对于任意给定的 $\vareps ...

  5. ZOJ 3780 Paint the Grid Again(隐式图拓扑排序)

    Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N × N cel ...

  6. matplotlib pyplot 中文显示问题

    import pylab pylab.mpl.rcParams['font.sans-serif'] = ['SimHei'] pylab.mpl.rcParams['axes.unicode_min ...

  7. .bat 批处理

    最简单的一个批处理文件 @echo off echo 这是测试内容1 echo 这是测试内容2 pause 输出: 这是测试内容1 这是测试内容2 请按任意键继续. . .

  8. JavaScript (JS)基础:DOM 浅析 (含数组Array、字符串String基本方法解析)

    ①文本对象document: 例如:document.getElementById()    只获取一个对象          document.getElementsByTagName()   获取 ...

  9. HDOJ Important Sisters

    Important Sisters Time Limit: 7000/7000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  10. 【CF1043A】Elections(签到)

    题意:给定n个数字,第i个为a[i],求使得sigma k-a[i]>sigma a[i]最小的k n,a[i]<=1e2 思路: #include<cstdio> #incl ...