代码随想录刷题记录 Day11
To Do List
- DAY 10 回顾
- LeetCode 20. 有效的括号
- LeetCode 1047. 删除字符串中的所有相邻重复项
- LeetCode 150. 逆波兰表达式求值
- DAY 11 总结
DAY 10 回顾
()
LeetCode 20. 有效的括号 / Valid Parentheses
1 | Difficulty: Easy |
Given a string s
containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
- Every close bracket has a corresponding open bracket of the same type.
Translated by zh-CN
给定一个只包括 '(',')','{','}','[',']' 的字符串 s,判断字符串是否有效。有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
每个右括号都有一个对应的相同类型的左括号。
Example 1:
1 | Input: s = "()" |
Example 2:
1 | Input: s = "()[]{}" |
Example 3:
1 | Input: s = "(]" |
Constraints:
1 <= s.length <= 104
s
consists of parentheses only'()[]{}'
.Translated by zh-CN
s 仅由括号 '()[]{}' 组成
解法:
(JavaScript)
1 | /** |
PS:
LeetCode 1047. 删除字符串中的所有相邻重复项
1 | Difficulty: Easy |
You are given a string s
consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them.
We repeatedly make duplicate removals on s
until we no longer can.
Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique.
Translated by zh-CN
给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。在 S 上反复执行重复项删除操作,直到无法继续删除。
在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。
Example 1:
1 | Input: s = "abbaca" |
Example 2:
1 | Input: s = "azxxzy" |
Constraints:
1 <= s.length <= 105
s
consists of lowercase English letters.Translated by zh-CN
S 仅由小写英文字母组成。
解法:
(JavaScript)
1 | /** |
PS:
LeetCode 150. 逆波兰表达式求值 / Evaluate Reverse Polish Notation
1 | Difficulty: Medium |
You are given an array of strings tokens
that represents an arithmetic expression in a Reverse Polish Notation.
Evaluate the expression. Return an integer that represents the value of the expression.
Note that:
The valid operators are
'+'
,'-'
,'*'
, and'/'
.Each operand may be an integer or another expression.
The division between two integers always truncates toward zero.
There will not be any division by zero.
The input represents a valid arithmetic expression in a reverse polish notation.
The answer and all the intermediate calculations can be represented in a 32-bit integer.
Translated by zh-CN
给你一个字符串数组 tokens ,表示一个根据 逆波兰表示法 表示的算术表达式。
请你计算该表达式。返回一个表示表达式值的整数。
注意:
有效的算符为 '+'、'-'、'*' 和 '/' 。
每个操作数(运算对象)都可以是一个整数或者另一个表达式。
两个整数之间的除法总是 向零截断 。 表达式中不含除零运算。
输入是一个根据逆波兰表示法表示的算术表达式。
答案及所有中间计算结果可以用 32 位 整数表示。
Example 1:
1 | Input: tokens = ["2","1","+","3","*"] |
Example 2:
1 | Input: tokens = ["4","13","5","/","+"] |
Example 3:
1 | Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"] |
Constraints:
1 <= tokens.length <= 104
tokens[i]
is either an operator:"+"
,"-"
,"*"
, or"/"
, or an integer in the range[-200, 200]
.Translated by zh-CN
tokens[i] 是一个算符("+"、"-"、"*" 或 "/"),或是在范围 [-200, 200] 内的一个整数
逆波兰表达式:
逆波兰表达式是一种后缀表达式,所谓后缀就是指算符写在后面。
- 平常使用的算式则是一种中缀表达式,如
( 1 + 2 ) * ( 3 + 4 )
。 - 该算式的逆波兰表达式写法为
( ( 1 2 + ) ( 3 4 + ) * )
。
逆波兰表达式主要有以下两个优点:
- 去掉括号后表达式无歧义,上式即便写成
1 2 + 3 4 + *
也可以依据次序计算出正确结果。 - 适合用栈操作运算:遇到数字则入栈;遇到算符则取出栈顶两个数字进行计算,并将结果压入栈中
解法:
(JavaScript)
1 | /** |
PS:
DAY 11 总结
…