1. /*
  2. * @lc app=leetcode.cn id=20 lang=c
  3. *
  4. * [20] 有效的括号
  5. *
  6. * https://leetcode-cn.com/problems/valid-parentheses/description/
  7. *
  8. * algorithms
  9. * Easy (36.53%)
  10. * Total Accepted: 49.8K
  11. * Total Submissions: 136K
  12. * Testcase Example: '"()"'
  13. *
  14. * 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
  15. *
  16. * 有效字符串需满足:
  17. *
  18. *
  19. * 左括号必须用相同类型的右括号闭合。
  20. * 左括号必须以正确的顺序闭合。
  21. *
  22. *
  23. * 注意空字符串可被认为是有效字符串。
  24. *
  25. * 示例 1:
  26. *
  27. * 输入: "()"
  28. * 输出: true
  29. *
  30. *
  31. * 示例 2:
  32. *
  33. * 输入: "()[]{}"
  34. * 输出: true
  35. *
  36. *
  37. * 示例 3:
  38. *
  39. * 输入: "(]"
  40. * 输出: false
  41. *
  42. *
  43. * 示例 4:
  44. *
  45. * 输入: "([)]"
  46. * 输出: false
  47. *
  48. *
  49. * 示例 5:
  50. *
  51. * 输入: "{[]}"
  52. * 输出: true
  53. *
  54. */
  55. bool isValid(char* s) {
  56. if(*s==NULL){
  57. return true;
  58. }
  59. int flag = ;
  60. char a[];
  61. int top,i;
  62. char temp;
  63. // 初始化一个栈
  64. top = ;
  65. for(i=;i<strlen(s);i++){
  66. if(s[i]=='['){ // 如果是左括号直接入栈
  67. a[++top]=s[i];
  68. continue;
  69. }
  70. if(s[i]==']'){ // 如果是右括号,则尝试匹配
  71. temp = a[top];
  72. if(temp=='['){
  73. flag = ;
  74. top--;
  75. continue;
  76. }else{
  77. flag = ;
  78. break;
  79. }
  80. }
  81.  
  82. if(s[i]=='('){ // 如果是左括号直接入栈
  83. a[++top]=s[i];
  84. continue;
  85. }
  86. if(s[i]==')'){ // 如果是右括号,则尝试匹配
  87. temp = a[top];
  88. if(temp=='('){
  89. flag = ;
  90. top--;
  91. continue;
  92. }else{
  93. flag = ;
  94. break;
  95. }
  96. }
  97. if(s[i]=='{'){ // 如果是左括号直接入栈
  98. a[++top]=s[i];
  99. continue;
  100. }
  101. if(s[i]=='}'){ // 如果是右括号,则尝试匹配
  102. temp = a[top];
  103. if(temp=='{'){
  104. flag = ;
  105. top--;
  106. continue;
  107. }else{
  108. flag = ;
  109. break;
  110. }
  111. }
  112.  
  113. }
  114. if(flag&&(top==)){
  115. return true;
  116. }else{
  117. return false;
  118. }
  119. }

如果是空的话,返回true。创建一个栈,top为栈内移动指针,如果是左括号,则存入栈中,top加一,如果是右括号,则和当前的栈顶元素进行匹配。匹配若成功,则top减一。匹配若不成功,则flag=0,直接跳出循环。

最后判断flag是否等于一,并且栈顶指针top是否为0(证明所有左括号都被匹配过)

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

python:

  1. #
  2. # @lc app=leetcode.cn id=20 lang=python3
  3. #
  4. # [20] 有效的括号
  5. #
  6. # https://leetcode-cn.com/problems/valid-parentheses/description/
  7. #
  8. # algorithms
  9. # Easy (36.53%)
  10. # Total Accepted: 49.8K
  11. # Total Submissions: 136K
  12. # Testcase Example: '"()"'
  13. #
  14. # 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
  15. #
  16. # 有效字符串需满足:
  17. #
  18. #
  19. # 左括号必须用相同类型的右括号闭合。
  20. # 左括号必须以正确的顺序闭合。
  21. #
  22. #
  23. # 注意空字符串可被认为是有效字符串。
  24. #
  25. # 示例 1:
  26. #
  27. # 输入: "()"
  28. # 输出: true
  29. #
  30. #
  31. # 示例 2:
  32. #
  33. # 输入: "()[]{}"
  34. # 输出: true
  35. #
  36. #
  37. # 示例 3:
  38. #
  39. # 输入: "(]"
  40. # 输出: false
  41. #
  42. #
  43. # 示例 4:
  44. #
  45. # 输入: "([)]"
  46. # 输出: false
  47. #
  48. #
  49. # 示例 5:
  50. #
  51. # 输入: "{[]}"
  52. # 输出: true
  53. #
  54. #
  55. class Solution:
  56. def isValid(self, s: str) -> bool:
  57.  
  58. stack=[] #设置一个列表,把该列表当做栈来使用即可。
  59. dic={')':'(','}':'{',']':'['} #使用字典存储括号,并且右括号为key,左括号为value
  60. for char in s:
  61. if char in dic.values(): #左括号就入栈
  62. stack.append(char)
  63. elif char in dic.keys(): #有右括号的话就进行比较,
  64. if stack==[] or dic[char] != stack.pop():
  65. return False
  66. else:
  67. return False #不再字典中的输入直接输出错误
  68.  
  69. return stack==[] #如果栈最后是空的,那么则符合要求,输出true,如果不是,则输出false,使用一个条件表达式

Leecode刷题之旅-C语言/python-20.有效的括号的更多相关文章

  1. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  2. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  3. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  4. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  5. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  6. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  7. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  8. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

  9. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

随机推荐

  1. Azure镜像市场再下一城,中标软件入驻开启Azure国产操作系统时代

    近日,中标软件成功入驻 Azure 镜像市场,提供中标麒麟 Linux 的产品镜像服务,这样一来,中标麒麟也成为国内唯一能够在 Azure 公有云上运行的国产操作系统产品. 作为国内操作系统的领头羊, ...

  2. Sql根据经纬度算出距离

    SELECT  ISNULL((2 * 6378.137 * ASIN(SQRT(POWER(SIN((117.223372- ISNULL(Latitude,0) )*PI()/360),2)+CO ...

  3. laravel + haproxy + https 后生成分页 url 非 https 解决办法

    更合适的解决办法:在 AppServiceProvider boot 方法中使用 \URL::forceScheme('https'); 即可. 背景 近日对所有的客户都上线了 https ,本来在 ...

  4. windows 服务器时间同步失败处理方法

    服务器的时间同步失败,通过命令行的形式进行处理. 1.编写时间命令行代码 w32tm /config /manualpeerlist:time.windows.com /syncfromflags:M ...

  5. C# 任务并行库使用小计 z

    1.简单创建使用 using System; using System.Diagnostics; using System.Threading; using System.Threading.Task ...

  6. CSS z-index的用法

    理清 position及z-index的用法: static :  无特殊定位,对象遵循HTML定位规则absolute :  将对象从文档流中拖出,使用left,right,top,bottom等属 ...

  7. GitLab 数据备份和恢复

    GitLab 备份 /opt/gitlab/bin/gitlab-rake gitlab:backup:create //只是备份各项目的数据 完成后会在默认路径下多出来备份的tar包! /var/o ...

  8. CSS基础语法(二) CSS的9种选择器

    样式表的选择器 1.类选择器 根据HTML标签的class属性选择样式应用的属性 .类值{ … } 2.ID选择器 根据HTML标签的ID属性选择样式应用的元素 #id值{ … }  3.标签选择器 ...

  9. Andriod ADB Interface驱动安装失败Configure USB Debug for Android

    介绍: Linux或Apple或OS X ,已经安装了USB驱动调试为Android的帮助,确认您的Android USB调试连接配置和正常工作. Windows下需要自己手动下载驱动安装或者通过下载 ...

  10. RPMForge介绍及安装

    网站RPMForge介绍,安装 http://wiki.centos.org/AdditionalResources/Repositories/RPMForge#head-f0c3ecee3dbb40 ...