java - 回文数

扫码查看

我必须确定整数是否是JAVA中的回文。我需要帮助来了解为什么我编写的代码会返回Time Limit Exceeded

class Solution {
    public boolean isPalindrome(int x) {
        int reverse = 0;
        int remainder = 0;
        int originalX = x;
        boolean isPalindrome = false;


        while(x >=1)
        {
            remainder = x % 10;
            reverse = reverse * 10 + remainder;
            remainder = x / 10;
        }

        if (reverse == originalX){
            isPalindrome = true;
        }

        return isPalindrome;

    }
}

最佳答案

这里有一些经过修改和测试的代码,

public boolean isPalindrome(int x)
   {

      int reverse = 0;
      int remainder = 0;
      int originalX = x;

      while (x > 0)
      {
         remainder = x % 10;
         reverse = reverse * 10 + remainder;
         x = x / 10;
      }

      return (reverse == originalX);

   }

10-05 22:48
查看更多