第七章第三十一题(合并两个有序列表)(Merge two ordered tables)
#第七章第三十一题(合并两个有序列表)(Merge two ordered tables)
**7.31(合并两个有序列表)编写下面的方法,将两个有序列表变成一个新的有序列表。
public static int[] merge(int[] list1,int[] list2)
只进行list.length+list2.length次比较来实现该方法。该实现的动画演示参见。编写一个测试程序,提示用户输入两个有序列表,然后显示合并后的列表。下面是一个运行示例。注意,输入的第一个数字表示列表元素的个数。该数字不是列表的一部分。
Enter list1 size and contents:5 1 5 16 61 111
Enter list2 size and contents:4 2 4 5 6
1 2 4 5 5 6 16 61 111
*7.31(Merge two ordered tables)Write the following method to change two ordered tables into a new ordered list.
public static int[] merge(int[] list1,int[] list2)
Only list.length +List2. Length times to implement the method. Animation of the implementation [for demonstration, see]( http://liveexample.pearsoncmg.com/dsanimation/MergeSortNeweBook.html )。 Write a test program, prompt the user to input two sequential tables, and then display the merged list. Here is a running example. Note that the first number you enter represents the number of list elements. The number is not part of the list.
Enter list1 size and contents:5 1 5 16 61 111
Enter list2 size and contents:4 2 4 5 6
1 2 4 5 5 6 16 61 111参考代码:
package chapter07;import java.util.Arrays; import java.util.Scanner;public class Code_31 {public static void main(String[] args) {Scanner input=new Scanner(System.in);System.out.print("Enter list1 size and contents:");int length1=input.nextInt();int[] list1=new int[length1];for(int i=0;i<list1.length;i++){list1[i]=input.nextInt();}System.out.print("Enter list2 size and contents:");int length2=input.nextInt();int[] list2=new int[length2];for(int i=0;i<list2.length;i++){list2[i]=input.nextInt();}for(int i=0;i<merge(list1,list2).length;i++)System.out.print(merge(list1,list2)[i]+" ");}public static int[] merge(int[] list1,int[] list2){int[] list3=new int[list1.length+list2.length];for(int i=0;i<list1.length;i++){list3[i]=list1[i];}for(int i=list1.length,j=0;i<list1.length+list2.length;i++,j++){if(j>list2.length)break;list3[i]=list2[j];}Arrays.sort(list3);return list3;} }
结果显示:
Enter list1 size and contents:5 1 5 16 61 111 Enter list2 size and contents:4 2 4 5 6 1 2 4 5 5 6 16 61 111 Process finished with exit code 0
第七章第三十一题(合并两个有序列表)(Merge two ordered tables)相关推荐
- 《LeetCode力扣练习》第21题 合并两个有序链表 Java
<LeetCode力扣练习>第21题 合并两个有序链表 Java 一.资源 题目: 将两个升序链表合并为一个新的 升序 链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例 ...
- python合并两个有序列表(list)
python合并两个有序列表(list) 原来的列表是有序的合并之后的列表也要是有序的. 很多时候,我们会遇到一个问题,我们希望使用归并排序的归并函数,这是一个经典问题,在竞争性编程中经常出现.这种类 ...
- Python合并两个有序列表
提出要求: 合并两个有序列表 例如: L1=[23,45,67,77,78,80,90] L2=[4,11,26,33,42,61,80] 解决方法: 方法1:使用Python自带的extend()方 ...
- 编写下面的函数合并两个有序列表构成一个新的有序列表: def merge(list1,list2): 编写测试程序提示用户输入两个有序列表,然后显式合并后的有序列表。
题目内容:编写下面的函数合并两个有序列表构成一个新的有序列表:def merge(list1,list2):编写测试程序提示用户输入两个有序列表,然后显式合并后的有序列表.要求:不允许使用系统提供的s ...
- 两个数组合并排序python_python算法题---合并两个有序数组
leetcode-python-合并两个有序数组 今天第一次电话面试,有点紧张,所以自己感觉表现的不是很好,面试过程中面试官问到了两个算法题,都是leecode上面的题目,而且我都做过,但是当时一下子 ...
- 牛客网刷题-合并两个有序数组
前言: 想要学好嵌入式,C语言与数据结构是必要熟练掌握的,而想熟练掌握一门语言,必须经过大量的练习,刷题,至少需要一两万行的代码量,才能具有一定的编程能力,至少拿到一个功能,怎么去用编程语言去实现它, ...
- LeetCode题组:第21题-合并两个有序链表
1.题目:合并两个有序链表 将两个升序链表合并为一个新的升序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出: ...
- 两个有序链表序列的交集_腾讯50题---合并两个有序链表(简单)
5.1 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1-&g ...
- python实现合并两个有序列表
题目描述 将两个升序链表合并为一个新的升序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode原题地址:https://leetcode-cn.com/problems/me ...
最新文章
- vs2019怎么调整字体大小_考研倒计时23天,答题卡怎么使用?来看看正确的使用方法吧!...
- android apr分析,APR分析信号篇
- “约见”面试官系列之常见面试题之第八十五篇之css响应式(建议收藏)
- 作者:王腾蛟(1973-),男,北京大学信息科学技术学院教授、博士生导师,北京大学文理大数据研究中心常务副主任。...
- mysql 连接差异_和你一起从 0 实现一个数据库差异对比工具
- JS特效代码大全(十一)超炫的js图片展示效果(三)
- Kafka中的配置项参数unclean.leader.election.enable
- 视觉SLAM十四讲_3-李群和李代数
- Elasticsearch 系列4 --- Windows10安装Kibana
- python列表常用方法_python 列表常用方法
- C语言实现九九乘法表,以for循环为主
- imagej 基本操作
- Android可拖动对话框Dialog
- sqlserver执行生成rpt文件怎么回事_使用生成脚本功能和数据导出工具实现SQL Server2014数据降级2008R2版本...
- 打开网页时有些图片显示不出怎么办
- 【计算机网络系列】链路层的差错控制与流量控制
- 计算机未连接到网络,电脑未连接到一个互联网的问题,解决网络问题方法
- Silverlight游戏设计(Game Design):(十三)帝国时代II (Demo) 之 “战争艺术”②
- 堆排序基础讲解(代码+注释)
- wbin about