LeetCode 224,227,772 Basic Calculator 1,2,3 Explanation and Solution

preview_player
Показать описание

LeetCode 224 Basic Calculator
LeetCode 227 Basic Calculator 2
LeetCode 772 Basic Calculator 3

Subscribe and turn on "All notifications" for my channel!

Thanks in advance. :D

Happy Coding Every day! :D
Рекомендации по теме
Комментарии
Автор

Although it's a bit difficult to understand some of the words at first. But the Algorithm and illustration is so easy to understand as well as it's lucid and general. Glad to find this channel. Hub of hard leetcode.

AbhishekKumar-yvih
Автор

This is genius! Happy girl makes me happy as well!

jinghuang
Автор

Here is the converted Python code for 227 since this one has a special handling needed for the division:

class Solution:
def calculate(self, s: str) -> int:
from collections import deque
q = deque(list(filter(lambda x: x != ' ', s)))
q.append(' ')
def backtracking(q:deque)-> int:
pre, num, place_holder, pre_op = 0, 0, 0, '+'
while q:
ch = q.popleft()
if ch >= '0' and ch <= '9':
num = num * 10 + int(ch)
elif ch == '(':
# pass by reference
num = backtracking(q)
else:
if pre_op == '+':
place_holder += pre
pre = num
elif pre_op == '-':
place_holder += pre
pre = -num
elif pre_op == "*":
pre *= num
elif pre_op == "/":
tmp = pre // num

if tmp < 0 and pre % num != 0:
tmp += 1
pre = tmp


if ch == ")": break
pre_op = ch
num = 0
return pre + place_holder

return backtracking(q)

aunnfriends
Автор

It took a couple of attempts for me to understand this code, but this is a wonderful solution! keep it up.

umabharati
Автор

Very helpful and your notes makes it easy to understand. Thank you !!

reyazbasha
Автор

Really appreciate your hard work.. especially speaking English without being native speaker

adarshjadhav
Автор

Thank you for the solution :-)
If you loop over String s and remove one character at a time, you will save that extra space used for queue. How much space is used by the DFS recursion call ? What is the time and space complexity ?

suhasnayak
Автор

What is the time complexity for this ?

reshmadokania
Автор

thanks a lot. This question is asked by Uber

psn
Автор

I'm glad that you are doing this. Very helpful!

MrUnemployedYouth
Автор

So if input is only number, for example 335, solution breaks?

amitashah
Автор

isn't there a Stack based solution for this, as interviewers might perfer iterative approach

raghurambhagawatula
Автор

Great video overall. I felt that some parts (eg. tracing with example) could have been more clear, nevertheless, it's a great video!

gollapudibhargav
Автор

Sorry if this is stupid (and I blame NYE celebrations) but why do we need to remove whitespace?

siobhanahbois
Автор

Can you also please explain how to handle negative integer values.

suhasnayak