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

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

推断一个数是不是回文数。首先负数不可能是回文数;其次要求不能用“额外”的存储空间,这句话的意思并不是表示不能用存储空间。应该是表示不能用过多的存储空间;那么能够推断首位与尾位是数值是否相等,再去掉头尾,再次推断首位和尾位是否相等。直至该值为0为止。

class Solution {
public:
bool isPalindrome(int x) {
int a = static_cast<int>(floor(log10(x)) + 1);
int b= static_cast<int>(pow(10, a - 1)); if (x < 0)
return false;
while (x)
{
if (x % 10 != x / b)return false; x = x % b;//去掉最高位
x = x / 10;//去掉最低位
//注意,以上两够不可逆,取余不一定能去掉最后一位
b = b / 100;
}
return true;
}
};

LeetCode(35):Palindrome Number的更多相关文章

  1. Leetcode练习题 Palindrome Number

    9. Palindrome Number Question: Determine whether an integer is a palindrome. An integer is a palindr ...

  2. Leetcode 9. Palindrome Number(水)

    9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...

  3. leetcode 9 Palindrome Number 回文数

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  4. LeetCode 9. Palindrome Number (回文数字)

    Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...

  5. Leetcode 9. Palindrome Number(判断回文数字)

    Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...

  6. [LeetCode][Python]Palindrome Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/palindr ...

  7. 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]

    题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  8. [LeetCode] 9.Palindrome Number - Swift

    Determine whether an integer is a palindrome. Do this without extra space. 题目意思:判断一个整数是否是回文数 例如:1232 ...

  9. [LeetCode] 9. Palindrome Number 验证回文数字

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

随机推荐

  1. ubuntu C++开发环境

    最近在VM中装了Ubuntu,为了开发程序,于是在网上找了些由于C/C++开发环境搭建的资料,供大家参考. 以下文字主要讲如何搭建Code::Blocks+wxWidgets. 搭建步骤: 1.安装编 ...

  2. 解决“listView点击一个Item,另外几个Item也跟着改变”的问题

    如图所看到的: 我点击Item,右边的checkBox就会对应的变化.可是当我第一次做的时候.点击第一个Item,右边的checkBox变为绿色,可是当我listView往下拉的时候,发现以下也有是绿 ...

  3. C语言变量的类型和存储位置

    . C语言变量主要分为全局变量.静态全局变量.局部变量.静态局部变量和寄存器变量.其中静态变量用static关键字进行修饰.程序所占用的内存可以分为以下几个部分: ()代码段-存放程序代码,只读的,不 ...

  4. 开启hadoop和Hbase集群的lzo压缩功能(转)

    原文链接:开启hadoop和Hbase集群的lzo压缩功能 问题导读: 1.如何启动hadoop.hbase集群的压缩功能? 2.lzo的作用是什么? 3.hadoop配置文件需要做哪些修改? 首先我 ...

  5. 混沌数学之Arnold模型

    相关软件混沌数学之离散点集图形DEMO 相关代码: class ArnoldEquation : public DiscreteEquation { public: ArnoldEquation() ...

  6. 点云数据(point cloud) 【转】

    转自caimagic的专栏 一:什么是点云数据 点云数据是指在一个三维坐标系统中的一组向量的集合.这些向量通常以X,Y,Z三维坐标的形式表示,而且一般主要用来代表一个物体的外表面形状.不经如此,除(X ...

  7. DIV焦点事件详解 --【focus和tabIndex】​

    添加 tabindex='-1' 属性: 默认:获取不到焦点事件(blur) 1 <div class="wl-product" id="wl-product&qu ...

  8. 【小程序】wxs使用

    wxs使用 WXS(WeiXin Script)是小程序的一套脚本语言,结合WXML,可以构建出页面的结构. wxs可以说就是为了满足能在页面中使用js存在的,在wxml页面中,只能在插值{{ }}中 ...

  9. idea unicode自动转码设置

    idea unicode自动转码设置 File > Settings > Editor > File Encodings 右侧 Properties Files 中 选中 Trans ...

  10. OleView.exe:查看机器上的COM 组件。

      OleView.exe可以查看机器上安装的所有COM组件的类别以及各个类别下的COM组件.