Below is my code and I don't know what is causing it to exceed the time limit, is there a more efficient way to do it?
class Solution: # @param tokens, a list of string # @return an integer def evalRPN(self, tokens): stack = ; for item in tokens: print stack if item.isdigit(): stack.append(int(item)) elif item[1:].isdigit() and item=="-": stack.append(int(item)) else: num2 = stack.pop() num1 = stack.pop() if item == "+": stack.append(num1+num2) elif item == "-": stack.append(num1-num2) elif item == "*": stack.append(num1*num2) elif item == "/": stack.append(num1/num2) else: print "symbol not recognized" return stack
The print statement is just in case of invalid input, if the input is valid, there should not be anything printed
Did you get your code accepted in python? I found that python has a really different way in processing division when negative numbers involved, I had submitted two solutions, one in python and the other in c++, those sharing the same algorithm, but I got an accepted in c++ but a wrong answer in python. LOL
Yes, I got mine accepted in java first and then in python. Just comment out the above print stack line and I think it would be fine.
Yes it is. I'm also using python, I got problem about this testcase
In python, 6 / -132 = -1. But in C++, the output is 0. That's the difference.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.