zoukankan      html  css  js  c++  java
  • 括号问题

    /*
    题目:括号问题
    内容:
    下面的代码用于判断一个串中的括号是否匹配
    所谓匹配是指不同类型的括号必须左右呼应,可以相互包含,但不能交叉

    例如:
    ..(..[..]..).. 是允许的
    ..(...[...)....].... 是禁止的
    对于 main 方法中的测试用例,应该输出:
    false
    true
    false
    false

    import java.util.*;
    public class A22
    {
    public static boolean isGoodBracket(String s)
    {
    Stack<Character> a = new Stack<Character>();

    for(int i=0; i<s.length(); i++)
    {
    char c = s.charAt(i);
    if(c=='(') a.push(')');
    if(c=='[') a.push(']');
    if(c=='{') a.push('}');

    if(c==')' || c==']' || c=='}')
    {
    if(____________________) return false; // 填空
    if(a.pop() != c) return false;
    }
    }

    if(___________________) return false; // 填空

    return true;
    }

    public static void main(String[] args)
    {
    System.out.println( isGoodBracket("...(..[.)..].{.(..).}..."));
    System.out.println( isGoodBracket("...(..[...].(.).){.(..).}..."));
    System.out.println( isGoodBracket(".....[...].(.).){.(..).}..."));
    System.out.println( isGoodBracket("...(..[...].(.).){.(..)...."));
    }
    }


    请分析代码逻辑,并推测划线处的代码。

    答案写在 “解答.txt” 文件中

    注意:只写划线处应该填的内容,划线前后的内容不要抄写。

    */

     

     1 import java.util.Stack;
     2 
     3     
     4     public class pro10
     5     {
     6         public static boolean isGoodBracket(String s)
     7         {
     8             Stack<Character> a = new Stack<Character>();
     9             
    10             for(int i=0; i<s.length(); i++)
    11             {
    12                 char c = s.charAt(i);
    13                 if(c=='(') a.push(')');
    14                 if(c=='[') a.push(']');
    15                 if(c=='{') a.push('}');
    16                 
    17                 if(c==')' || c==']' || c=='}')
    18                 {
    19                     if(a.empty()) return false;    // 填空 //如果栈已空,却有括号来匹配,则返回false
    20                     if(a.pop() != c) return false;
    21                 }
    22             }
    23             
    24             if(!a.empty()) return false;  // 填空  //如果一串字符串已经匹配完了,而栈底还有未匹配上的括号,则返回false,
    25             
    26             return true;
    27         }
    28         
    29         public static void main(String[] args)
    30         {
    31             System.out.println( isGoodBracket("...(..[.)..].{.(..).}..."));
    32             System.out.println( isGoodBracket("...(..[...].(.).){.(..).}..."));
    33             System.out.println( isGoodBracket(".....[...].(.).){.(..).}..."));
    34             System.out.println( isGoodBracket("...(..[...].(.).){.(..)...."));
    35         }
    36     }

    /*
      这道题目并不难,是数据结构中栈的简单应用,

    */

  • 相关阅读:
    基于Redis主从复制读写分离架构的Session共享(Windows Server)
    第3章 线性表
    第2章 算法
    python中统计计数的几种方法和Counter的介绍
    Linux关于文件的权限笔记
    线程同步与互斥(线程安全)
    Python csv模块的使用
    Python 源码分析:queue 队列模块
    Java锁,真的有这么复杂吗?
    1537 学生干部虚基类
  • 原文地址:https://www.cnblogs.com/wsxjbky/p/3056451.html
Copyright © 2011-2022 走看看