zoukankan      html  css  js  c++  java
  • 两个有序list合并

    package 剑指office;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class ListMerge {
        /**
         * 两个已顺序排序数组的合并
         * 
         * @param aList
         * @param bList
         * @return
         */
        public static List<Integer> mergeTwoSortList(List<Integer> aList, List<Integer> bList) {
            int aLength = aList.size(), bLength = bList.size();
            List<Integer> mergeList = new ArrayList<Integer>();
            int i = 0, j = 0;
            while (aLength > i && bLength > j) {
                if (aList.get(i) > bList.get(j)) {
                    mergeList.add(i + j, bList.get(j));
                    j++;
                } else {
                    mergeList.add(i + j, aList.get(i));
                    i++;
                }
            }
            // blist元素已排好序, alist还有剩余元素
            while (aLength > i) {
                mergeList.add(i + j, aList.get(i));
                i++;
            }
            // alist元素已排好序, blist还有剩余元素
            while (bLength > j) {
                mergeList.add(i + j, bList.get(j));
                j++;
            }
            return mergeList;
    
        }
    
        public static void main(String[] args) {
            List<Integer> aList = new ArrayList<Integer>();
            ;
            aList.add(3);
            aList.add(6);
            aList.add(7);
            aList.add(10);
            aList.add(13);
            List<Integer> bList = new ArrayList<Integer>();
            bList.add(1);
            bList.add(2);
            bList.add(5);
            bList.add(12);
            bList.add(15);
            bList.add(20);
            bList.add(21);
            List<Integer> mergeList = mergeTwoSortList(aList, bList);
    
            System.err.println(mergeList.toString());
        }
    }
  • 相关阅读:
    linux 首次登陆与线上求助
    003生信人必练
    计算器概论
    01 git 概念
    01 基因组学基本感念
    Python 函数习题
    Python字符编码详解,str,bytes
    python class
    [Leetcode]287. Find the Duplicate Number
    深度解析Word2vec
  • 原文地址:https://www.cnblogs.com/chengpeng15/p/5804581.html
Copyright © 2011-2022 走看看