Last active
February 24, 2017 06:10
-
-
Save yigger/654d6889c973aab3b3a5f20656bc80cc to your computer and use it in GitHub Desktop.
判断一个数是否是回文,如121 true, 133 false
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 从后往前算 == 从前往后算(当数字位数是偶数 或者 0) 或者 从后往前算/10 == 从前往后算(当数字位数是奇数) | |
*/ | |
bool isPalindrome(int x) { | |
if(x < 0 || (x != 0 && x % 10 == 0)) return false; | |
int sum = 0; | |
while(x > sum) { | |
sum = sum*10 + (x % 10); | |
x = x / 10; | |
} | |
return (x == sum || x == sum / 10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment