[LeetCode]Palindrome Number

class Solution {
//compare both the left most and the right most digit, 
//if not equal return false
public:
	bool isPalindrome(int x) {
		// Start typing your C/C++ solution below
		// DO NOT write int main() function
		if(x < 0) return false;
		int dvsNow = 1;
		while(x/dvsNow >= 10)
			dvsNow *= 10;
		while(x != 0)
		{
			int l = x/dvsNow;
			int r = x%10;
			if(l != r) return false;
			x = (x%dvsNow)/10;//take off both the left most and the right most digit
			dvsNow /= 100;
		}
		return true;
	}
};

second time

class Solution {
public:
    bool isPalindrome(int x) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(x < 0) return false;
        
        long long target = x;
        //if(target < 0) target = -target; 
        long long reverse = 0;
        long long curNum = target;
        while(curNum != 0)
        {
            reverse = 10*reverse+curNum%10;
            curNum /= 10;
        }
        
        return reverse == target;
    }
};


你可能感兴趣的:([LeetCode]Palindrome Number)