首页 > 其他 > 详细

leetcode Merge Sorted Array 2.19 难度系数2

时间:2014-01-27 19:59:53      阅读:354      评论:0      收藏:0      [点我收藏+]

Question:

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 to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.

public class Solution {
    public void merge(int A[], int m, int B[], int n) {
        int lena = m-1;
        int lenb = n-1;
        int lenres = m+n-1;
        while(lena>=0&&lenb>=0){
        	if (A[lena]>=B[lenb]) {
        		A[lenres--] = A[lena--];
			}else {
				A[lenres--] = B[lenb--];
			}
        }
        while(lena>=0){
        	A[lenres--] = A[lena--];
        }
        while(lenb>=0){
        	A[lenres--] = B[lenb--];
        }
    }
}


leetcode Merge Sorted Array 2.19 难度系数2

原文:http://blog.csdn.net/yiding_he/article/details/18812159

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