I am newbie , have two doubts. 1) why we using long to define ans 2)in the line ans=ans*10+s[i]-' 0 ' what is purpose of this one'0' I hope you will explain it😢😢
hi sure, :) so see, 1. if we do not use long, and we use int, we get signed overflow error Line 23: Char 23: runtime error: signed integer overflow: 912834723 * 10 cannot be represented in type 'int' (solution.cpp) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior solution.cpp:23:23 this is because ans variable may add up to values which go beyond specific integer range, so we use long :) 2. so, the expression s[i] - '0' is a common way to convert a character representing a digit to its corresponding integer value. for eg. If s[i] is '5', its ASCII value is 53. Subtracting the ASCII value of '0' (48) from it gives 53 - 48 = 5, which is the integer representation of the digit.
🎯 Key points for quick navigation: 00:02 *Handle leading whitespace* 00:42 *Convert string to integer* 01:25 *Check sign (positive/negative)* 02:08 *Read until non-digit* Made with HARPA AI
Videos are helpful. Just adding the python version in case anyone needs class Solution: def myAtoi(self, str: str) -> int: # for removing white spaces str = str.strip() if not str: return 0 sign = -1 if str[0] == '-' else 1 str = str[1:] if str[0] in ['-', '+'] else str res = 0 for char in str: if not char.isdigit(): break res = res * 10 + int(char) if res * sign >= 2**31 - 1: return 2**31 - 1 if res * sign
why i got this error " module 'string' has no attribute 'letters'" in leetcode. Below is my code int myAtoi(string s) { if(s.length()==0) return 0; int i=0; if(i
Because there might be a space after digit or anywhere in middle, for eg : " 234 6hhs", in this test case there is space after 4 , so we will break after that and output will be 234
Working code on Leetcode , with updated tests class Solution { public: int myAtoi(string s) { int len = s.size(); int i = 0; int sign = 1; // postive long long int res = 0; while(i
I am newbie , have two doubts.
1) why we using long to define ans
2)in the line ans=ans*10+s[i]-' 0 ' what is purpose of this one'0'
I hope you will explain it😢😢
hi sure, :)
so see,
1. if we do not use long, and we use int, we get signed overflow error
Line 23: Char 23: runtime error: signed integer overflow: 912834723 * 10 cannot be represented in type 'int' (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior solution.cpp:23:23
this is because ans variable may add up to values which go beyond specific integer range, so we use long :)
2. so, the expression s[i] - '0' is a common way to convert a character representing a digit to its corresponding integer value.
for eg.
If s[i] is '5', its ASCII value is 53. Subtracting the ASCII value of '0' (48) from it gives 53 - 48 = 5, which is the integer representation of the digit.
@@AyushiSharmaDSA omg thanks mam 👍 . 😊
@@selvarajan4062 no worries :)
This Question is king of edge cases.
yes
True
I saw multiple video solutions but no one able to clear this much. Good job. Keep doing good work. Thanks!!
ok
thank you so much :)
🎯 Key points for quick navigation:
00:02 *Handle leading whitespace*
00:42 *Convert string to integer*
01:25 *Check sign (positive/negative)*
02:08 *Read until non-digit*
Made with HARPA AI
You don't know how happy i am after seeing your video in search result...every time.
Thank you so much Tushar, means a lot😊😊
Videos are helpful. Just adding the python version in case anyone needs
class Solution:
def myAtoi(self, str: str) -> int:
# for removing white spaces
str = str.strip()
if not str:
return 0
sign = -1 if str[0] == '-' else 1
str = str[1:] if str[0] in ['-', '+'] else str
res = 0
for char in str:
if not char.isdigit():
break
res = res * 10 + int(char)
if res * sign >= 2**31 - 1:
return 2**31 - 1
if res * sign
Clear explanation, every second was worth it!!
thank you so much 😊🙌
Detailed explanation for each case...!!Wow
Thank you :) glad it was helpful
really great explanation !!!! found this after watching some tuff explaining videos....keep doing well....
thank you, glad you liked it :)
I love your voice❤️,such a nice explanation keep uploading quality content.
Thank you, I will :)
Best explanation among all videos of this prob I have ever seen.....Thank You
Thank you so much Amarjeet, glad it was helpful 🤩😊
thankyou so much for such an easy explanation of this problem
Crystal clear Explanation. Thanks! Happy Makar Sankranti ! 😇
Thank you Chandra, and Happy Makar Sakranti to you too :)
she's looking like she's solving this for like days (might be because of all the edge cases)
REAL KODER
thank you :)
Great Explaination Ayushi ❤❤👋👋
thank you 🤗
Thank you so much ....your way of explanation is awesome☺
Most welcome 😊
Explained very well
thank you :)
Awesome explanation thanks for providing java solution as well means a lot you're an inspiration
Thank you Subhasis 😄
Thank you Ayushi, Good explantion
thank you, glad you liked it :)
Good Explaination...
Thanks 😊
Thanks Ayushi for your time.
Welcome Ashwani, glad it was helpful :)
Thanks Ayushi ,amazing explaination
Welcome, glad it was helpful 😊
Thanks Ayushi for this awesome explanation❤❤
@Ayushi Sharma
@@allmovies2473 welcome, glad it was helpful ❤️😊
I just opened youtube thinking that today's leetcode video has been uploaded here.😁
🥺🥺😊
Thanks Ayushi. Covered everything
Welcome, glad it was helpful :)
Explanation is best. Just wanted to know is cp mandatory for coding rounds of product based companies. If yes then how to begin.
Thanks a lot for this awesome explanation.
well explained !! thanks
Thanks 😊
Thanks, these videos are really helpful!
Welcome Surya, glad they are helpful 🤗
Very nice,crisp and intutive approach didi thanks a lot
Most welcome 😊
*line 24 & 25* should include equal to also , like this
if( sign==-1 && -1*ans
no, the problem statement condition states strictly out of range.
thanks Maam for the crystal and clear explanation 👏👏
thank you, glad it was helpful :)
thanks for the explanation mam ,respect your efforts
Great Explanation!!
glad you found it helpful :)
very nice explanation ....
Thank you :)
Thanks Ayushi
welcome :)
Clear explanation.. well done 👍
Thank you Rashid, glad it was helpful :)
i think in second while loop , it should be s[i]==' ' instead of s[0]==' ' .
bro she change the string from original s to s.substr(i) so it from starting that's why it is s[0]
very well explained
Superb explanation !
Thank you 😊
💯🔥
Reach ++;
Thank you :)
very nice explanation
Thank you 😊
very nicely explained❤
Thank you Rahul :)
Great Job!
Thanks!
Big fan ❤❤❤
I am too 😛😁
@@AyushiSharmaDSA 😁😁😁😁
awesome solution
thank you :)
why i got this error " module 'string' has no attribute 'letters'" in leetcode.
Below is my code
int myAtoi(string s) {
if(s.length()==0) return 0;
int i=0;
if(i
you are awesomeee....but at the same time, after seeing your Github, feeling like i'm just way too far from it🙂
thank you 🤗
Mam your voice ❤️❤️
Thanks Amit 😊
you earned a sub!!!!
Thank you 😊
Why are checking again s[0]==‘ ‘ in 2 nd while loop?
Because there might be a space after digit or anywhere in middle, for eg : " 234 6hhs", in this test case there is space after 4 , so we will break after that and output will be 234
@@AyushiSharmaDSA Thanks for ur reply.
I mean does isdigit function doesn’t consider spaces?
@@replyplz8874 No, it doesn't consider spaces, it will return true if there is digit otherwise false
@@AyushiSharmaDSA why s[0] == ' ' instead of s[i] == ' ' ??
@dipanshu apka bohottt bdaa fan h
ohh, agar yahi dipanshu bolta to zyada acha lgta 😜 btw thank you for letting me know :)
@@AyushiSharmaDSA 1 min usko btata hu wo krega comment 😂
nice
class Solution {
public int myAtoi(String s) {
//remove leading zeroes
StringBuilder sb = new StringBuilder(s);
while(sb.length()>0 && Character.isWhitespace(sb.charAt(0))){
sb.deleteCharAt(0);
}
if(sb.length()==0)
return 0;
boolean neg = false;
char ch = sb.charAt(0);
if(ch=='-'){
neg = true;
sb.deleteCharAt(0);
}
if(ch=='+'){
sb.deleteCharAt(0);
}
StringBuilder sb2 = new StringBuilder();
for(int i=0; i='0' && ch2
1030 cases are running out of 1084 cases...is it true for you as well?bcz i can't find any error in my code
you earned a sub
thank you, glad it was helpful :)
Can I get all your java coding session in one place..?So,I can practice in order.
Thank you!!
Abhi me apka he vedio dekh rahi thi😄
🥺🥺🥺🙌🏻
@@AyushiSharmaDSA and thank you for providing java code link🤩
what about alphabets ??
Thanks a lot!
You’re welcome ☺️
above while loop not removing spaces
Great thanks
just tell me what can I do to say just like you did, that this is very easy peoblem?
home theather pe lga ke suni h ye video itna acha mic h tumhara
If after trim string become empty then how it will check for sign
trim only removes the leading whitespaces, if string has digits, those will not be removed :)
Tnq u 🎉❤
whats the meaning of s[i]-'0'?
will it convert to int?
Yes
please provide reccursive solution of this question
Thanks mam 😌
like who is here after passing all the given test cases
Good JOb
Thanks Vishal :)
OP Solution
Thank you Saurabh :)
Thank you dI
ye ab nhi chalra hai.update soln bnao
Wonderful Explanation ,I understand the each and every line.
Thanks Manoj 🤗
Jab phli bar dekha toh lga ki aray yaar bachi pdha rhi hai phir LinkedIn pe naam search kiya toh Walmart phir Microsoft 😂😂😂
hehe, 😂😂
This quest should have been in HARDof leetcode.
Please, before uploading the video check it . Sometime solution is hide behind your face screen. Otherwise it's good . Thank for explanation 🙂.
Yeah, so sorry for this, will take care next time :)
ooooooo cutieeee🥲🙂🙂
Thank you
You're welcome
for "+ -12" it is showing 0
It should show 0
why cant we do this 😂
return stoi(s);
although all cases are accepted but in submission it it throwing RTE lol
thanks didi
ethna he hai kya questions string k ma'am abh recursion chalu karo aap 🥲
Thanx Sharma
Welcome :)
Thanks di
Welcome Pranav, glad it was helpful :)
some of the testcases failing
Working code on Leetcode , with updated tests
class Solution {
public:
int myAtoi(string s) {
int len = s.size();
int i = 0;
int sign = 1; // postive
long long int res = 0;
while(i
thanks for sharing ur code :)
easy tha but test case bekar the
After multiple tries, I came up with this solution:
class Solution {
public:
int myAtoi(string s)
{
int flag = 0;
long int result = 0;
int j = 0;
for(int i = 0; i < s.length(); ++i)
{
if(isdigit(s[i]))
{
result = result*10 + int(s[i]) - 48;
if(flag*result > 2147483647)
{
return 2147483647;
}
else if(flag*result < -2147483648)
{
return -2147483648;
}
++j;
if(flag == 0)
{
flag = 1;
}
}
else if(s[i] == '-' || s[i] == '+')
{
if(!flag && s[i] == '-')
{
flag = -1;
}
else if(!flag && s[i] == '+')
{
flag = 1;
}
else
{
break;
}
}
else if(j || flag || flag == -1)
{
break;
}
else if(s[i] != ' ')
{
return 0;
}
}
result *= flag;
return (int)result;
}
};
Awesome Explanation !!
thank you :)