Leecode刷题之旅-C语言/python-20.有效的括号
- /*
- * @lc app=leetcode.cn id=20 lang=c
- *
- * [20] 有效的括号
- *
- * https://leetcode-cn.com/problems/valid-parentheses/description/
- *
- * algorithms
- * Easy (36.53%)
- * Total Accepted: 49.8K
- * Total Submissions: 136K
- * Testcase Example: '"()"'
- *
- * 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
- *
- * 有效字符串需满足:
- *
- *
- * 左括号必须用相同类型的右括号闭合。
- * 左括号必须以正确的顺序闭合。
- *
- *
- * 注意空字符串可被认为是有效字符串。
- *
- * 示例 1:
- *
- * 输入: "()"
- * 输出: true
- *
- *
- * 示例 2:
- *
- * 输入: "()[]{}"
- * 输出: true
- *
- *
- * 示例 3:
- *
- * 输入: "(]"
- * 输出: false
- *
- *
- * 示例 4:
- *
- * 输入: "([)]"
- * 输出: false
- *
- *
- * 示例 5:
- *
- * 输入: "{[]}"
- * 输出: true
- *
- */
- bool isValid(char* s) {
- if(*s==NULL){
- return true;
- }
- int flag = ;
- char a[];
- int top,i;
- char temp;
- // 初始化一个栈
- top = ;
- for(i=;i<strlen(s);i++){
- if(s[i]=='['){ // 如果是左括号直接入栈
- a[++top]=s[i];
- continue;
- }
- if(s[i]==']'){ // 如果是右括号,则尝试匹配
- temp = a[top];
- if(temp=='['){
- flag = ;
- top--;
- continue;
- }else{
- flag = ;
- break;
- }
- }
- if(s[i]=='('){ // 如果是左括号直接入栈
- a[++top]=s[i];
- continue;
- }
- if(s[i]==')'){ // 如果是右括号,则尝试匹配
- temp = a[top];
- if(temp=='('){
- flag = ;
- top--;
- continue;
- }else{
- flag = ;
- break;
- }
- }
- if(s[i]=='{'){ // 如果是左括号直接入栈
- a[++top]=s[i];
- continue;
- }
- if(s[i]=='}'){ // 如果是右括号,则尝试匹配
- temp = a[top];
- if(temp=='{'){
- flag = ;
- top--;
- continue;
- }else{
- flag = ;
- break;
- }
- }
- }
- if(flag&&(top==)){
- return true;
- }else{
- return false;
- }
- }
如果是空的话,返回true。创建一个栈,top为栈内移动指针,如果是左括号,则存入栈中,top加一,如果是右括号,则和当前的栈顶元素进行匹配。匹配若成功,则top减一。匹配若不成功,则flag=0,直接跳出循环。
最后判断flag是否等于一,并且栈顶指针top是否为0(证明所有左括号都被匹配过)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
python:
- #
- # @lc app=leetcode.cn id=20 lang=python3
- #
- # [20] 有效的括号
- #
- # https://leetcode-cn.com/problems/valid-parentheses/description/
- #
- # algorithms
- # Easy (36.53%)
- # Total Accepted: 49.8K
- # Total Submissions: 136K
- # Testcase Example: '"()"'
- #
- # 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
- #
- # 有效字符串需满足:
- #
- #
- # 左括号必须用相同类型的右括号闭合。
- # 左括号必须以正确的顺序闭合。
- #
- #
- # 注意空字符串可被认为是有效字符串。
- #
- # 示例 1:
- #
- # 输入: "()"
- # 输出: true
- #
- #
- # 示例 2:
- #
- # 输入: "()[]{}"
- # 输出: true
- #
- #
- # 示例 3:
- #
- # 输入: "(]"
- # 输出: false
- #
- #
- # 示例 4:
- #
- # 输入: "([)]"
- # 输出: false
- #
- #
- # 示例 5:
- #
- # 输入: "{[]}"
- # 输出: true
- #
- #
- class Solution:
- def isValid(self, s: str) -> bool:
- stack=[] #设置一个列表,把该列表当做栈来使用即可。
- dic={')':'(','}':'{',']':'['} #使用字典存储括号,并且右括号为key,左括号为value
- for char in s:
- if char in dic.values(): #左括号就入栈
- stack.append(char)
- elif char in dic.keys(): #有右括号的话就进行比较,
- if stack==[] or dic[char] != stack.pop():
- return False
- else:
- return False #不再字典中的输入直接输出错误
- return stack==[] #如果栈最后是空的,那么则符合要求,输出true,如果不是,则输出false,使用一个条件表达式
Leecode刷题之旅-C语言/python-20.有效的括号的更多相关文章
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
- Leecode刷题之旅-C语言/python-349两整数之和
/* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...
随机推荐
- Azure镜像市场再下一城,中标软件入驻开启Azure国产操作系统时代
近日,中标软件成功入驻 Azure 镜像市场,提供中标麒麟 Linux 的产品镜像服务,这样一来,中标麒麟也成为国内唯一能够在 Azure 公有云上运行的国产操作系统产品. 作为国内操作系统的领头羊, ...
- Sql根据经纬度算出距离
SELECT ISNULL((2 * 6378.137 * ASIN(SQRT(POWER(SIN((117.223372- ISNULL(Latitude,0) )*PI()/360),2)+CO ...
- laravel + haproxy + https 后生成分页 url 非 https 解决办法
更合适的解决办法:在 AppServiceProvider boot 方法中使用 \URL::forceScheme('https'); 即可. 背景 近日对所有的客户都上线了 https ,本来在 ...
- windows 服务器时间同步失败处理方法
服务器的时间同步失败,通过命令行的形式进行处理. 1.编写时间命令行代码 w32tm /config /manualpeerlist:time.windows.com /syncfromflags:M ...
- C# 任务并行库使用小计 z
1.简单创建使用 using System; using System.Diagnostics; using System.Threading; using System.Threading.Task ...
- CSS z-index的用法
理清 position及z-index的用法: static : 无特殊定位,对象遵循HTML定位规则absolute : 将对象从文档流中拖出,使用left,right,top,bottom等属 ...
- GitLab 数据备份和恢复
GitLab 备份 /opt/gitlab/bin/gitlab-rake gitlab:backup:create //只是备份各项目的数据 完成后会在默认路径下多出来备份的tar包! /var/o ...
- CSS基础语法(二) CSS的9种选择器
样式表的选择器 1.类选择器 根据HTML标签的class属性选择样式应用的属性 .类值{ … } 2.ID选择器 根据HTML标签的ID属性选择样式应用的元素 #id值{ … } 3.标签选择器 ...
- Andriod ADB Interface驱动安装失败Configure USB Debug for Android
介绍: Linux或Apple或OS X ,已经安装了USB驱动调试为Android的帮助,确认您的Android USB调试连接配置和正常工作. Windows下需要自己手动下载驱动安装或者通过下载 ...
- RPMForge介绍及安装
网站RPMForge介绍,安装 http://wiki.centos.org/AdditionalResources/Repositories/RPMForge#head-f0c3ecee3dbb40 ...