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. 【linux】linux查看文件大小,磁盘大小

    查看指定目录下 文件或目录大小超过多少的 查看 /backup/tomcat7/ 目录下 超过500M大小的文件 并展示 文件详情 find /backup/tomcat7/  -type f -si ...

  2. jQuery插件——可以动态改动颜色的jQueryColor

    1.请选择代码框中所有代码后, 保存为 jquery.color.js /*! * jQuery Color Animations v@VERSION * https://github.com/jqu ...

  3. spring service事务传播

    spring定义的事务行为有以下几种: REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务.这是最常见的选择. SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行. ...

  4. 用emoji表情包来可视化北京市历史天气状况!

    用emoji表情包来可视化北京市历史天气状况!   最近有了一个突如其来的想法,主要是看到了R社区有大神做了emoji表情包,并已经打通了ggplot的链接,所以想用ggplot结合emoji表情做一 ...

  5. 洛谷P1772 [ZJOI2006]物流运输 题解

    题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格的管理和跟踪. ...

  6. insert-delete-getrandom-o1-duplicates-allowed

    https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/ public class Randomized ...

  7. jquery获取元素各种宽高及页面宽高总结

    window.onload=function(){ var a = $("#div").width(),//width()返回元素的宽高,不包括padding/border/mar ...

  8. htmlDOM树

    原文: https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM Shadow DOM API  研究一 ...

  9. COM中的几个基本概念

    类厂 组件结构示例 DllGetClassObject COM库与类厂的交互

  10. C++变量未进行初始化时的默认值

      对于built-in类型,未初始化的变量的值是undefined value. 对于自定义类型,未初始化变量的值是类型默认构造函数指定的值.