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. Android系列教程(十六) 在电脑上装Android

    [软件准备] 1.LiveAndroid v0.3 liveCD [点击下载] 2.VirtualBox 3.0.4 [点击下载]       [图片安装流程]       主要安装思路为:通过vir ...

  2. RobotFramework自动化4-批量操作案例

    前言 有时候一个页面上有多个对象需要操作,如果一个个去定位的话,比较繁琐,这时候就可以定位一组对象.Selenium2library提供了Get Webelements 关键字,用于定位一组元素 以百 ...

  3. go test 单元函数测试

    首先安装单元测试包,go get github.com/smartystreets/goconvey/convey 源程序如下,定义了加减乘除4个函数 package test222 import ( ...

  4. eclipse 设置打开java文件代码自动折叠

    eclipse 设置打开java文件代码自动折叠 java: windows/preference/java/editor/folding->enable folding 可以在里面设置所要折叠 ...

  5. 通过Web启动本地应用程序

    通过自定义协议在Web中启动本地应用程序 实例是打开本地安装的Word程序   注册自己的协议Windows Registry Editor Version 5.00 [HKEY_CLASSES_RO ...

  6. 阿里的STORM——JSTORM

    看介绍文档貌似挺好:https://github.com/alibaba/jstorm   阿里拥有自己的实时计算引擎 类似于hadoop 中的MR 开源storm响应太慢 开源社区的速度完全跟不上A ...

  7. 支持5G-WiFi的安卓设备搜索不到5G信号解决方法

    安卓设备必须获得root权限,然后修改 /system/etc/wifi/nvram_net.txt 文件, 将ccode = CN 改为 ccode = ALL.保存并重启即可. 三星EK-GC11 ...

  8. Ubuntu14.04下Neo4j图数据库官网安装部署步骤(图文详解)(博主推荐)

    不多说,直接上干货! 说在前面的话  首先,查看下你的操作系统的版本. root@zhouls-virtual-machine:~# cat /etc/issue Ubuntu 14.04.4 LTS ...

  9. 我对REST的理解

    1:rest的由来 REST即表述性状态传递(英文:Representational State Transfer,简称REST) 通俗点说:资源在网络中以某种表现形式进行状态转移. 源于REST之父 ...

  10. Dynamic Programming for TSP

    See how Dynamic programming working for TSP: Check this link: http://www.youtube.com/watch?v=IUzE1Mb ...