LeetCode刷题(C++)——Palindrome Number(Easy)

偏执的太偏执、 2022-06-16 01:42 242阅读 0赞

Determine whether an integer is a palindrome. Do this without extra space.

  1. class Solution {
  2. public:
  3. bool isPalindrome(int x) {
  4. if (x < 0)
  5. return false;
  6. int base = 1;
  7. while (x / base >= 10)
  8. base *= 10;
  9. while(x){
  10. if (x/base != x % 10) // x/base为最高位数字 x%10位最低位数字
  11. return false;
  12. x = (x%base) / 10; //去掉最高位数字和最低位数字
  13. base /= 100;
  14. }
  15. return true;
  16. }
  17. };

发表评论

表情:
评论列表 (有 0 条评论,242人围观)

还没有评论,来说两句吧...

相关阅读

    相关 52LeetCode_LeetCode手册

    虽然刷题一直饱受诟病,不过不可否认刷题确实能锻炼我们的编程能力,相信每个认真刷题的人都会有体会。现在提供在线编程评测的平台有很多,比较有名的有 hihocoder,LintCo

    相关 LeetCode指南

    以下是我个人做题过程中的一些体会:  1. LeetCode的题库越来越大,截止到目前,已经有321个问题了。对于大多数人来说,没有时间也没有必要把所有题目都做一遍(时间充

    相关 Leetcode

    39. 组合总和 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。