Merge Sorted Array -- leetcode

来源:互联网 发布:大数据mobi 编辑:程序博客网 时间:2024/06/15 05:45

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 nrespectively.


从A数组的后面往前填充。省掉数组插入时的移动操作。


class Solution {public:    void merge(int A[], int m, int B[], int n) {        while (n) {            A[m+n-1] = (!m || A[m-1]<B[n-1]) ? B[--n] : A[--m];        }    }};


代码参考自:

https://leetcode.com/discuss/30859/1-line-solution

0 0
原创粉丝点击