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

click to show spoilers.

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.

题意:判断一个整数是否为回文数。

思路:常规思路是,将数字转换成字符串或者用数组保存各位上的值,然后从两端开始遍历,判断是否为回文,或者将数反转以后,判断是否相等(这时要注意,若是数很大,怎么办,会溢出)。以上都不行,我就想,数为回文,要高位和低位上的值对应相等,那如何取得各个位上的值了?要是知道这个整数有多少位,就可以取余,或者求商得到对应位的值。按这个思路,我们首先求出的是这个数的位数,我们可以通过不断除10来得到位数。这里值得注意的是:每次比较完以后,如何找到下一对位置上值的比较。代码如下:

 class Solution {
public:
bool isPalindrome(int x)
{
if(x<) return false;
if(x<) return true; int base=;
while(x/base>=)
base*=; while(x)
{
int lNum=x/base;
int rNum=x%;
if(lNum !=rNum)
return false; x-=lNum*base;
x/=;
base/=;
}
return true;
}
};

[Leetcode] Palindrome number 判断回文数的更多相关文章

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

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

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

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

  3. 从0开始的LeetCode生活—9. Palindrome Number(回文数)

    题目大意: 判断输入的数字是不是回文数.所谓回文数就是正反读都一样的数字,比如说11,121,1221这样子的数字.负数不会是回文数. 解题思路: 思路一:如果这个数是负数,则返回false,否则用一 ...

  4. LeetCode OJ Palindrome Number(回文数)

    class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ...

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

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

  6. palindrome number(回文数)

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

  7. 9. Palindrome Number[E]回文数

    题目 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same b ...

  8. LeetCode 9. Palindrome Number(回文数)

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

  9. [LeetCode]9. Palindrome Number判断回文数字

    /* 查看网上的思路有两种: 1.每次取两边的数,然后进行比较 2.取数的倒置数,进行比较 */ public boolean isPalindrome1(int x) { if (x<0) r ...

随机推荐

  1. WordPress博客插件程序:搜索下拉框openSug

    百度搜索框下拉提示Wordpress组插件. 下载地址:https://www.opensug.org/faq/wp-content/uploads/2018/12/opensug.wordpress ...

  2. requests模块基础

    requests模块 .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { bor ...

  3. 【Java】关于Spring框架的总结 (一)

    本文总结一些关于Spring框架的理解,注意点及基础操作.如果有不对的地方,欢迎批评和建议.大家一起努力吧! Spring 框架简介 Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创 ...

  4. 永无BUG 注释

    /** *                   _ooOoo_ *                  o8888888o *                  88" . "88 ...

  5. python 产生有序整数序列

    其中一种方法 A = np.linspace(0,n,n)

  6. mutation与vuex区别

    vuex- action   1.当点发送过快,页面中渲染的内容与state中的数据不一致,vuex里面的state变得慢,且不持续更新 2.action中是可以做到页面中state中数据保持一致 责 ...

  7. pprof 查看goroutine

    package main import ( "net/http" "runtime/pprof" ) var quit chan struct{} = make ...

  8. Java中的原生数据类型

    Java中的原生数据类型(Primitive DataType)共有8种: 1)整型:     使用int表示(32位).2)字节型: 使用byte表示(从-128到127之间的256个整数).3)短 ...

  9. CSP201312-1:出现次数最多的数

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的“计算机职业资格认证”考试,针对计算机软件开发. ...

  10. MySQL性能分析和优化-part 1

    MySQL性能优化 平时我们在使用MySQL的时候,怎么评估系统的运行状态,怎么快速定位系统瓶颈,又如何快速解决问题呢? 本文总结了多年来MySQL优化的经验,系统介绍MySQL优化的方法. OS性能 ...