LeetCode Merge two Sorted Array
2012-11-19 02:21
459 查看
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.
Solution:
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
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[n-1]=B[n-1];
--n;
}
}
};
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:
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
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[n-1]=B[n-1];
--n;
}
}
};
相关文章推荐
- LeetCode4/88 Median of Two Sorted Arrays/Merge Sorted Array
- LeetCode之Merge two sorted array
- Merge Sorted Array leetcode java(回顾MergeTwoArray和MergeTwoLinkedList)
- LeetCode|Merge Two Sorted Lists/Merge Sorted Array
- [leetcode][array][two pointers] Merge Sorted Array
- leetcode 088 —— Merge Sorted Array
- LeetCode Merge Two Sorted Lists
- LeetCode Merge Two Sorted Lists
- [leetcode] Merge Sorted Array
- LeetCode Merge Sorted Array
- Leetcode 21. Merge Two Sorted Lists
- 【Leetcode】Merge Two Sorted Lists (2 lists)
- leetcode 29: Merge Sorted Array
- leetcode刷题日记——Merge Two Sorted Lists
- LeetCode No.21 MergeTwoSortedLists
- Leetcode - Array - Merge Sorted Array
- LeetCode 88 Merge Sorted Array
- [Leetcode] Merge Two Sorted Lists
- LeetCode | Merge Two Sorted Lists(合并两个链表)
- LeetCode 第 21 题 (Merge Two Sorted Lists)