博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
算法(Algorithms)第4版 练习 1.3.27 1.3.28
阅读量:6709 次
发布时间:2019-06-25

本文共 2777 字,大约阅读时间需要 9 分钟。

代码实现:

//1.3.27    /**     * return the value of the maximum key in the list     *      * @param list the linked list of Integer     *      * @return return the maximum key in the list     */    public static int max(LinkedList
list) { if(list.first == null) return 0; int max = 0; for(int val : list) { if(val > max) max = val; } return max; } //1.3.28 /** * return the value of the maximum key in the list by recursion * * @param list the linked list of Integer * * @return return the maximum key in the list */ public static int maxByRecursion(LinkedList
list) { if(list.first == null) return 0; int first = list.first.item;//first item list.first = list.first.next;//remove first item in the list int max = maxByRecursion(list);//calculate the maximum value of the new list if(first > max) return first; else return max; }

测试用例:

package com.qiusongde.linkedlist;import edu.princeton.cs.algs4.StdIn;import edu.princeton.cs.algs4.StdOut;public class Exercise1327 {    public static void main(String[] args) {                LinkedList
list = new LinkedList
(); while(!StdIn.isEmpty()) { int val = StdIn.readInt(); list.insertAtBeginning(val); StdOut.println("insertAtBeginning success: " + val); StdOut.println(list); } int max = LinkedList.max(list); StdOut.println("The maximum key is:" + max); max = LinkedList.maxByRecursion(list); StdOut.println("The maximum key is:" + max + "(By Recursion)"); }}

 

测试数据1:

insertAtBeginning success: 10

10
insertAtBeginning success: 25
25 10
insertAtBeginning success: 30
30 25 10
insertAtBeginning success: 100
100 30 25 10
insertAtBeginning success: 51
51 100 30 25 10
insertAtBeginning success: 26
26 51 100 30 25 10
insertAtBeginning success: 69
69 26 51 100 30 25 10
insertAtBeginning success: 6
6 69 26 51 100 30 25 10
insertAtBeginning success: 32
32 6 69 26 51 100 30 25 10
insertAtBeginning success: 78
78 32 6 69 26 51 100 30 25 10
insertAtBeginning success: 90
90 78 32 6 69 26 51 100 30 25 10
The maximum key is:100
The maximum key is:100(By Recursion)

 

测试数据2:

insertAtBeginning success: 9090 insertAtBeginning success: 7878 90 The maximum key is:90The maximum key is:90(By Recursion)

 

测试数据3:

insertAtBeginning success: 9090 The maximum key is:90The maximum key is:90(By Recursion)

 

测试数据4(输入为空):

The maximum key is:0The maximum key is:0(By Recursion)

 

转载于:https://www.cnblogs.com/songdechiu/p/6512096.html

你可能感兴趣的文章
virtualbox安装ghost版本winxp iso
查看>>
rsync生产实战考试题模拟09
查看>>
Session详解
查看>>
我的友情链接
查看>>
基于centOS6.7搭建LAMP(httpd-2.4.18+mysql-5.5.47+php-5.6.16)环境
查看>>
AIX下PVID详解及其修改方法
查看>>
C# Directory和DirectoryInfo类(文件目录操作)
查看>>
OSPF中的五类LSA
查看>>
maven 加入json-lib.jar 报错 Missing artifact net.sf.js
查看>>
当Elasticsearch logstash kibana (ELK) 遇到symantec
查看>>
单片机的汇编语言与嵌入式C语言的比较
查看>>
POJ-2509(Water,Greedy)
查看>>
获取img元素图片的实际尺寸
查看>>
我的友情链接
查看>>
最新HADOOP 调优常用参数统计表
查看>>
haproxy 配置详解
查看>>
nginx代理resin
查看>>
Java编程最差实践
查看>>
linux运维常用命令
查看>>
axis开发webservice
查看>>