mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
Leetcode day1
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
class Solution{
|
||||
public:
|
||||
bool isPalindrome(int x){
|
||||
int rev = 0;
|
||||
if(x<0 || x%10 == 0 && x!=0){
|
||||
return false;
|
||||
}
|
||||
while(x>rev){
|
||||
rev = (rev*10) + (x%10);
|
||||
x/=10;
|
||||
}
|
||||
return ( x==rev || x = rev/10);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
An easy problem but with a common pitfall hence a tricky one.
|
||||
The idea is to only check till half of the number and not full where rev*10 exceeds int's range for larger values
|
||||
Two pointer method is quite verbose as we check equality from either side using two pointers and return false if there's a mismatch
|
||||
Reference in New Issue
Block a user