Skip to content

Instantly share code, notes, and snippets.

@yigger
Last active February 24, 2017 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yigger/654d6889c973aab3b3a5f20656bc80cc to your computer and use it in GitHub Desktop.
Save yigger/654d6889c973aab3b3a5f20656bc80cc to your computer and use it in GitHub Desktop.
判断一个数是否是回文,如121 true, 133 false
/*
* 从后往前算 == 从前往后算(当数字位数是偶数 或者 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