[LeetCode] Merge Sorted Array
2013-11-10 14:36
267 查看
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.
解题思路:
额外开了空间,不开也可以,但是会比较麻烦。
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.
解题思路:
额外开了空间,不开也可以,但是会比较麻烦。
class Solution { public: void merge(int A[], int m, int B[], int n) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int *C = new int[m + n + 1]; int a = 0, b = 0; for(int i = 0;i < m + n;i++) { if(a < m) { if(b < n) { if(A[a] < B[b]) C[i] = A[a++]; else C[i] = B[b++]; } else C[i] = A[a++]; } else { if(b < n) C[i] = B[b++]; else break; } } for(int i = 0;i < m + n;i++) A[i] = C[i]; } };
相关文章推荐
- Leetcode: Merge Sorted Array
- 【leetcode】Merge Sorted Array
- leetcode [Merge Sorted Array]
- [LeetCode] Merge Sorted Array
- leetcode — merge-sorted-array
- Leetcode_Merge Sorted Array
- Merge Sorted Array:from LeetCode
- Leetcode解题笔记-Merge sorted Array
- LeetCode Online Judge 题目C# 练习 - Merge Sorted Array
- LeetCode 88 --- Merge Sorted Array
- Leetcode 88 Merge Sorted Array
- [LeetCode]Merge Sorted Array
- 【LeetCode】88 - Merge Sorted Array
- [leetcode]Merge Sorted Array
- LeetCode--No.88--Merge Sorted Array
- leetcode: Merge Sorted Array
- (java)leetcode-88:Merge Sorted Array
- LeetCode Merge Sorted Array 合并已排序的数组
- leetcode 88 Merge Sorted Array
- [LeetCode]Merge Sorted Array