思路:

数据结构: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. 一些方便系统诊断的bash函数

    原文地址:一些方便系统诊断的bash函数 一些方便系统诊断的bash函数:http://hongjiang.info/common-bash-functions/ 这段脚本包含100多个bash函数, ...

  2. js 时分秒与秒数的转换

    1. 时间戳 格式化为 时分秒(00:00:00) /** * 时间秒数格式化 * @param s 时间戳(单位:秒) * @returns {*} 格式化后的时分秒 */ var sec_to_t ...

  3. Oracle课程档案,第十七天

    flashback drop 闪回下降(删除)SQL> show parameter recyclebinSQL> purge recyclebin;(清除回收站)SQL> crea ...

  4. 环形dp

    对于环形的dp 大多情况都是破环成链 例如 那道宝石手镯的环形 一般来说都是要破环成链的. 或者说 是 两次dp 一次断开 一次强制连接即可. 我想 我应该沉淀下来了这些天写的题都有点虚 要不就是看了 ...

  5. CSS弹性盒布局(display:flex)

    CSS弹性布局(display:flex) 参考: http://www.runoob.com/w3cnote/flex-grammar.html https://www.jianshu.com/p/ ...

  6. vue-cli脚手架

    cnpm i vue-cli -g   //npm 安装报错,原因不明,可能是我改过东西的原因,但是cnpm可以安装 命令行进入要新建的vue的目录执行 C:\Users\76912\Videos\v ...

  7. java框架之Hibernate(2)-持久化类&主键生成策略&缓存&事务&查询

    持久化类 概述 持久化:将内存中的对象持久化到数据库中的过程就是持久化.Hibernate 就是用来进行持久化的框架. 持久化类:一个 Java 对象与数据库的表建立了映射关系,那么这个类在 Hibe ...

  8. Unicode编码学习

    unicode基础知识 简单来说,** unicode 是字符集,utf-8,utf-16,utf-32是编码规则.** unicode 字符集: ttps://unicode-table.com/ ...

  9. 《linux就该这么学》第五节课,shell脚本的各种语句!

    第四章shell语句 (据课本和虚拟机实验排版,借鉴请改动)               4.2:shell脚本   脚本包括:脚本声明,脚本注释,脚本内容和命令 例:#!/bin/bash      ...

  10. 使用 Weinre 调试移动网站

    在 PC 端,我们可以使用 Firebug 或者 Chrome 开发人员工具方便的调试网站或者 Web 应用.但是,当我们想在移动端调试站点或者应用的时候,这些工具就派不上用场了.因此,移动开发人员都 ...