Skip to content

Commit

Permalink
剩余代码误删文件替换
Browse files Browse the repository at this point in the history
  • Loading branch information
XuDaHaoRen committed Aug 29, 2021
1 parent f8215f8 commit e1cc87a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
29 changes: 17 additions & 12 deletions problems/0150.逆波兰表达式求值.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# 150. 逆波兰表达式求值

https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/
[力扣题目链接](https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/)

根据 逆波兰表示法,求表达式的值。

Expand All @@ -23,7 +23,7 @@ https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/

整数除法只保留整数部分。
给定逆波兰表达式总是有效的。换句话说,表达式总会得出有效数值且不存在除数为 0 的情况。
 


示例 1:
* 输入: ["2", "1", "+", "3", " * "]
Expand All @@ -37,16 +37,21 @@ https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/

示例 3:
* 输入: ["10", "6", "9", "3", "+", "-11", " * ", "/", " * ", "17", "+", "5", "+"]

* 输出: 22

* 解释:该算式转化为常见的中缀算术表达式为:

```
((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22

= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
```


逆波兰表达式:是一种后缀表达式,所谓后缀就是指算符写在后面。

Expand All @@ -62,20 +67,20 @@ https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/

# 思路

在上一篇文章中[1047.删除字符串中的所有相邻重复项](https://mp.weixin.qq.com/s/1-x6r1wGA9mqIHW5LrMvBg)提到了 递归就是用栈来实现的。
在上一篇文章中[1047.删除字符串中的所有相邻重复项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)提到了 递归就是用栈来实现的。

所以**栈与递归之间在某种程度上是可以转换的!** 这一点我们在后续讲解二叉树的时候,会更详细的讲解到。

那么来看一下本题,**其实逆波兰表达式相当于是二叉树中的后序遍历**。 大家可以把运算符作为中间节点,按照后序遍历的规则画出一个二叉树。

但我们没有必要从二叉树的角度去解决这个问题,只要知道逆波兰表达式是用后续遍历的方式把二叉树序列化了,就可以了。

在进一步看,本题中每一个子表达式要得出一个结果,然后拿这个结果再进行运算,那么**这岂不就是一个相邻字符串消除的过程,和[1047.删除字符串中的所有相邻重复项](https://mp.weixin.qq.com/s/1-x6r1wGA9mqIHW5LrMvBg)中的对对碰游戏是不是就非常像了。**
在进一步看,本题中每一个子表达式要得出一个结果,然后拿这个结果再进行运算,那么**这岂不就是一个相邻字符串消除的过程,和[1047.删除字符串中的所有相邻重复项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)中的对对碰游戏是不是就非常像了。**

如动画所示:
![150.逆波兰表达式求值](https://code-thinking.cdn.bcebos.com/gifs/150.逆波兰表达式求值.gif)

相信看完动画大家应该知道,这和[1047. 删除字符串中的所有相邻重复项](https://mp.weixin.qq.com/s/1-x6r1wGA9mqIHW5LrMvBg)是差不错的,只不过本题不要相邻元素做消除了,而是做运算!
相信看完动画大家应该知道,这和[1047. 删除字符串中的所有相邻重复项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)是差不错的,只不过本题不要相邻元素做消除了,而是做运算!

C++代码如下:

Expand Down
6 changes: 3 additions & 3 deletions problems/0300.最长上升子序列.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

## 300.最长递增子序列

题目链接:https://leetcode-cn.com/problems/longest-increasing-subsequence/
[力扣题目链接](https://leetcode-cn.com/problems/longest-increasing-subsequence/)

给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。

子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子序列。

 

示例 1:
输入:nums = [10,9,2,5,3,7,101,18]
输出:4
Expand All @@ -27,7 +27,7 @@
示例 3:
输入:nums = [7,7,7,7,7,7,7]
输出:1
 

提示:

* 1 <= nums.length <= 2500
Expand Down

0 comments on commit e1cc87a

Please sign in to comment.