题解 | #牛的回文编号#
牛的回文编号
https://www.nowcoder.com/practice/f864e31a772240f1b4310fbdc27fad48
知识点:回文串
这道题目有很多解法,可以用数字进行反转,也可以直接将数字转换成字符串,判定字符串是否对称,这里只展示下通过数字进行反转的方法。利用%运算得到最低位数字,加入到目标最高位中,再将其除以10,去除最低位,重复以上操作,直至为零。
Java题解如下
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param x int整型 * @return bool布尔型 */ public boolean isPalindrome (int x) { // write code here return x == getNum(x); } private int getNum(int x) { int res = 0; while(x > 0) { res = res * 10 + x % 10; x /= 10; } return res; } }