zoukankan      html  css  js  c++  java
  • 【Leetcode】Palindrome Number

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

    思路:若使用【Leetcode】Reverse Integer 的方法。推断反转后的整数是否与原整数同样,则可能出现溢出情况;又由于题目要求不适用额外空间,能够之间对照整数第一个与最后一个数的值。再依次类推。

    class Solution {
    public:
        bool isPalindrome(int x) {
            if(x < 0)   return false;
            
            int bitNum = 0;
            int temp = x;
            while(temp != 0)
            {
                temp /= 10;
                bitNum++;
            }
            
            for(int i = 1; i <= bitNum / 2; i++)
            {
                if((x / (int)pow(10, bitNum - i)) % 10 == (x % (int)pow(10, i)) / (int)pow(10, i - 1))
                    continue;
                else
                    return false;
            }
            
            return true;
        }
    };

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    Nginx配置文件详解
    Mycat概述
    日志切割之Logrotate
    js数组(二)
    js数组(一)
    sass颜色
    scss
    HTML5新属性
    HTML5新元素
    Bootstrap
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4840504.html
Copyright © 2011-2022 走看看