华为-成研所笔试-java-括号匹配
2015-04-23 23:02
351 查看
华为2015年暑假实习生成研所笔试(机试)题目 第二题 括号匹配
三种括号 []{}()
括号匹配规则:
{{()[]}} 则为true;
{[()}} 则为false 并打印出未匹配位置;
源码为
三种括号 []{}()
括号匹配规则:
{{()[]}} 则为true;
{[()}} 则为false 并打印出未匹配位置;
源码为
import java.util.Scanner; import java.util.Stack; public class Test { public static void main(String[] args) { Stack<Character> stack = new Stack<Character>(); Scanner sc = new Scanner(System.in); char[] b = sc.nextLine().toCharArray(); boolean result = false; int count = 0; for (char c : b) { if (c == '(' || c == '[' || c == '{') { stack.push(c); } else if (c == ')' || c == ']' || c == '}') { switch (c) { case ')': { if (!stack.isEmpty()) result = stack.pop() == '(' ? true : false; if (!result) break; } case '}': { if (!stack.isEmpty()) result = stack.pop() == '{' ? true : false; if (!result) break; } case ']': { if (!stack.isEmpty()) result = stack.pop() == '[' ? true : false; if (!result) break; } default: { } } } count++; } count = result ? 0 : count; System.out.println("result=" + result + count); } }
相关文章推荐
- 华为笔试题--LISP括号匹配 解析及源码实现
- 华为笔试题--LISP括号匹配 解析及源码实现
- 【2018校招笔试-京东=java开发】题目1括号匹配方案
- 用栈实现括号匹配检验(java)
- Java实现括号匹配
- 网易2016年Java工程师实习编程题目之括号匹配的验证
- java括号匹配算法
- Java堆栈的应用1----------堆栈的自定义实现以及括号匹配算法的Java实现
- 华为Java笔试题
- Java 用栈解决括号匹配问题
- java--一道简单的括号匹配问题
- 华为Java笔试题
- 华为的JAVA笔试题
- ACM括号匹配问题(java实现)
- 栈之括号匹配问题(java实现)
- OJ——华为编程题目:输入字符串括号是否匹配
- 华为的一道括号匹配题目
- 使用栈实现括号匹配算法-java
- 华为笔试 java 试题
- 对如下字符串(234453)[234]{2324}分析它的括号使用是否正确,括号匹配(Java实现)