题解 | #牛的回文编号#
牛的回文编号
https://www.nowcoder.com/practice/f864e31a772240f1b4310fbdc27fad48
1.考察知识点:
数组处理、双指针
2.编程语言:
C
3.解题思路:
暴力解法,直接每次取该数字个位,然后使用初始化为0的res,每次循环res * 10 + 个位数,得出反转之后的数字
判断其与原来的x值是否相等即可,注意在得出反转数字时,x值会变为0,需要在开始保存在x_old中
4.完整代码:
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param x int整型 * @return bool布尔型 */ bool isPalindrome(int x ) { // write code here if(x<0) return false; int res = 0; int x_old = x; while(x!=0) { int gewei = x%10; x /= 10; res = res*10 + gewei; } return x_old==res; }#面试高频TOP202#