[刷题]Merge Sorted Array

来源:互联网 发布:淘宝上的好评怎么写 编辑:程序博客网 时间:2024/06/07 06:16

[LintCode]Merge Sorted Array

class Solution {    /**     * @param A and B: sorted integer array A and B.     * @return: A new sorted integer array     */    public ArrayList<Integer> mergeSortedArray(ArrayList<Integer> A, ArrayList<Integer> B) {        // 2015-4-12        ArrayList<Integer> rst = new ArrayList<>();        if (A == null && B == null) {            return rst;        } else if (A == null || A.size() == 0) {            return B;        } else if (B == null || B.size() == 0) {            return A;        }                int index1 = 0;        int index2 = 0;                while (index1 < A.size() && index2 < B.size()) {            if (A.get(index1) <= B.get(index2)) {                rst.add(A.get(index1));                index1++;            } else {                rst.add(B.get(index2));                index2++;            }        }                while (index2 < B.size()) {            rst.add(B.get(index2));            index2++;        }        while (index1 < A.size()) {            rst.add(A.get(index1));            index1++;        }        return rst;            }}


0 0
原创粉丝点击