forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_Sorted_Array.cpp
More file actions
44 lines (40 loc) · 954 Bytes
/
Merge_Sorted_Array.cpp
File metadata and controls
44 lines (40 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
Author: Weixian Zhou, ideazwx@gmail.com
Date: Jul 6, 2012
Problem: Merge Sorted Array
Difficulty: medium
Source: http://www.leetcode.com/onlinejudge
Notes:
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.
Solution:
This problem should be solved without a third array.
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
using namespace std;
class Solution {
public:
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) {
A[k--] = (A[i] > B[j] ? A[i--] : B[j--]);
}
while (i >= 0) {
A[k--] = A[i--];
}
while (j >= 0) {
A[k--] = B[j--];
}
}
};