首页 > 其他 > 详细

Merge Sorted Array -- leetcode

时间:2015-04-25 12:16:44      阅读:201      评论:0      收藏:0      [点我收藏+]

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

Merge Sorted Array -- leetcode

原文:http://blog.csdn.net/elton_xiao/article/details/45268641

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!