• 算法(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)
  • 相关阅读:
    线程同步的三种方式(Mutex,Event,Critical Section) 沧海
    VC++多线程下内存操作的优化 沧海
    C++内存对象大会战 沧海
    技术关注:搜索引擎经验 沧海
    jira 3.13.5版 安装 配置 用户权限控制 拂晓风起
    C++ int string 转换 拂晓风起
    C++调用C链接库会出现的问题 拂晓风起
    Windows Server 2003 IIS Service Unavailable 问题解决 拂晓风起
    研究 学术 开发 的好用工具(不包括常见的) 拂晓风起
    SGMarks 问世 (Firefox扩展Gmarks的扩展版) 纯属学习 拂晓风起
  • 原文地址:https://www.cnblogs.com/songdechiu/p/6512096.html
Copyright © 2020-2023  润新知