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 8f5c4b9

Browse files
authored
150 solved. (#48)
1 parent a0e9e1a commit 8f5c4b9

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ continually updating 😃.
110110
* [9. Palindrome Number](src/0009_palindrome_number/palindrome_number.go)
111111
* [13. Roman to Integer](src/0013_roman_to_integer/roman_to_integer.go)   *`string`*
112112
* [66. Plus One](src/0066_plus_one/plus_one.go)   *`array`*
113+
* [150. Evaluate Reverse Polish Notation](src/0150_evaluate_reverse_polish_notation/evaluate_reverse_polish_notation.go)   *`stack`*
113114

114115
<details>
115116
</details>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
150. Evaluate Reverse Polish Notation
3+
https://leetcode.com/problems/evaluate-reverse-polish-notation/
4+
5+
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
6+
7+
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
8+
9+
Note:
10+
11+
Division between two integers should truncate toward zero.
12+
The given RPN expression is always valid.
13+
That means the expression would always evaluate to a result and there won't be any divide by zero operation.
14+
*/
15+
// time: 2019年01月07日
16+
17+
package evaluatereversepolishnotation
18+
19+
import "strconv"
20+
21+
// stack
22+
// time complexity: O(n)
23+
// space complexity: O(n)
24+
func evalRPN(tokens []string) int {
25+
stack := make([]int, len(tokens))
26+
top := -1
27+
for i := 0; i < len(tokens); i++ {
28+
switch ch := tokens[i]; ch {
29+
case "+":
30+
stack[top-1] += stack[top]
31+
top--
32+
case "-":
33+
stack[top-1] -= stack[top]
34+
top--
35+
case "*":
36+
stack[top-1] *= stack[top]
37+
top--
38+
case "/":
39+
stack[top-1] /= stack[top]
40+
top--
41+
default:
42+
top++
43+
stack[top], _ = strconv.Atoi(ch)
44+
}
45+
}
46+
return stack[0]
47+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package evaluatereversepolishnotation
2+
3+
import "testing"
4+
5+
func TestEvalRPN(t *testing.T) {
6+
testCases := [][]string{
7+
{"2", "1", "+", "3", "*"},
8+
{"4", "13", "5", "/", "+"},
9+
{"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"},
10+
{"4", "3", "-"},
11+
}
12+
13+
expected := []int{9, 6, 22, 1}
14+
15+
for index, tokens := range testCases {
16+
if res := evalRPN(tokens); res != expected[index] {
17+
t.Errorf("expected %d, got %d", expected[index], res)
18+
}
19+
}
20+
}

‎src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
|0122|[122. Best Time to Buy and Sell Stock II](0122_best_time_to_buy_and_sell_stock_2/maxprofit.go)|Easy|*`greedy`*|
5959
|0125|[Valid Palindrome](0125_valid_palindrome/valid_palindrome.go)|Easy||
6060
|0144|[144. Binary Tree Preorder Traversal](0144_binary_tree_preorder_traversal/binary_tree_preorder_traversal.go)|Medium|*`binary tree`*|
61+
|0150|[150. Evaluate Reverse Polish Notation](0150_evaluate_reverse_polish_notation/evaluate_reverse_polish_notation.go)|Medium|*`stack`*|
6162
|0167|[Two Sum II - Input array is sorted](./0167_two_sum2/two_sum2.go)|Easy|*`对撞指针(双索引)`*|
6263
|0198|[House Robber](./0198_house_robber/house_robber.go)|Easy|*`memory search;`* *`dynamic programming`*|
6364
|0209|[Minimum Size Subarray Sum](./0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)|Medium|*`sliding window`*|

0 commit comments

Comments
(0)

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