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

150. Evaluate Reverse Polish Notation #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
zwfang merged 1 commit into master from evaluate-reverse-polish-notation
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ continually updating 😃.
* [9. Palindrome Number](src/0009_palindrome_number/palindrome_number.go)
* [13. Roman to Integer](src/0013_roman_to_integer/roman_to_integer.go)   *`string`*
* [66. Plus One](src/0066_plus_one/plus_one.go)   *`array`*
* [150. Evaluate Reverse Polish Notation](src/0150_evaluate_reverse_polish_notation/evaluate_reverse_polish_notation.go)   *`stack`*

<details>
</details>
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
150. Evaluate Reverse Polish Notation
https://leetcode.com/problems/evaluate-reverse-polish-notation/

Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are +, -, *, /. Each operand may be an integer or another expression.

Note:

Division between two integers should truncate toward zero.
The given RPN expression is always valid.
That means the expression would always evaluate to a result and there won't be any divide by zero operation.
*/
// time: 2019年01月07日

package evaluatereversepolishnotation

import "strconv"

// stack
// time complexity: O(n)
// space complexity: O(n)
func evalRPN(tokens []string) int {
stack := make([]int, len(tokens))
top := -1
for i := 0; i < len(tokens); i++ {
switch ch := tokens[i]; ch {
case "+":
stack[top-1] += stack[top]
top--
case "-":
stack[top-1] -= stack[top]
top--
case "*":
stack[top-1] *= stack[top]
top--
case "/":
stack[top-1] /= stack[top]
top--
default:
top++
stack[top], _ = strconv.Atoi(ch)
}
}
return stack[0]
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package evaluatereversepolishnotation

import "testing"

func TestEvalRPN(t *testing.T) {
testCases := [][]string{
{"2", "1", "+", "3", "*"},
{"4", "13", "5", "/", "+"},
{"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"},
{"4", "3", "-"},
}

expected := []int{9, 6, 22, 1}

for index, tokens := range testCases {
if res := evalRPN(tokens); res != expected[index] {
t.Errorf("expected %d, got %d", expected[index], res)
}
}
}
1 change: 1 addition & 0 deletions src/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
|0122|[122. Best Time to Buy and Sell Stock II](0122_best_time_to_buy_and_sell_stock_2/maxprofit.go)|Easy|*`greedy`*|
|0125|[Valid Palindrome](0125_valid_palindrome/valid_palindrome.go)|Easy||
|0144|[144. Binary Tree Preorder Traversal](0144_binary_tree_preorder_traversal/binary_tree_preorder_traversal.go)|Medium|*`binary tree`*|
|0150|[150. Evaluate Reverse Polish Notation](0150_evaluate_reverse_polish_notation/evaluate_reverse_polish_notation.go)|Medium|*`stack`*|
|0167|[Two Sum II - Input array is sorted](./0167_two_sum2/two_sum2.go)|Easy|*`对撞指针(双索引)`*|
|0198|[House Robber](./0198_house_robber/house_robber.go)|Easy|*`memory search;`* *`dynamic programming`*|
|0209|[Minimum Size Subarray Sum](./0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)|Medium|*`sliding window`*|
Expand Down

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