Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f8cd8c4

Browse files
committed
Merge pull request #378 from 0xff-dev/150
Add solution and test-cases for problem 150
2 parents c409055 + 5dda1f4 commit f8cd8c4

File tree

3 files changed

+86
-9
lines changed

3 files changed

+86
-9
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# [150.Evaluate Reverse Polish Notation][title]
2+
3+
## Description
4+
Evaluate the value of an arithmetic expression in [Reverse Polish Notation](http://en.wikipedia.org/wiki/Reverse\_Polish\_notation).
5+
6+
Valid operators are `+`, `-`,`*`, and `/`. Each operand may be an integer or another expression.
7+
8+
**Note** that division between two integers should truncate toward zero.
9+
10+
It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: tokens = ["2","1","+","3","*"]
16+
Output: 9
17+
Explanation: ((2 + 1) * 3) = 9
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input: tokens = ["4","13","5","/","+"]
24+
Output: 6
25+
Explanation: (4 + (13 / 5)) = 6
26+
```
27+
28+
**Example 3:**
29+
30+
```
31+
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
32+
Output: 22
33+
Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
34+
= ((10 * (6 / (12 * -11))) + 17) + 5
35+
= ((10 * (6 / -132)) + 17) + 5
36+
= ((10 * 0) + 17) + 5
37+
= (0 + 17) + 5
38+
= 17 + 5
39+
= 22
40+
```
41+
42+
43+
## 结语
44+
45+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
46+
47+
[title]: https://leetcode.com/problems/evaluate-reverse-polish-notation/
48+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "strconv"
4+
5+
func Solution(tokens []string) int {
6+
stack := make([]int, len(tokens))
7+
idx := 0
8+
for _, n := range tokens {
9+
if !(n == "+" || n == "-" || n == "*" || n == "/") {
10+
nn, _ := strconv.Atoi(n)
11+
stack[idx] = nn
12+
idx++
13+
continue
14+
}
15+
a, b := stack[idx-2], stack[idx-1]
16+
idx--
17+
var c int
18+
if n == "+" {
19+
c = a + b
20+
}
21+
if n == "*" {
22+
c = a * b
23+
}
24+
if n == "/" {
25+
c = a / b
26+
}
27+
if n == "-" {
28+
c = a - b
29+
}
30+
31+
stack[idx-1] = c
32+
}
33+
return stack[0]
534
}

‎leetcode/101-200/0150.Evaluate-Reverse-Polish-Notation/Solution_test.go‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []string{"2", "1", "+", "3", "*"}, 9},
17+
{"TestCase2", []string{"4", "13", "5", "/", "+"}, 6},
18+
{"TestCase3", []string{"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"}, 22},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
//压力测试
33+
//压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
//使用案列
37+
//使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /