678. Valid Parenthesis String
Medium

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

Note:

  1. The string size will be in the range [1, 100].

大致思路是:自左向右遍历时, 记录当前最多能匹配多少右括号(high表示),至少还要匹配多少右括号(low表示)。因为high的数值对后面的右括号还起作用,要维护这一变量,当某一时刻low<0时。说明右括号对于左侧过多。*号能根据后面右括号的情况而变化,遍历过程中只要low>=0,就行了。当遍历到一个左括号是,标志着它能用于抵消后面的右括号,也标志着,后面必须要有右括号或*号与其抵消。

类似只有'(' 和 ')' 的情况,count分别加减1,看是否最终等于0. 这里存在*情况,所以变为[low, high]

时间复杂度O(n),空间复杂度O(1).

class Solution {
public:
bool checkValidString(string s) {
if (s.size() == ){
return true;
}
int low = , high = ; for (int i = ; i < s.length(); i++) {
if (s[i] == '(') {
low++;
high++;
} else if (s[i] == ')') {
if (low > ) {
low--;
}
high--;
} else {
if (low > ) {
low--;
}
high++;
}
if (high < ) {
return false;
}
}
return low == ;
}
};

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 验证括号字符串

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

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

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

  4. 678. Valid Parenthesis String

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

  5. 【leetcode】Valid Parenthesis String

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

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

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

  7. LeetCode Valid Parenthesis String

    原题链接在这里:https://leetcode.com/problems/valid-parenthesis-string/description/ 题目: Given a string conta ...

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

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

  9. [LeetCode] Longest Valid Parentheses

    第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...

随机推荐

  1. Hadoop系列007-HDFS客户端操作

    title: Hadoop系列007-HDFS客户端操作 date: 2018-12-6 15:52:55 updated: 2018-12-6 15:52:55 categories: Hadoop ...

  2. Python基础(解压序列)

    解压序列: a,b=(1,2) # a,b=[1,2] print(a,b) #输出结果:1 2 #获取第一个值和最后一个值 a,*_,c=(1,2,3,4,5,6,7,8,9) print(a) p ...

  3. Spring之旅第六篇-事务管理

    一.什么是事务 什么是事务(Transaction)?事务是数据库中的概念,是指访问并可能更新数据库中各种数据项的一个程序执行单元(unit). 有个非常经典的转账问题:A向B转款1000元,A转出成 ...

  4. springboot~Integer和int如何选择,Integer的意义何在

    今天说一下自己在项目中遇到的问题,然后总结一下Integer引用类型和int值类型 关于默认值 Integer默认为null int默认为0 为什么把数据实体设计成Integer或者不是int 大叔认 ...

  5. DotNetCore跨平台~为debain系统添加阿里云加速

    回到目录 直接把它阿里云的镜像覆盖到原来的/etc/apt/sources.list文件 cat > /etc/apt/sources.list << EOF deb http:// ...

  6. Python的魔法函数系列 __getattrbute__和__getattr__

      #!/usr/bin/env python # -*- coding: utf-8 -*- import sys __metaclass__ = type """ _ ...

  7. DirectX11 初探XMVECOTR&XMMATRIX

    初探DX11的数学库:向量XMVECOTR,矩阵XMMATRIX |XMVECTOR(XNA数学库的向量) DirectX中的一个核心数据类型为 XMVECTOR对它的运算可以使用SIMD指令集加速( ...

  8. 机器学习之决策树二-C4.5原理与代码实现

    决策树之系列二—C4.5原理与代码实现 本文系作者原创,转载请注明出处:https://www.cnblogs.com/further-further-further/p/9435712.html I ...

  9. Docker进阶之四:centos7安装docker

    centos7.6 安装docker 参考:https://docs.docker.com/install/linux/docker-ce/centos/ 一.存在老版本先删除 yum remove ...

  10. Map集合。

    Map集合: java.util,Map<k,v> 特点:1.键值对 2.key-value一一对应 3.key不允许重复. Map常用实现类: java.util.HashMap< ...