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 ...
随机推荐
- IOS开发入门实例
关于如何创建第一个 iOS 应用 本篇“第一个 iOS 应用”教程将向你介绍 iOS 应用开发中的“三个T”: Tools(工具)如何利用 Xcode 创建和管理工程. Technologies(技术 ...
- MySQL代码备份
package com.dus.utils; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io. ...
- 极点五笔词库DIY
2004年没啥好的拼音输入法,试了清华紫光输入法一段时间,也相当不满意, 于是在2005年开始学五笔,很快就选定极点五笔了, 使用过程中没啥不满意的,反而还有惊喜: 重装系统后,双击就安装好输入法了, ...
- 三个方法教会你win7中IIS7配置php环境
三个方法教会你win7中IIS7配置php环境.今天静下心来研究了下在win7中使用IIS7配置php环境,其实很简单!跟下面方法做之前,请先确定你的电脑中未安装其它相关环境程序及服务,之前安装过ap ...
- Orchard Core 文档翻译 (七)Contents
CMS Modules »Contents Contents (OrchardCore.Contents) 此模块提供内容管理服务. Liquid 您可以使用“content ”属性从liquid 视 ...
- Python简单介绍以及Python环境搭建(入门1)
转载请标明出处: http://www.cnblogs.com/why168888/p/6400694.html 本文出自:[Edwin博客园] Python 简单介绍 适合领域: Web网站和各种网 ...
- MySQL联合索引最左匹配范例
MySQL联合索引最左匹配范例 参考文章:http://blog.jobbole.com/24006/ 创建示例表. 示例表来自MySQL官方文档: https://dev.mysql.com/doc ...
- iOS UI(布局)约束是什么?view1.attr1 = view2.attr2 * multiplier + constant
/* Create constraints explicitly. Constraints are of the form "view1.attr1 = view2.attr2 * mul ...
- LA 4327 多段图
题目链接:https://vjudge.net/contest/164840#problem/B 题意: 从南往北走,横向的时间不能超过 c: 横向路上有权值,求权值最大: 分析: n<=100 ...
- 百度提供的LBS服务
并不是所有 LBS 云服务 都可以使用 js Ajax 访问,涉及跨域问题 (Jsonp 方式解决)Jsonp 解决跨域问题原理,在页面生成<script> 加载远程 js 代码片段.在L ...