1. 程式人生 > >【leetcode】9. Palindrome Number

【leetcode】9. Palindrome Number

Description:

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example1:

Input: 121 Output: true

Example2:

Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

提交程式碼:

bool isPalindrome(int x) {
   if(x < 0)	return false;
   if (x == 0)	return true;
	
	int i=0,len=0,tmp,nums[11];

	while (x)
	{
		tmp = x % 10;
		nums[i++] = tmp;
		x /= 10;
		len++;
	}

	for (i = 0; i < len / 2 + 1; i++)
	{
		if (nums[i] != nums[len - i-1])
			return false;
	}

	return true;
}

執行結果: 在這裡插入圖片描述