Untitled
unknown
plain_text
5 months ago
1.9 kB
2
Indexable
from collections import deque class Solution: # @param A : string # @return a string def solve(self, A): st = deque() A = list(A) result = [] st = deque() for i in range(len(A)): if A[i] in ['+','-','*','/','^','(']: if A[i] =="*": while len(st)>0 and st[-1] in ['^','/','*']: x = st.pop() result.append(x) st.append(A[i]) elif A[i]=='/': while len(st)>0 and st[-1] in ['^','*','/']: x = st.pop() result.append(x) st.append(A[i]) elif A[i] == '^': while len(st)>0 and st[-1] =='^': x = st.pop() result.append(x) st.append(A[i]) elif A[i] == '+': while len(st)>0 and st[-1] in ['-','+','*','/','^']: x = st.pop() result.append(x) st.append(A[i]) elif A[i] == '-': while len(st)>0 and st[-1] in ['-','+','*','/','^']: x = st.pop() result.append(x) st.append(A[i]) elif A[i] == "(": st.append(A[i]) elif A[i] ==")": while len(st)>0 and st[-1]!="(": x = st.pop() result.append(x) st.pop() else: result.append(A[i]) while len(st)>0: result.append(st.pop()) return "".join(result)
Editor is loading...
Leave a Comment