算法(Algorithms)第4版 练习 1.3.27 1.3.28
代码实现:
//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<Integer> 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<Integer> 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<Integer> list = new LinkedList<Integer>(); 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: 90
90
insertAtBeginning success: 78
78 90
The maximum key is:90
The maximum key is:90(By Recursion)
测试数据3:
insertAtBeginning success: 90
90
The maximum key is:90
The maximum key is:90(By Recursion)
测试数据4(输入为空):
The maximum key is:0
The maximum key is:0(By Recursion)
算法(Algorithms)第4版 练习 1.3.27 1.3.28的更多相关文章
- 1.2 Data Abstraction(算法 Algorithms 第4版)
1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...
- 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)
1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...
- ubuntu命令行下java工程编辑与算法(第四版)环境配置
ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...
- 配置算法(第4版)的Java编译环境
1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...
- 算法(第四版)C# 习题题解——1.3.49 用 6 个栈实现一个 O(1) 队列
因为这个解法有点复杂,因此单独开一贴介绍. 那么这里就使用六个栈来解决这个问题. 这个算法来自于这篇论文. 原文里用的是 Pure Lisp,不过语法很简单,还是很容易看懂的. 先导知识——用两个栈模 ...
- 在Eclipse下配置算法(第四版)运行环境
第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...
- 排序算法总结(C语言版)
排序算法总结(C语言版) 1. 插入排序 1.1 直接插入排序 1.2 Shell排序 2. 交换排序 2.1 冒泡排序 2.2 快速排序 3. 选择 ...
- 算法(第四版)C#题解——2.1
算法(第四版)C#题解——2.1 写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...
- 《算法》第四版 IDEA 运行环境的搭建
<算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程 ...
随机推荐
- 【Salvation】—— 项目策划&市场分析
写在前面:这个项目是2017年,我们评选校级创新基金项目的参加作品,小组4人,我为负责人,这个项目现在已经基本完成,目前处于后期收尾阶段. 一.项目的目标.内容及创新之处 1.研究目标 体现人类与自然 ...
- 2017.2.7 开涛shiro教程-第六章-Realm及相关对象(一)
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第六章 Realm及相关对象 1.用户.角色.权限的关系 用户和角 ...
- 2016.12.5 在Eclipse中为实现类impl自动生成对应接口
参考来自:http://jingyan.baidu.com/article/ab69b270d63f572ca6189f51.html 在Spring应用中,常常会用到“接口+实现类”的形式,即要实现 ...
- iOS的URLScheme
一直都有接触要设置app的url scheme,从最早的facebook開始. 当时的理解是SSO用的,当授权成功之后,facebook app或者safari能够利用给定的url scheme来回调 ...
- 15:取近似值ApproximateValue
题目描述 写出一个程序,接受一个正浮点数值,输出该数值的近似整数值.如果小数点后数值大于等于5,向上取整:小于5,则向下取整. 输入描述:输入一个正浮点数值 输出描述:输出该数值的近似整数值 输入例子 ...
- grunt使用一步一步讲解
grunt 是一套前端自动化工具,一个基于nodeJs的命令行工具,一般用于:① 压缩文件② 合并文件③ 简单语法检查 对于其他用法,我还不太清楚,我们这里简单介绍下grunt的压缩.合并文件,初学, ...
- a标签javascript传参不正确的解决办法!
代码部分: <a href="javascript:void(0);" onClick="findList(${goodsClassify.id})"&g ...
- Ubuntu下安装Oracle JRE运行环境
安装Oracle JDK -linux-i586.tar.gz安装参见在Ubuntu下利用Eclipse调试FFmpeg Linux x64:链接:http://pan.baidu.com/s/1gd ...
- 研究下JavaScript中的Rest參数和參数默认值
研究下JavaScript中的Rest參数和參数默认值 本文将讨论使 JavaScript 函数更有表现力的两个特性:Rest 參数和參数默认值. Rest 參数 通常,我们须要创建一个可变參数的函数 ...
- ASP.NET MVC 相关的社群与讨论区
ASP.NET MVC 官方论坛 http://forums.asp.net/1146.aspx 台湾微软MSDN论坛 --- ASP.NET 与 AJAX(ASP.NET AND AJAX)讨论区 ...