LeetCode 9.Palindrome Number2

来源:互联网 发布:淘宝免单微信群2017 编辑:程序博客网 时间:2024/05/29 14:26

针对上一题贴出另一种解法,但是速度没有上一篇文章的快。

class Solution {public:    bool isPalindrome(int x) {    if (x < 0)      {          return false;      }        int tmp = 1;        // 将tmp位数变为与n一致      while(x / tmp >= 10) // 防止tmp溢出      {          tmp *= 10;      }        // n = 0 表示所有位比较完      while(x != 0)      {          // 最高位 != 最低位          if (x / tmp != x % 10)          {              return false;          }            // 最高位 = 最低位 去掉最高位 去掉最低位          // 继续比较          x = (x % tmp) / 10;          tmp /= 100;      }        return true; }};


原创粉丝点击