Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules:

  1. Any left parenthesis '(' must have a corresponding right parenthesis ')'.
  2. Any right parenthesis ')' must have a corresponding left parenthesis '('.
  3. Left parenthesis '(' must go before the corresponding right parenthesis ')'.
  4. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string.
  5. An empty string is also valid.

Example 1:

Input: "()"
Output: True

Example 2:

Input: "(*)"
Output: True

Example 3:

Input: "(*))"
Output: True

题目

验证有效括号字符串

思路

recursion

类似Leetcode 22 Generate Parenthesis 思路

代码

 class Solution {
public boolean checkValidString(String s) {
return check(s, 0, 0);
} private boolean check(String s, int start, int count) {
if (count < 0) return false; for (int i = start; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '(') {
count++;
}
else if (c == ')') {
if (count <= 0) return false;
count--;
}
else if (c == '*') {
//1. * for '(' --> (*))
//2. * for ')' --> ((*)
//3. * for empty string --> (*)
return check(s, i + 1, count + 1) || check(s, i + 1, count - 1) || check(s, i + 1, count);
}
} return count == 0;
} }

[leetcode]678. Valid Parenthesis String验证有效括号字符串的更多相关文章

  1. [LeetCode] 678. Valid Parenthesis String 验证括号字符串

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

  2. leetcode 678. Valid Parenthesis String

    678. Valid Parenthesis String Medium Given a string containing only three types of characters: '(', ...

  3. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  4. [LeetCode] Valid Parenthesis String 验证括号字符串

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

  5. 678. Valid Parenthesis String

    https://leetcode.com/problems/valid-parenthesis-string/description/ 这个题的难点在增加了*,*可能是(也可能是).是(的前提是:右边 ...

  6. [LeetCode] 680. Valid Palindrome II 验证回文字符串 II

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  7. 【leetcode】Valid Parenthesis String

    题目: Given a string containing only three types of characters: '(', ')' and '*', write a function to ...

  8. LeetCode 680. Valid Palindrome II (验证回文字符串 Ⅱ)

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  9. [Swift]LeetCode678. 有效的括号字符串 | Valid Parenthesis String

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

随机推荐

  1. AJAX之发送GET请求

    用jquery发送get请求 function AjaxSubmit1() { $.ajax({ //用jQuery发送 url: '/app04/ajax1/', type: 'GET', data ...

  2. idea 执行maven 命令

    如果当前账号不是超级管理员,这边需要执行系统用户变量, 输入安装文件bin路径 参考:https://blog.csdn.net/qq_19167629/article/details/7958490 ...

  3. ZEOSDBO-7安装

    zeosdbo是一套免费开源的Delphi数据库连接组件,可连接mssql.mysql.sybase.oracle.firebird.sqlite.postgresql等多种数据库.调用方法简单. 连 ...

  4. 观察者模式——Head First

    一.定义 观察者模式(Observer Pattern)定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新. 二.类图 三.气象站 //Subject p ...

  5. Haskell语言学习笔记(83)Pipes

    安装 pipes $ cabal install pipes Installed pipes-4.3.9 Prelude> import Pipes Prelude Pipes> impo ...

  6. Ajax核心技术代码

    /* @author weichen */ var xhr = ''; function Ajax() { if(window.XMLHttpRequest) { var xhr = new XMLH ...

  7. .net上的 jpa

    还没试过,有空试试: NPersistence ORSQL

  8. spring 改变url

    server:  port: 9010  servlet:    context-path: /console

  9. vl_sift函数用法

    I = vl_impattern('roofs1') ; image(I) ; %vl_sift函数的输入是一个单精度的灰度图像,灰度值区间归一化到[, ]. %因此图像 I 需要通过下面的函数转成相 ...

  10. opencv 学习笔记

    Opencv 笔记 路径问题: 路径输入:Opencv载Qt中不能出现汉字,路径也不能出现汉字在vs中可以出现. (”D:/QTopencv/.1jpg”)=(”D:\\QTopencv\\.1jpg ...