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.

思路:

不能用额外的空间,数字还有可能过大。 处理方法是把数字截为两半,后半段翻转与前半段对比

class Solution {
public:
bool isPalindrome(int x) {
int y = ; //把x的后半段反过来
if(x > && x % == ) //如果大于0的数字以0结尾 一定不是回文
return false;
if(x >= && x < ) //个位数一定是回文
return true;
while(y <= x)
{
if(y == x || (x / > && y == x / )) //偶数个数字 和奇术个数字都要考虑
return true;
y = y * + x % ;
x = x / ; //把x后面给y了的截掉
}
return false;
}
};

同样思路,更简洁的代码:

class Solution {
public:
bool isPalindrome(int x) {
int i = ;;
if ((x % == && x != ) || x < ) return false;
while (i < x) {
i = i * + x % ;
x = x / ;
}
return (i == x || i / == x);
}
};

【leetcode】Palindrome Number (easy)的更多相关文章

  1. 【leetcode】Palindrome Number

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

  2. 【LeetCode】Palindrome Number(回文数)

    这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...

  3. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  4. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  5. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  6. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  7. 【LeetCode】Palindrome Partitioning 解题报告

    [题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

  8. 【leetcode】Happy Number(easy)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  9. 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...

随机推荐

  1. mysql 游标嵌套循环实例

    BEGIN #Routine body goes here... ####所有的2个小时之前生成的待支付订单更新为已过期 DECLARE tmp_id INT; DECLARE tmp_order_i ...

  2. Session id实现通过Cookie来传输方法及代码参考

    1. Web中的Session指的就是用户在浏览某个网站时,从进入网站到浏览器关闭所经过的这段时间,也就是用户浏览这个网站所花费的时间.因此从上述的定义中我们可以看到,Session实际上是一个特定的 ...

  3. 那万恶的ssh真是麻烦

    设置为允许某组远程ssh ,key也放入相应的服务器了,死活提示,Permission denied (publickey). 而偏偏另外的用户相同的配置却又可以,这里找到答案,原来是跟目录的权限有关 ...

  4. iOS动画——文字晃动

    比如密码错误提示,需要文字左右晃动几次,以起到警示的作用. 为了调用使用方便,做一个分类.代码如下: .h文件 #import <QuartzCore/QuartzCore.h> @int ...

  5. 手机看youtube的方法|一个好用的VPN

    俗话说:网络无国界.但是由于天朝的限制,我们无法访问YouTube .谷歌.twitter等一些国外网站. 这个时候我们就需要FQ.最直接有效的方法就是:VPN(虚拟专用网络). 虚拟专用网络的功能是 ...

  6. 图像热点&图像映射

    图像映射 图像映射也称为图像热点. 作用: 让同一张图片上的不同区域,可以实现多个不同的超链接功能. 图示: <map>图像映射三步走: 图像映射的实现需要三方面配合完成: 1.图像映射容 ...

  7. Js 变量的作用域

    变量的作用域 所有的变量作用域是指在哪里可用哪里不可用 局部作用域和全局作用域 在js中以函数来分割作用域 在函数里面就叫做局部作用域,在函数外面的就叫全局作用域 在函数里面如果省略var 关键字 那 ...

  8. MySQL学习笔记之数据存储类型

    说明:本文是作者对MySQL数据库数据存储类型的小小总结. Numeric Type (数字类型) 1.TINYINT.SMALLINT.MEDIUMINT.INT.BIGINT主要根据存储字节长度不 ...

  9. jQuery Mobile里xxx怎么用呀?(集成篇)

    jQuery Mobile如何使用GA(Google Analytics)? 什么是GA: http://baike.baidu.com/view/34729.htm http://www.googl ...

  10. Discuz 哪些文件和文件夹需要777权限

    Discuz有强大的缓存能力,当然要想建立这些缓存,就需要对需要的文件和文件夹进行读写,因此给需要的文件和文件夹777权限(Linux服务器)是必不可少的,下面就列出了需要给777权限的文件和文件夹, ...