算法(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搭建的教程 ... 
随机推荐
- mysql 导入sql文件时编码报错
			1.命令行导入 mysql -uroot -pnewpwd --default-character-set=utf8 databasename < xxx.sql 2.使用source导入 进入 ... 
- Win7如何自定义鼠标右键菜单 添加在此处打开CMD窗口
			将下面文件保存为"右键添加在此处打开CMD窗口.reg"双击导入运行即可 Windows Registry Editor Version 5.00 [HKEY_CLASSES_RO ... 
- centos DHCP
			yum install dhcp cat /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example > /etc/dhcp/dhcpd.conf vim /etc ... 
- BZOJ 4128 Matrix BSGS+矩阵求逆
			题意:链接 方法: BSGS+矩阵求逆 解析: 这题就是把Ax=B(mod C)的A和B换成了矩阵. 然而别的地方并没有修改. 所以就涉及到矩阵的逆元这个问题. 矩阵的逆元怎么求呢? 先在原矩阵后接一 ... 
- rename命令
			rename命令用字符串替换的方式批量改变文件名. 语法 rename(参数) 参数 原字符串:将文件名需要替换的字符串: 目标字符串:将文件名中含有的原字符替换成目标字符串: 文件:指定要改变文件名 ... 
- iOS 瀑布流封装
			代码地址如下:http://www.demodashi.com/demo/12284.html 一.效果预览 功能描述:WSLWaterFlowLayout 是在继承于UICollectionView ... 
- grunt使用一步一步讲解
			grunt 是一套前端自动化工具,一个基于nodeJs的命令行工具,一般用于:① 压缩文件② 合并文件③ 简单语法检查 对于其他用法,我还不太清楚,我们这里简单介绍下grunt的压缩.合并文件,初学, ... 
- Linux QtCreator 设置mingw编译器生成windows程序
			Qt跨平台,那必须在Linux平台编译一个可以在windows下运行的Qt程序才行,当然还得和QtCreator环境弄在一起才行 工作环境:Centos 7 yum install qt5-qt* m ... 
- 机器学习实战之K-Means算法
			一,引言 先说个K-means算法很高大上的用处,来开始新的算法学习.我们都知道每一届的美国总统大选,那叫一个竞争激烈.可以说,谁拿到了各个州尽可能多的选票,谁选举获胜的几率就会非常大.有人会说,这跟 ... 
- mybatis--foreach,choose 小结
			写博客个人不喜欢写那种长篇大论.富有文採与哲学的文章,搞开发的就喜欢直击重点,仅仅记录重要的信息就好了,以后查看的时候也很方便! mybatis 中 在if语句或when中 假设推断一个字段是否和1同 ... 
