leetcode 刷题之路 37 Merge Sorted Array
2014-08-01 16:02
399 查看
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.
数组的归并排序,为了避免大量多次移动数据,采用从后向前的方式对数组进行归并。
accepted answer:
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.
数组的归并排序,为了避免大量多次移动数据,采用从后向前的方式对数组进行归并。
accepted answer:
class Solution { public: void merge(int A[], int m, int B[], int n) { int i = m-1, j = n-1; int k = m + n - 1; while (i >= 0 && j >= 0) { if (A[i] > B[j]) A[k--] = A[i--]; else A[k--] = B[j--]; } while (i >= 0) A[k--] = A[i--]; while (j >= 0) A[k--] = B[j--]; } };
相关文章推荐
- Leetcode 第88题 Merge Sorted Array
- leetcode:Merge Sorted Array 5行代码AC
- 【LeetCode】Merge Sorted Array ---合并数组
- LeetCode-Merge Sorted Array
- 【题解】【数组】【Leetcode】Merge Sorted Array
- leetcode -- Merge Sorted Array -- 简单重点
- LeetCode 88 Merge Sorted Array(合并排序数组)(*)
- Leetcode 88 Merge Sorted Array
- LeetCode88 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 88 Merge Sorted Array C++
- LeetCode "Merge Sorted Array"
- leetcode Merge Sorted Array 合并有序数组
- Leetcode-88 Merge Sorted Array