LeetCode OJ - Merge Sorted Array

原地归并。

下面是AC代码:


 1  public void merge(int A[], int m, int B[], int n) {
2
3 int len = A.length;
4 //first copy m elements of A to the end of A
5 for(int i=m-1,j = len-1;i>=0;i--){
6 A[j--] = A[i];
7 }
8 //merge the A and B
9 int startA = len - m;
10 int startB = 0;
11 int i = 0;
12 while(startA<len || startB<n){
13 while(startA<len&& startB<n && A[startA]<=B[startB])
14 {
15 A[i++] = A[startA];
16 startA++;
17 }
18 while(startA<len && startB<n && B[startB]<A[startA]){
19 A[i++] = B[startB];
20 startB++;
21 }
22 while(startA == len && startB<n){
23 A[i++] = B[startB++];
24 }
25 while(startB == n && startA<len){
26 A[i++] = A[startA++];
27 }
28 }
29 }


 1 /**
2 * second method very nice . from the end to start
3 * @param A
4 * @param m
5 * @param B
6 * @param n
7 */
8 public void merge2(int A[], int m, int B[], int n){
9 int i = m+n-1;
10 while(i>=0 &&( n>=0 || m>=0)){
11 A[i--] = m-1 < 0? B[(n--)-1]:
12 n-1 < 0? A[(m--)-1] : A[m-1]>=B[n-1]? A[(m--)-1]:B[(n--)-1];
13 }
14 }

LeetCode OJ - Merge Sorted Array

时间: 2024-12-23 13:08:42

LeetCode OJ - Merge Sorted Array的相关文章

&lt;LeetCode OJ&gt; Merge Sorted Array【88】

88. Merge Sorted Array My Submissions Question Total Accepted: 80830 Total Submissions: 274120 Difficulty: Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough sp

LeetCode --- 88. Merge Sorted Array

题目链接:Merge Sorted Array 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 init

LeetCode OJ - Convert Sorted Array/List to Binary Search Tree

虚函数使用的时机 为什么虚函数不总是适用? 1. 虚函数有事会带来很大的消耗: 2. 虚函数不总是提供所需的行为: 3. 当我们不考虑继承当前类时,不必使用虚函数. 必须使用虚函数的情况: 1. 当你想删除一个表面上指向基类对象,实际却是指向派生类对象的指针,就需要虚析构函数. LeetCode OJ - Convert Sorted Array/List to Binary Search Tree,布布扣,bubuko.com LeetCode OJ - Convert Sorted Arra

【leetcode】Merge Sorted Array

Merge Sorted Array 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 initialize

leetCode 88. Merge Sorted Array 有序数组

88. Merge Sorted Array Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The n

LeetCode (1) Merge Sorted Array

题目: 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 to hold additional elements from B. The number of elements initialized in A and B are m and n respectively. 刚开始根据归并排序写的多了一个中

LeetCode:Merge Sorted Array

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

leetcode 【 Merge Sorted Array 】python 实现

题目: 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 ar

LeetCode 88. Merge Sorted Array(合并有序数组)

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initi