Implement atoi | Convert string to integer

preview_player
Показать описание
This video lecture explains the idea behind how to implement the atoi function which is very frequently asked in interviews and programming rounds in different company hiring process. The CODE for this is present in the LINK below but i would highly recommend you to try solving this on your own first. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)

Рекомендации по теме
Комментарии
Автор

Thanks for such an awesome, crisp and to the point explaination.

aryansinha
Автор

This was very useful. Instead of giving the solution it's but the path to get to the solution. Just what I need to learn it on my own.

CerberusDawg
Автор

The given explanation doesn't consider a lot of cases present in the LeetCode problem. For eg:

1) leading spaces
2) positive and negative signs
3) period signs
4) out of range numbers

rhythmbhatia
Автор

It does not consider the space issue right? If there are leading spaces then it would return -1 right?

vbalas
Автор

Love your concise and clear explanations!

disiuncta
Автор

Best explanation, thank you so much you're the real deal man

itskellycious
Автор

very nice explanation...kudos...its true that any problem is categorized as difficult only if we are not able to understand it....A good teacher makes everything look simple.

somyasrivastava
Автор

What to do if we have a negative number in the form of string .
How to solve that thing!!? Like for an example we have a string like
" -324".

ankitaverma
Автор

Such a great tutorial I’ve been looking for the math behind this and you gave it to me thank you so much. I can finally implement my own atoi!!!

loganschmogan
Автор

what happens if we pass "-123"? The '-' character will cause us to return -1 instead of -123. How do we account for this in your algorithm?

philbowden
Автор

shouldnt we do this??
try:
    a = int(input())
    print(a)
except:
    print(-1)

RTX_valorant
Автор

Nice explanation. I have a suggestion!! U can give hint before start explaining the solution, so that we can think over that. And we also get basic idea on the particular prob.

dhilipkumar
Автор

its not working as it does not have " " and over+underflow condition thanks

vipingautam
Автор

for a test case of
"words and 987", I returned 987 but the autograder said i was wrong and the correct asnwer should be 0. why is this the case?

ryankim
Автор

But this approach won't work on leetcode. The edge cases are so creatively made.

studyaccount
Автор

I have added the condition for negative no.s too. Btw nice explanation :)

def function():
s = input("INPUT A NUMBER")
toggle = False
if s[0] == '-':
toggle = True
s = s[1:]
sum = 0
k = 1
for i in range(len(s)):
if ord(s[len(s) - 1 - i]) - ord('0') <= 9:
sum += (ord(s[len(s) - 1 - i]) - ord('0')) * k
k *= 10
else:
return "WRONG INPUT"
if toggle == True:
return "-{}".format(sum)
else:
return "{}".format(sum)


print(function())

dipesh
Автор

Sir how to convert "57.77192" into integer format

saichennu
Автор

Im just wondering why did u return -1 instead of 0

timetravel
Автор

comparing str[0] with anything is giving an error:

error: array required, but String found.

kirtisoni
Автор

Nice Explanation ! But why can't we optimize the code with the help of java try-catch blocks. Below code worked for me and passed in GFG also.
int atoi(String str)
{
int num = 0;
try
{
num =Integer.parseInt(str);
}
catch(NumberFormatException e)
{
return -1;
}
return num;
}

shivaprasadgurram
welcome to shbcf.ru