题解 | #合并两个有序的数组#

合并两个有序的数组

https://www.nowcoder.com/practice/89865d4375634fc484f3a24b7fe65665

using System;

using System.Collections.Generic;

class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param A int整型一维数组
     * @param m int整型
     * @param B int整型一维数组
     * @param n int整型
     * @return void
     */
    public void merge (int[] A, int m, int[] B, int n) {
        // write code here
        List<int> res = new List<int>();
        int i = 0, j = 0;
        for (; i < m && j < n;) {
            if (A[i] > B[j]) {
                res.Add( B[j]);
                ++j;
            } else {
                res.Add(A[i]);
                ++i;
            }
        }
        while (i < m) {
            res.Add(A[i++]);
        }
        while (j < n) {
            res.Add(B[j++]);
        }
        for ( i = 0; i < res.Count; i++) {
            A[i] = res[i];
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务