Amazon interview question | String to Integer (atoi) | leetcode 8

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


Please like the video, this really motivates us to make more such videos and helps us to grow. thecodingworld is a community which is formed to help fellow student and professionals looking to crack the “coding interviews”.

We love solving and sharing problems which are frequently asked in technical interviews and voted by community at various websites.

✅ For more and upcoming updates join our community

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

This code is not working when index [0] is negative sign . Because we run our loop from index 1 . And that time in our out only - is stored. So don't able to all the string to integer

engineersvlog
Автор

what if input is " - 42" and " 90 09"?

avnishgupta
Автор

Here is complete code (I'm sure you can make some optimizations):
class Solution(object):
def myAtoi(self, s):
upperBound = 2 ** 31
lowerBound = upperBound * (-1)
integerCounter = 0
tenCounter = 1
idx = 0
digitStack = []
length = len(s)
s = s.strip()
isNeg = False

if len(s) == 0:
return 0

if s[0] == '+':
isNeg = False
idx = 1
elif s[0] == '-':
isNeg = True
idx = 1
elif not s[0].isnumeric():
return 0

if idx >= length or not s[idx].isnumeric():
return 0

while idx < len(s):
if s[idx].isnumeric():
digitStack.append(ord(s[idx]) - 48)
idx += 1
else:
idx = length

while len(digitStack) > 0:
poppedVal = digitStack[-1]
digitStack.pop()
integerCounter += poppedVal * tenCounter
tenCounter *= 10


if isNeg:
integerCounter *= -1

if integerCounter > upperBound-1:
return upperBound - 1
elif integerCounter < lowerBound+1:
return lowerBound
else:
return integerCounter

davidraygoza
Автор

Thank you for taking the time to make this video. You did an awesome job of explaining it.

ajaykotian
Автор

thanks for these videos - curious, wouldn't casting integer values be sufficient using int("1") instead of retrieving ascii value thru ord calls? thanks

vee-obsidian
Автор

Best solution out there on youtube - Clear and concise. Well done!

shashwatshekhar
Автор

Coding : By AGHOUJDAM Ettijani, with C langaue :
// function to convet asccii caracter to integer number : this function take a string
// of caracters and return value or 0 in error case
#include<stdio.h>
#include<stdlib.h>


int ft_atoi(char *str) // function prototype
{
int i; // declaration of increment index in integer type
int nbr; // declaration of number in i store my result of convert operation
int sign; // declaration signed flag if the user input the signed string

nbr = 0; // initialization
i = 0; // initialization

sign = 1; // initialization
if (str[0] == '-') // we tcheck if the first string is minus
{
sign = -1; // convert the signe to negative value to multiply it on return value
i++; // we increment to second caracter
}
while(str[i] != '\0')
{
if ((str[i] - '0') > 9) // we tchecking if the caracter indexing minus zero carter is > 9 we return 0 because the result is out of arangement 0 to 9
return (nbr * sign);
nbr = (nbr * 10) + (str[i] - 48);//or nbr = nbr * 10 + str[i] - '0'
i++;
}
return (nbr * sign);
}

int main(int argc, char **argv) {

if (argc != 2)
return (1);
printf("%d\n", ft_atoi(argv[1]));
printf("%d", atoi(argv[1]));

return 0;
}

alibabascience
Автор

Awesome explanation.Shouldnot isnumeric on line 14 should be isnumeric() with braces-just a typo but thought will point out

gouravmitra
Автор

great video man, what is the tool that you use to draw with this video of the coding diagram?

xiubinzheng
Автор

not working for " -42" why?

ShubhamPatil-bvmk
Автор

Why are not You continuing this kind of learning and helpful video Bro???

Plutonium_
Автор

Red color part is not visible can you use some other color's instead of red

suryaprakashs
Автор

Keep on going
These videos really help us
:)

divyanshukumar
Автор

Hey can you make a playlistfor Goldman Sachs Placement coding ques and for TCS codevita too

vedantshirpurkar
Автор

Indentation of the for loop is wrong. The for loop is not inside the else condition.

utkarshsharma
Автор

but y cant we directly use
if (l.isdigit()):
l=int(l)
instead of using ord to make things difficult for us?

jugh
Автор

will this work for " -41 "

vipingautam
Автор

when str[]=='+' then negative is false. why?

prachimutha
Автор

Pehle toh phir bhi samjh aa rha tha, code dekha tabh toh bilkul hi upar se a gaya, not understood (as you wrote in python)

maheshjindal
Автор

It shows str object has no attribute isnumeric

missimpersia
welcome to shbcf.ru