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());
}
}

两个有序list合并的更多相关文章

  1. 两个有序数组合并成一个有序数组(要求时间复杂度为O(n))

    面试题: 怎样把两个有序数组合并成有序数组呢 逻辑步骤: 1.假设两个数组为A和B 2.A和B都是从小到大的顺序进行排列 ** 1.我们可以直接比较两个数组的首元素,哪个小就把这个小元素放入可变数组. ...

  2. Ex 2_22 两个有序列表合并后的第k小元素..._第四次作业

    package org.xiu68.ch02; public class Ex2_22 { public static void main(String[] args) { // TODO Auto- ...

  3. Python3将两个有序数组合并为一个有序数组

    [本文出自天外归云的博客园] 第一种思路,把两个数组合为一个数组然后再排序,问题又回归到冒泡和快排了,没有用到两个数组的有序性.(不好) 第二种思路,循环比较两个有序数组头位元素的大小,并把头元素放到 ...

  4. 两个有序数组合并为一个有序数组---python

    def merge(a, b): """ 合并2个有序数组,默认a,b都是从小到大的有序数组 """ # 1.临时变量 i, j = 0, ...

  5. 求两个有序序列合并成新有序序列的中位数,求第k小数

    此算法涉及一个重要数学结论:如果A[k/2-1]<B[k/2-1],那么A[0]~A[k/2-1]一定在第k小的数的序列当中,可以用反证法证明. 算法思想如下: 1,假设A长度为m,B长度为n, ...

  6. iOS常用算法之两个有序数组合并, 要求时间复杂度为0(n)

    思路: 常规思路: 先将一个数组作为合并后的数组, 然后遍历第二个数组的每项元素, 一一对比, 直到找到合适的, 就插入进去; 简单思路: 设置数组C, 对比A和B数组的首项元素, 找到最小的, 就放 ...

  7. [LeetCode] 21. 合并两个有序链表

    题目链接:https://leetcode-cn.com/problems/merge-two-sorted-lists/ 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定 ...

  8. [Swift]LeetCode21. 合并两个有序链表 | Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  9. 合并两个有序链表的golang实现

    将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 输入:->->, ->-> 输出:->->->->-> ...

随机推荐

  1. GitHub以及Git学习 持续编辑学习中

    官网地址: http://www.worldhello.net/gotgithub/01-explore-github/030-explore-github.html 1 加入github, http ...

  2. linux 内核源代码分析 - 获取数组的大小

    #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 測试程序: #include<stdio.h> #include<stdlib. ...

  3. 【概率DP入门】

    http://www.cnblogs.com/kuangbin/archive/2012/10/02/2710606.html 有关概率和期望问题的研究 摘要 在各类信息学竞赛中(尤其是ACM竞赛中) ...

  4. 计算(a/b)%c

    如果b与c互素,则(a/b)%c=a*b^(phi(c)-1)%c 如果b与c不互素,则(a/b)%c=(a%bc)/b 对于b与c互素和不互素都有(a/b)%c=(a%bc)/b成立

  5. 纯CSS绘制三角形

    扒segmentfault的导航栏时候发现的,用了个span标签写了个三角形出来,第一次发现,好神奇,查了下还有挺多种玩法的.基本的用法就是将盒子的width和height设为0,然后用border搭 ...

  6. Oracle游标cursor1基础和隐式游标

    --指向表行的指针,一次一行,一般向前移动 Resultset --游标永远代代表的是一行数据. /* 使用步骤 第一步:声明游标,就像是声明一个变量样. 游标的关键字就是cursor. Declar ...

  7. ZOJ 1530 - Find The Multiple

    Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal repr ...

  8. oracle12c及PLSQL Developer安装全程记录

    一.登陆oracle下载页面  http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html 下 ...

  9. Android 判断听云是否嵌入正确

    编译打包成apk之后,将apk在手机上进行安装,连接数据线,打开命令行,输入以下命令: adb logcat -v time -s NBSAgent:V 之后运行嵌入听云代码的app,进行有效的网络访 ...

  10. VMware的CentOS无法上网的解决方法

    1)点击 VM->Settings Hardware 选项卡下面 2)点击 Network Adapter 设置在虚拟机中将网络配置设置成NAT 3)开启 Windows服务中的 VMware ...