1.3揹包,隊列和棧 (Part1 -- Exercises)

State:Updating


第一題

    public boolean isFull(){
        N == a.length;
    }

第三題

b f g

第四題

package com.chen_swe;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Stack;

/**
 * Created by chen_swe on 3/17/16.
 */
public class Parentheses {
    public static void main(String[] args)
            throws IOException {

        try(BufferedReader reader = new BufferedReader(
                new FileReader("input.txt"))) {

            Stack<Character> stack = new Stack<>();
            int ch;

            while ((ch = reader.read()) != -1) {
                System.out.format("%c",(char)ch);
                switch ((char) ch) {
                    case '(':
                    case '[':
                    case '{':
                        stack.push((char) ch);
                        break;
                    case ')':
                        if (stack.pop() != '(') System.exit(1);
                        break;
                    case ']':
                        if (stack.pop() != '[') System.exit(1);
                        break;
                    case '}':
                        if (stack.pop() != '{') System.exit(1);
                        break;
                    default:
                        break;
                }
            }
        }catch (IOException x){
            System.err.println(x.getMessage());
        }
        System.out.println(true);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章