思路:

数据结构:stack。遍历整个字符串,如果遇到左向括号( [ { 则入栈。如果遇到右向括号时,先检查栈是否为空,为空说明左右向括号数目不一致,返回false;不为空则弹出栈顶元素查看是否和右向括号匹配。遍历完,要return stack.isEmpty(); 确保栈内没有剩余。

代码:

public class Solution {
/**
* @param s: A string
* @return: whether the string is a valid parentheses
*/
public boolean isValidParentheses(String s) {
Stack<Character> st = new Stack<Character>(); for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '(' || c == '[' || c == '{') {
st.push(c);
}
if (c == ')' || c == ']' || c == '}') {
if (st.isEmpty()) {
return false;
}
char popChar = st.pop();
if ( c == ')' && popChar != ')') {
return false;
}
if ( c == ']' && popChar != ']') {
return false;
}
if ( c == '}' && popChar != '}' ) {
return false;
}
}
}
return st.isEmpty();
}
}

代码风格优化:String遍历每一个char(line 9) ; 用if-else分支更准确(line 10, 12)

 public class Solution {
/**
* @param s: A string
* @return: whether the string is a valid parentheses
*/
public boolean isValidParentheses(String s) {
Stack<Character> st = new Stack<Character>(); for (Character c : s.toCharArray()) {
if (c == '(' || c == '[' || c == '{') {
st.push(c);
} else {
if (st.isEmpty()) {
return false;
}
char popChar = st.pop();
if ( c == ')' && popChar != '(') {
return false;
}
if ( c == ']' && popChar != '[') {
return false;
}
if ( c == '}' && popChar != '{') {
return false;
}
}
}
return st.isEmpty();
}
}

Lintcode423-Valid Parentheses-Easy的更多相关文章

  1. [leetcode] 20. Valid Parentheses (easy)

    原题链接 匹配括号 思路: 用栈,遍历过程中,匹配的成对出栈:结束后,栈空则对,栈非空则错. Runtime: 4 ms, faster than 99.94% of Java class Solut ...

  2. leetCode练题——20. Valid Parentheses

    1.题目 20. Valid Parentheses——Easy  Given a string containing just the characters '(', ')', '{', '}',  ...

  3. C# 写 LeetCode easy #20 Valid Parentheses

    20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  4. LeetCode--To Lower Case && Remove Outermost Parentheses (Easy)

    709. To Lower Case(Easy)# Implement function ToLowerCase() that has a string parameter str, and retu ...

  5. 46.Valid Parentheses

    Valid Parentheses My Submissions QuestionEditorial Solution Total Accepted: 106346 Total Submissions ...

  6. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  7. [LeetCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  8. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. 72. Generate Parentheses && Valid Parentheses

    Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  10. leetcode 32. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

随机推荐

  1. pro.antd.design

    有关react的ui组建antd.design新推出的pro版本,最近用到了路由方面. common与routes; 首先需要在menu.js里面配置: { name: '精品', icon: 'da ...

  2. 再次重温《Right here waiting》

    记得高中时候听到这首曲子(当时还让同桌帮我抄了这首曲子,后来这个本子也不知道扔到哪里去了), 前天偶尔在虾米遇到这首曲子,过去的青涩岁月历历在目,自己手动打打歌词,一方面是为了重温这首曲子,另一方面, ...

  3. 通过ICE轻松部署WES7镜像

    作者:雷志刚 转自:http://lzg-ad.blog.sohu.com/156323256.html 注:该文转自Happymy,感谢他的技术提供和分享. 本文适合的软件版本:CTP,RC 如果大 ...

  4. AVL树C++实现(插入,删除,查找,清空,遍历操作)

    AVL.h文件代码 #pragma once #include<iostream> #include<stack> #include <assert.h> usin ...

  5. Java 关于密码处理的工具类[MD5编码][AES加密/解密]

    项目中又遇到了加密问题,又去翻了半天,然后做测试,干脆就把常用的两类小结一下. 1.第一种所谓的MD5加密 其实也不算加密,只是基于Hash算法的不可逆编码而已,等于说,一旦经过MD5处理,是不可能从 ...

  6. 1、js比较日期的大小

    ① html <div class="ptb10"><span>共享开始时间:</span><input type="text& ...

  7. 1、vue 笔记之 组件

    1.组件个人理解:  <组件>是页面的一部分,将界面切分成部分,每部分称为 <组件>   2.组件化思想:          //2.1.定义一个全局的组件,组件支持‘驼峰命名 ...

  8. 深入理解Java虚拟机5-chap7-斗者2星

    一.类加载机制 1.类加载机制:虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的Class文件加载到内存,这就是类加载机制. 2.类型 ...

  9. 台式电脑、笔记本快捷选择启动项Boot 快捷键大全

    我们在安装系统时,会去设置电脑是从硬盘启动.U盘启动.光驱启动.网卡启动. 一般设置的方法有两种:一种是进BIOS主板菜单设置启动项顺序:另一种就是我在这里要介绍的快捷选择启动项. 以下是网友整理的各 ...

  10. js 执行机制

    浏览器中每个一个窗口都是一个单独的进程.这就需要分析浏览器与Javascript解释引擎之间的关系.先给出结论,浏览器本身是多线程的,Javascript解释引擎是单线程的. 先说说浏览器有哪些线程, ...