LeetCode刷题笔记 [20] 有效的括号
2019-02-24 16:56
239 查看
/* * @lc app=leetcode.cn id=20 lang=c * * [20] 有效的括号 * * https://leetcode-cn.com/problems/valid-parentheses/description/ * * algorithms * Easy (35.90%) * Total Accepted: 42.1K * Total Submissions: 117.1K * Testcase Example: '"()"' * * 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。 * * 有效字符串需满足: * * * 左括号必须用相同类型的右括号闭合。 * 左括号必须以正确的顺序闭合。 * * * 注意空字符串可被认为是有效字符串。 * * 示例 1: * * 输入: "()" * 输出: true * * * 示例 2: * * 输入: "()[]{}" * 输出: true * * * 示例 3: * * 输入: "(]" * 输出: false * * * 示例 4: * * 输入: "([)]" * 输出: false * * * 示例 5: * * 输入: "{[]}" * 输出: true * */ #include<stdio.h> #include<stdlib.h> /* typedef int bool; #define true 1 #define false 0 */ bool isValid(char* s) { int i,j; char tmp[10000]; for (i = 0,j=0; s[i] != '\0';i++){ if(s[i]=='('||s[i]=='['||s[i]=='{'){ tmp[j] = s[i]; j++; } else if(j>0){ j--; switch(s[i]){ case(')'):{if(tmp[j]!='(') return false;break;} case(']'):{if(tmp[j]!='[') return false;break;} case('}'):{if(tmp[j]!='{') return false;break;} } } else return false; } if(i%2==1||j!=0) return false; else return true; } /* int main(){ printf("\n%d", isValid("((")); return 0; } */
数据结构每一本教材都有
相关文章推荐
- 20. 有效的括号-leetcode
- 【LeetCode】20 Valid Parentheses 有效括号
- Leetcode刷题记——20. Valid Parentheses (有效的括号即括号匹配)
- [leetcode]20. Valid Parentheses ,有效的括号符串python实现【easy】
- Leetcode做题日记:20. 有效的括号 (PYTHON)
- leetcode题解-20.有效的括号
- LeetCode 题 - 20. 有效的括号
- LeetCode 20. 有效的括号(java)
- 每日一恋 - LeetCode 20.有效的括号 & 150. 逆波兰表达式求值
- LeetCode - 20. 有效的括号
- 【LeetCode】[20] 有效的括号
- leetcode刷题之20.有效的括号
- Leetcode 20: 有效的括号(Valid Parentheses)
- LeetCode 20. 有效的括号
- Leetcode20. 有效的括号
- leetcode20 有效的括号
- leetcode 括号组合题目 20 是否是有效括号 32 最大有效括号 301 去掉无效括号
- 【刷算法】LeetCode.20-有效的括号
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
- LeetCode 20 Valid Parentheses(有效的括号)