LeetCode:Merge Sorted Array
2014-03-06 21:08
375 查看
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 and n 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 and n respectively.
class Solution { public: void merge(int A[], int m, int B[], int n) { int k = m+n-1; int i = m-1; int j = n -1; while(k >= 0 ){ if(i < 0){ A[k--] = B[j--]; } else if(j < 0){ A[k--] = A[i--]; }else{ if(A[i] > B[j]){ A[k--] = A[i--]; }else{ A[k--] = B[j--]; } } } } };
相关文章推荐
- LeetCode 之 Merge Sorted Array(排序)
- leetcode:Merge Sorted Array 5行代码AC
- [LeetCode] Merge Sorted Array
- LeetCode 88 Merge Sorted Array
- [LeetCode 088] Merge Sorted Array
- leetcode: Merge Sorted Array O(n)解法
- 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——88——Merge Sorted Array
- LeetCode:Merge Sorted Array
- [LeetCode]Merge Sorted Array
- LeetCode Merge two Sorted Array
- LeetCode (1) Merge Sorted Array
- [LeetCode] Merge Sorted Array
- LeetCode | Merge Sorted Array
- [leetcode]Merge Sorted Array