I did 3 Basic Math questions on leetcode- reverse, palindrome and counting digits that are divisible as a continuation to the sheet. I found the counting digits that are divisible quite interesting.
i ran into an error when I ran the reverse question
Line 7: Char 22: runtime error: signed integer overflow: 964632435 * 10 cannot be represented in type 'int' (solution.cpp) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior solution.cpp:7:22
Basically, the integer goes beyond the max so there's an overflow, you can fix this by returning 0 when the digit is close to int max.
class Solution {
public:
int reverse(int x) {
int rev = 0;
while(x!=0){
int lastdigit = x % 10;
if((rev> INT_MAX/10)||(rev<INT_MIN/10)){
return 0;
}
rev = rev*10 + lastdigit;
x=x/10;
}
return rev;
}
};
Anyways Keep Coding #Noobcodergang