leetcode-mid-others-150. Evaluate Reverse Polish Notation

mycode 42.30%,

Note: If the symbol is not considered, -1//3=-1 instead of 0, because it is rounded down

class Solution(object):

def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
from collections import deque
def cal(data_1,data_2,item):
if item == '/':
return abs(data_2) // abs(data_1) *(-1 if (data_2> 0) ^ (data_1> 0) else 1)
elif item == '+':
return data_2 + data_1
elif item == '-':
return data_2 - data_1
else:
return data_2 * data_1
dq
= deque()
calset
= ['/','+','-< span style="color: #800000;">','*']
for item in tokens:
if item in calset:
#print(‘if‘)
data_1 = dq.pop()
data_2
= dq.pop()
data
= cal(int(data_1),int(data_2),item)
dq.append(data)
else:
#print(‘else‘)
dq.append(item)
#print(dq)
return dq[0]

Reference:

< div class="code">

#https://www.cnblogs.com/zuoyuan /p/3760530.html Pay attention to the division of negative numbers, the difference between c++ and pytho

class Solution:
# @param tokens, a list of string
# @return an integer
def evalRPN(self, tokens):
stack
= []
for i in range(0,len(tokens)):
if tokens[i] != '+' and tokens [i] != '-' and tokens[i] != '*' and tokens[i ] != '/' :
stack.append(int(tokens[i]))
else:
a
= stack.pop()
b
= stack.pop()
if tokens[i] == '+':
stack.append(a
+b)
if tokens[i] == '-':
stack.append(b
-a)
if tokens[i] == '*':
stack.append(a
*b)
if tokens[i] == '/':
if a*b < 0:
stack.append(
-((-b)/a))
else:
stack.append(b
/a)
return stack.pop()

class Solution(object):

def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
from collections import deque
def cal(data_1,data_2,item):
if item == '/':
return abs(data_2) // abs(data_1) *(-1 if (data_2> 0) ^ (data_1> 0) else 1)
elif item == '+':
return data_2 + data_1
elif item == '-':
return data_2 - data_1
else:
return data_2 * data_1
dq
= deque()
calset
= ['/','+','-< span style="color: #800000;">','*']
for item in tokens:
if item in calset:
#print(‘if‘)
data_1 = dq.pop()
data_2
= dq.pop()
data
= cal(int(data_1),int(data_2),item)
dq.append(data)
else:
#print(‘else‘)
dq.append(item)
#print(dq)
return dq[0]

#https://www.cnblogs.com/zuoyuan/p/3760530.html pay attention to the division of negative numbers, the difference between c++ and pytho

class Solution:
# @param tokens, a list of string
# @return an integer
def evalRPN(self, tokens):
stack
= []
for i in range(0,len(tokens)):
if tokens[i] != '+' and tokens [i] != '-' and tokens[i] != '*' and tokens[i ] != '/' :
stack.append(int(tokens[i]))
else:
a
= stack.pop()
b
= stack.pop()
if tokens[i] == '+':
stack.append(a
+b)
if tokens[i] == '-':
stack.append(b
-a)
if tokens[i] == '*':
stack.append(a
*b)
if tokens[i] == '/':
if a*b < 0:
stack.append(
-((-b)/a))
else:
stack.append(b
/a)
return stack.pop()

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3195 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.