Leetcode - Array - Merge Sorted Array
2014-08-17 19:56
441 查看
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 andn respectively.
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 andn respectively.
class Solution { public: void merge(int A[], int m, int B[], int n) { if(A==NULL||m<0||B==NULL||n<0) return; reverse(A,A+m+n); reverse(A+n,A+m+n); int indexA=n,indexB=0,index=0; while(indexA<m+n&&indexB<n) { if(A[indexA]<=B[indexB]) A[index++]=A[indexA++]; else A[index++]=B[indexB++]; } while(indexB<n) A[index++]=B[indexB++]; } };
相关文章推荐
- [LeetCode] Merge Sorted Array
- Merge Sorted Array:from LeetCode
- LeetCode88 Merge Sorted Array
- LeetCode | Merge Sorted Array
- LeetCode - Merge Sorted Array
- Leetcode_Merge Sorted Array
- LeetCode(88)Merge Sorted Array
- Leetcode之Merge Sorted Array 问题
- LeetCode-Merge Sorted Array
- LeetCode - Merge Sorted Array
- LeetCode - Merge Sorted Array
- [LeetCode]Merge Sorted Array
- LeetCode——Merge Sorted Array
- LeetCode@Array_88_MergeSortedArray
- leetcode----Merge Sorted Array
- Leetcode: Merge Sorted Array
- LeetCode Merge Sorted Array
- Leetcode_c++: Merge Sorted Array (088)
- Leetcode-Merge Sorted Array
- Leetcode: Merge Sorted Array