代码实现:

    //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. 1.2 Data Abstraction(算法 Algorithms 第4版)

    1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...

  2. 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)

    1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...

  3. ubuntu命令行下java工程编辑与算法(第四版)环境配置

    ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...

  4. 配置算法(第4版)的Java编译环境

    1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...

  5. 算法(第四版)C# 习题题解——1.3.49 用 6 个栈实现一个 O(1) 队列

    因为这个解法有点复杂,因此单独开一贴介绍. 那么这里就使用六个栈来解决这个问题. 这个算法来自于这篇论文. 原文里用的是 Pure Lisp,不过语法很简单,还是很容易看懂的. 先导知识——用两个栈模 ...

  6. 在Eclipse下配置算法(第四版)运行环境

    第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...

  7. 排序算法总结(C语言版)

    排序算法总结(C语言版) 1.    插入排序 1.1     直接插入排序 1.2     Shell排序 2.    交换排序 2.1     冒泡排序 2.2     快速排序 3.    选择 ...

  8. 算法(第四版)C#题解——2.1

    算法(第四版)C#题解——2.1   写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...

  9. 《算法》第四版 IDEA 运行环境的搭建

    <算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程 ...

随机推荐

  1. 2016.8.19 在dialog上增加一个button出现错误:failed to execute setAttribute on Element...

    目标:想要在dialog上多加一个button. 语法来自: http://api.jqueryui.com/dialog/#option-buttons   可见新增在dialog上的button要 ...

  2. mongodb springdata 问题处理

    发现spring4-mongo3.2.1 加上用户名密码认证后无法认证的问题. 1.必须在当前使用的数据库下建用户,权限可以给readWrite 2.由于mongodb2和mongodb3的用户认证方 ...

  3. Codeforces Round #243 (Div. 2)——Sereja and Table

    看这个问题之前,能够先看看这个论文<一类算法复合的方法>,说白了就是分类讨论,可是这个思想非常重要 题目链接 题意: 首先给出联通块的定义:对于相邻(上下和左右)的同样的数字视为一个联通块 ...

  4. 如何让DIV居中

    总结:text-align:center;对三中浏览器而言,都具有文字/行内元素的嵌套式居中,或者说继承式的居中,只要外面的容器设置了这个属性,那么他内部的所有元素都具有这个属性(意思是,虽然这个属性 ...

  5. Android学习笔记(36):Android的两种事件处理方式

    Android提供了两种事件处理的方式:基于回调的事件处理 和 基于监听的事件处理. 我们来说的easy理解一点: (1)基于回调的事件处理就是继承GUI组件,并重写该组件的事件处理方法.除了一些特定 ...

  6. Web安全系列(三):XSS 攻击进阶(挖掘漏洞)

    前言 在前些章节 (web安全系列(一):XSS 攻击基础及原理)以及(Web安全系列(二):XSS 攻击进阶(初探 XSS Payload))中,我详细介绍了 XSS 形成的原理以及 XSS 攻击的 ...

  7. 【转载】ASP.Net请求处理机制初步探索之旅 - Part 3 管道

    开篇:上一篇我们了解了一个ASP.Net页面请求的核心处理入口,它经历了三个重要的入口,分别是:ISAPIRuntime.ProcessRequest().HttpRuntime.ProcessReq ...

  8. POJ 3373 Changing Digits

    题目大意: 给出一个数n,求m,使得m的长度和n相等.能被k整除.有多个数符合条件输出与n在每位数字上改变次数最小的.改变次数同样的输出大小最小的.  共同拥有两种解法:DP解法,记忆化搜索的算法. ...

  9. mysql解决中文乱码

    mysql>use mydb; mysql>alter database mydb  character set utf8;! 这种方法只对设置后重新创建的表有效,对已存在的表无效 des ...

  10. 【翻译自mos文章】关于分区索引:Global, Local, Prefixed and Non-Prefixed

    来源于: Partitioned Indexes: Global, Local, Prefixed and Non-Prefixed (文档 ID 69374.1) APPLIES TO: Oracl ...