DEV Community

guang
guang

Posted on

leetcode 150 evaluate reverse polish notation

at first i was worried since i didn't have any clue at all. i think it's hard to handle from the head of the list, since there's no rule in the numbers and operators. after racked my brain for one hour in the library, i finally get a clue. use stack to help, push them into stack from end of the list, when there is two numbers and a operator in a row, push them out and evaluate them, push the result back. i was so glad i solve it by myself. but it only beat 6% of person in time. i thought my time complexity is squared n, but it shows the time complexity is only n. maybe it's caused by the condition statement to check whether it's a number. in the solution, it used not in "+-*/", it's clean. but i used in "0123456789".
in the solution it also handle from the head of the list, and the core logic is same: when come across, operator, evaluate the operator and the two numbers before it.

Top comments (0)