Old discuss is read-only now. Please go to New LeetCode Discuss for your questions and answers!

User account in old discuss will not integrate to the new one, but new discuss is integrated with new online judge, which means, if you already have an account in new online judge, you can access new discuss immediately!

If you want to ask for question relevant to the posts in old discuss, please copy content as part of your question, only old discuss link is NOT ALLOWED!

Please read the FAQ in new LeetCode Discuss to help yourself making the best use of Discuss!

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.

asked 20 May '12, 16:19

1337c0d3r's gravatar image

1337c0d3r ♦♦
1.2k1184171
accept rate: 2%

closed 25 Oct, 06:41

The question has been closed for the following reason "bulk close" by 1337c0d3r 25 Oct, 06:41


12next »

public class Solution {

public void merge(int A[], int m, int B[], int n) {

    while(n>0){
        if(m <=0 || A[m-1] < B[n-1])
            A[n+m-1] = B[--n];
        else
            A[n+m-1] = A[--m];
    }
}

}

link

answered 17 Jan, 13:10

nipoleon's gravatar image

nipoleon
6113
accept rate: 0%

edited 17 Jan, 13:10

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

This version is a litter longer , used more words, but more clear. Cause the ambiguous --m is replaced.
A[n+m-1]=A[--m] is more likely to be interpreted as
two sentences
--m;
A[n+m-1]=A[m];

(26 Sep, 04:05) c0mm3t c0mm3t's gravatar image
class Solution {
public:
    void merge(int A[], int m, int B[], int n) {
        int pos = m + n -1;
        int a = m - 1;
        int b = n - 1;
        while(a>=0 && b>=0) A[pos--] = A[a]>B[b] ? A[a--] : B[b--];
        while(b>=0) A[pos--] = B[b--];      
    }
};
link

answered 23 Mar, 00:54

yangt12321's gravatar image

yangt12321
313
accept rate: 0%

edited 23 Mar, 00:58

public void merge(int A[], int m, int B[], int n) {
    m--;
    n--;
    for (int i = A.length - 1; i >= 0; i--) {
        if (m >= 0 && n >= 0) {
            if (A[m] > B[n]) {
                A[i] = A[m--];
            } else {
                A[i] = B[n--];
            }
        } else if (m >= 0) {
            A[i] = A[m--];
        } else {
            A[i] = B[n--];
        }
    }
}
link

answered 31 Dec '12, 17:12

fantasist's gravatar image

fantasist
262
accept rate: 0%

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

answered 09 Jan, 15:49

bridger's gravatar image

bridger
23113
accept rate: 0%

class Solution {
public:
void merge(int A[], int m, int B[], int n) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
if (n==0)
    return;
int mergePos = m + n - 1;
int i = m - 1;
int j = n - 1;
while (i>=0 && j>=0)
{
    if (A[i] > B[j])
        A[mergePos--] = A[i--];
    else
        A[mergePos--] = B[j--];
}
while (i>=0)
    A[mergePos--] = A[i--];
while (j>=0)
    A[mergePos--] = B[j--];
}
};
link

answered 10 Jan, 21:01

BlackMamba's gravatar image

BlackMamba
11124
accept rate: 0%

edited 10 Jan, 21:02

要学会逆向思维,从反面考虑问题!

(10 Jan, 21:02) BlackMamba BlackMamba's gravatar image
    int j = m + n -1;
    while( m!= 0 && n != 0)
    {
        if( A[m-1] < B[n-1])
        {
            A[j] = B[n-1];
            j--;
            n--;
        } 
        else
        {
            A[j] = A[m-1];
            j--;
            m--;
        }
    }
    while( m != 0)
    {
        A[j] = A[m-1];
        j--;
        m--;
    }
    while( n != 0)
    {
        A[j] = B[n-1];
        j--;
        n--;
    }
link

answered 11 Jan, 09:02

spooky's gravatar image

spooky
124
accept rate: 0%

tedious expression, what a shame

(11 Jan, 09:04) spooky spooky's gravatar image
void merge(int A[], int m, int B[], int n) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    int last = m+n-1;
    int i=m-1, j=n-1;
    while (i>=0 && j>=0) {

        if (A[i] > B[j]) A[last--]=A[i--];
        else A[last--] = B[j--];
    }
    while (j>=0) A[last--] = B[j--];
}
link

answered 14 Mar, 08:51

Shengzhe%20Chen's gravatar image

Shengzhe Chen
1124
accept rate: 4%

/*
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.
*/

#include <iostream>
#include <vector>
#include <climits>

using namespace std;

//Do it backward
void MergeSortedArray(int *A, int m, int *B, int n){
    int posA = m - 1, posB = n - 1, pos = m + n - 1, swap = 0;
    for (; pos >= 0; --pos){
        if (A[posA] > B[posB]){
            A[pos] = A[posA];
            posA--;
        }
        else if (A[posA] < B[posB]){
            A[pos] = B[posB];
            posB--;
        }
        else{
            A[pos] = A[--pos] = A[posA];
            posA--; posB--;
        }
    }
}

//Do it in-place
int main(){
    int A[20] = {1,3,5,6,12,13,15,16,27,32};
    int B[5]  = {2,8,14,24,44};
    MergeSortedArray(A, 10, B, 5);
    for (int i = 0; i < (15); ++i){
        cout << A[i] << ' ';
    }
    cout << endl;
    return 0;
}
link

answered 24 Mar, 15:02

FH86's gravatar image

FH86
213
accept rate: 0%

void merge(int A[], int m, int B[], int n) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    int a, b;
    for (a = m-1, b = n-1; a >= 0 && b >= 0;) {
        if (A[a] >= B[b]) {
            A[a+b+1] = A[a];
            a--;
        } else {
            A[a+b+1] = B[b];
            b--;
        }
    }

    if (b >= 0) {
        for (int i = 0; i <= b; i++) {
            A[i] = B[i];
        }
    }
}
link

answered 21 Apr, 00:18

whitebluff's gravatar image

whitebluff
212
accept rate: 0%

public class Solution { public void merge(int A[], int m, int B[], int n) {

    int posA = m-1, posB = n-1;
    boolean selectA;
    for(int i = m+n-1;i>=0;i--){
    if(posB<0||posA < 0)   selectA = (posB < 0)          ? true:false;
    else                   selectA = (A[posA] > B[posB]) ? true:false;         
    A[i] = selectA ? A[posA] : B[posB];
    posA = selectA ? posA-1  : posA;
    posB = selectA ? posB    : posB-1;
    }

}

link

answered 04 Jun, 11:41

lfgao's gravatar image

lfgao
1
accept rate: 0%

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • Indent code by 4 spaces.
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×134

Asked: 20 May '12, 16:19

Seen: 2,584 times

Last updated: 25 Oct, 06:41