[LeetCode][Python3] 227. Basic Calculator II
2019. 4. 9. 03:10 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/basic-calculator-ii/
My Solution :
class Solution:
def calculate(self, s: str) -> int:
s = s.replace(' ', '') + '+'
total = before = 0
sign = '+'
start = 0
for i in range(len(s)):
if s[i] in ('+', '-', '*', '/'):
num = int(s[start:i])
if sign == '+':
total += before
before = num
elif sign == '-':
total += before
before = -num
elif sign == '*':
before *= num
else:
before = int(before/num)
start = i+1
sign = s[i]
return total + before
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 313. Super Ugly Number (0) | 2019.04.13 |
---|---|
[LeetCode][Python3] 218. The Skyline Problem (0) | 2019.04.11 |
[LeetCode][Python3] 150. Evaluate Reverse Polish Notation (0) | 2019.04.10 |
[LeetCode][Python3] 33. Search in Rotated Sorted Array (0) | 2019.04.10 |
[LeetCode][Python3] 134. Gas Station (0) | 2019.04.08 |
[LeetCode][Python3] 23. Merge k Sorted Lists (0) | 2019.04.07 |
[LeetCode][Python3] 210. Course Schedule II (0) | 2019.04.05 |
[LeetCode][Python3] 148. Sort List (1) | 2019.04.04 |
최근에 달린 댓글 최근에 달린 댓글