博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] 678. Valid Parenthesis String
阅读量:5929 次
发布时间:2019-06-19

本文共 1273 字,大约阅读时间需要 4 分钟。

Problem

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:

Any left parenthesis '(' must have a corresponding right parenthesis ')'.

Any right parenthesis ')' must have a corresponding left parenthesis '('.
Left parenthesis '(' must go before the corresponding right parenthesis ')'.
'*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string.
An empty string is also valid.
Example 1:
Input: "()"
Output: True
Example 2:
Input: "(*)"
Output: True
Example 3:
Input: "(*))"
Output: True
Note:
The string size will be in the range [1, 100].

Solution

class Solution {    public boolean checkValidString(String s) {        return helper(s, 0, 0);    }    private boolean helper(String s, int index, int count) {        if (index == s.length()) {            return count == 0;        }                if (count < 0) return false;        char ch = s.charAt(index);        if (ch == '(') return helper(s, index+1, count+1);        else if (ch == ')') return helper(s, index+1, count-1);        else return helper(s, index+1, count-1) || helper(s, index+1, count+1) || helper(s, index+1, count);    }}

转载地址:http://qxevx.baihongyu.com/

你可能感兴趣的文章
Verify the Developer App certificate for your account is trusted on your device.
查看>>
神经网络- receptive field
查看>>
java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
查看>>
centos7.0搭建svn服务器
查看>>
JS多个对象添加到一个对象中
查看>>
九度 1376 最近零子序列
查看>>
yii---where or该如何使用
查看>>
非彼拉且数列的实现
查看>>
高性能缓存服务器Squid架构配置
查看>>
在Hyper-V下安装Windows 8
查看>>
Android:ZoomControls控件
查看>>
xshell 通过ssh连接 ubuntu15_x64
查看>>
mysql 2013错误解决
查看>>
Windows Server 2012 存储去重
查看>>
SQL/LINQ/Lamda 写法[转发]
查看>>
25.3. UUID()
查看>>
Hadoop - Mac OSX下配置和启动hadoop以及常见错误解决
查看>>
UIPassValue页面传值&amp;nbsp;UI_08(下)
查看>>
[译] 人人都是设计师。我们可以的
查看>>
你应该知道的前端--渲染原理
查看>>