算法-第四版-练习1.3.2解答

给定以下输入,java Stack的输出是什么?

it was - the best - of times - - - it was - the - -

Stack:

/**
 * Description : 
 * Author      : mn@furzoom.com
 * Date        : Sep 27, 2016 5:07:34 PM
 * Copyright (c) 2013-2016, http://furzoom.com All Rights Reserved.
 */
package com.furzoom.lab.algs.ch103;

import java.util.Iterator;

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

/**
 * ClassName    : Stack <br>
 * Function     : TODO ADD FUNCTION. <br>
 * date         : Sep 27, 2016 5:07:34 PM <br>
 * 
 * @version 
 */
public class Stack<Item> implements Iterable<Item>
{
    private Node first;
    private int n;
    private class Node
    {
        Item item;
        Node next;
    }
        
    public boolean isEmpty() 
    {
        return first == null;
    }
    
    public int size()
    {
        return n;
    }
    
    public void push(Item item)
    {
        Node oldfirst = first;
        first = new Node();
        first.item = item;
        first.next = oldfirst;
        n++;
    }
    
    public Item pop()
    {
        Item item = first.item;
        first = first.next;
        n--;
        return item;
    }
       
    @Override
    public Iterator<Item> iterator()
    {
        return new ReverseArrayIterator();
    }
    
    private class ReverseArrayIterator implements Iterator<Item>
    {
        private Node p = first;
        @Override
        public boolean hasNext()
        {
            return p != null;
        }

       @Override
        public Item next()
        {
           Item item = p.item;
           p = p.next;
           return item;
        }
        
    }

    public static void main(String[] args)
    {
        Stack<String> s;
        s = new Stack<String>();
        while (!StdIn.isEmpty())
        {
            String item = StdIn.readString();
            if (!item.equals("-"))
                s.push(item);
            else if (!s.isEmpty())
                StdOut.print(s.pop() + " ");
        }
        StdOut.println("(" + s.size() + " left on stack)");
    }
}


E10302.txt

it was - the best - of times - - - it was - the - -



结果如下:

>java -cp ".;../lib/algs4.jar" com
.furzoom.lab.algs.ch103.Stack < com/furzoom/lab/algs/ch103/E10302.txt
was best times of the was the it (1 left on stack)


算法-第四版-1.3 背包、队列和栈-习题索引汇总

算法-第四版习题索引汇总

发布了196 篇原创文章 · 获赞 109 · 访问量 66万+
展开阅读全文

算法第四版二分法代码运行出现错误

11-26

感谢各位大神! 代码是直接从官网保存下来的,在终端运行时出现以下错误提示,不知道是该怎么改啊... MA:src vicky$ javac BinarySearch.java MA:src vicky$ java BinarySearch tinyW.txt < tinyT.txt Exception in thread "main" java.lang.NumberFormatException: For input string: "{\rtf1\ansi\ansicpg936\cocoartf1404\cocoasubrtf470" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at In.readAllInts(In.java:528) at BinarySearch.main(BinarySearch.java:93) 源代码如下 /****************************************************************************** * Compilation: javac BinarySearch.java * Execution: java BinarySearch whitelist.txt < input.txt * Dependencies: In.java StdIn.java StdOut.java * Data files: https://algs4.cs.princeton.edu/11model/tinyW.txt * https://algs4.cs.princeton.edu/11model/tinyT.txt * https://algs4.cs.princeton.edu/11model/largeW.txt * https://algs4.cs.princeton.edu/11model/largeT.txt * * % java BinarySearch tinyW.txt < tinyT.txt * 50 * 99 * 13 * * % java BinarySearch largeW.txt < largeT.txt | more * 499569 * 984875 * 295754 * 207807 * 140925 * 161828 * [367,966 total values] * ******************************************************************************/ import java.util.Arrays; /** * The {@code BinarySearch} class provides a static method for binary * searching for an integer in a sorted array of integers. * <p> * The <em>indexOf</em> operations takes logarithmic time in the worst case. * <p> * For additional documentation, see <a href="https://algs4.cs.princeton.edu/11model">Section 1.1</a> of * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne. * * @author Robert Sedgewick * @author Kevin Wayne */ public class BinarySearch { /** * This class should not be instantiated. */ private BinarySearch() { } /** * Returns the index of the specified key in the specified array. * * @param a the array of integers, must be sorted in ascending order * @param key the search key * @return index of key in array {@code a} if present; {@code -1} otherwise */ public static int indexOf(int[] a, int key) { int lo = 0; int hi = a.length - 1; while (lo <= hi) { // Key is in a[lo..hi] or not present. int mid = lo + (hi - lo) / 2; if (key < a[mid]) hi = mid - 1; else if (key > a[mid]) lo = mid + 1; else return mid; } return -1; } /** * Returns the index of the specified key in the specified array. * This function is poorly named because it does not give the <em>rank</em> * if the array has duplicate keys or if the key is not in the array. * * @param key the search key * @param a the array of integers, must be sorted in ascending order * @return index of key in array {@code a} if present; {@code -1} otherwise * @deprecated Replaced by {@link #indexOf(int[], int)}. */ @Deprecated public static int rank(int key, int[] a) { return indexOf(a, key); } /** * Reads in a sequence of integers from the whitelist file, specified as * a command-line argument; reads in integers from standard input; * prints to standard output those integers that do <em>not</em> appear in the file. * * @param args the command-line arguments */ public static void main(String[] args) { // read the integers from a file In in = new In(args[0]); int[] whitelist = in.readAllInts(); // sort the array Arrays.sort(whitelist); // read integer key from standard input; print if not in whitelist while (!StdIn.isEmpty()) { int key = StdIn.readInt(); if (BinarySearch.indexOf(whitelist, key) == -1) StdOut.println(key); } } } 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览