文件: root - text - article - 2019 - 10 - daily-problem-validate-balanced-parentheses.txt
标签: 每日算法, 括号匹配, 面试, Python, daily problem, Validate Balanced Parentheses, algorithms, twitter interview question, python, | 英文 | 主页 | 类别: 计算机科学 | 539 次阅读, 24246 次搜索 | 161 个单词
| 浏览 | 博客存档
标签: 每日算法, 括号匹配, 面试, Python, daily problem, Validate Balanced Parentheses, algorithms, twitter interview question, python, | 英文 | 主页 | 类别: 计算机科学 | 539 次阅读, 24246 次搜索 | 161 个单词
| 浏览 | 博客存档
Hi, here's your problem today. This problem was recently asked by Uber:
Imagine you are building a compiler. Before running any code, the compiler must check that the parentheses in the program are balanced. Every opening bracket must have a corresponding closing bracket. We can approximate this using strings.
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets are closed by the same type of brackets.
- Open brackets are closed in the correct order.
- Note that an empty string is also considered valid.
Example:
Input: "((()))"
Output: True
Input: "[()]{}"
Output: True
Input: "({[)]"
Output: False
标签: 每日算法, 括号匹配, 面试, Python, daily problem, Validate Balanced Parentheses, algorithms, twitter interview question, python, | 英文 | 主页 | 类别: 计算机科学 | 539 次阅读, 24246 次搜索 | 161 个单词 Imagine you are building a compiler. Before running any code, the compiler must check that the parentheses in the program are balanced. Every opening bracket must have a corresponding closing bracket. We can approximate this using strings.
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets are closed by the same type of brackets.
- Open brackets are closed in the correct order.
- Note that an empty string is also considered valid.
Example:
Input: "((()))"
Output: True
Input: "[()]{}"
Output: True
Input: "({[)]"
Output: False
class Solution:
def isValid(self, s):
# Fill this in.
# Test Program
s = "()(){(())"
# should return False
print(Solution().isValid(s))
s = ""
# should return True
print(Solution().isValid(s))
s = "([{}])()"
# should return True
print(Solution().isValid(s))
猜您喜欢...
- Daily Interview Problem: Maximum Profit From Stocks
- Daily Interview Problem: Room scheduling
- [Daily Problem] Longest Increasing Subsequence
- [Daily Problem] Witness of The Tall People
- Invert a Binary Tree
- Compare Version Numbers
- First and Last Indices of an Element in a Sorted Array
- Algorithm Interview: Maximum Path Sum in Binary Tree
- Generate All IP Addresses
- Most Frequent Subtree Sum
©2006~2024 牛排过熟 - 0.0077 秒 - 2563.603 KB/s - 27 在线 内存: 493.72 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - 服务器 PHP 版本号: 7.4.33
牛排怎么做才好吃? | <meta name="机器人" content="不索引, 跟踪" />
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - 服务器 PHP 版本号: 7.4.33
评论 (0)
读写完全 - 一般 - 最小 - 表格 - 所有评论 - 统计
当前页暂时没有评论。