• 题目描述

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true

Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Follow up:

Coud you solve it without converting the integer to a string?

  • 解题思路

判断一个字符串是回文的算法很容易完成。但对于整数而言,是否也需要先将整数各位存储到数组,然后进行运算呢?

一开始的想法确实是这样,后来一想:

  1. 对于正整数,在转换数组的同时可以计算翻转值。
  2. 负数,根据示例,是不可能为回文数的。
  3. 0,肯定为回文。

感觉这个实现,复杂度依然和位数线性相关,leetcode外文无法访问了,也不能看到更好的解决方案了~

  • 实例代码
class Solution {
public:
bool isPalindrome(int x) {
if(x < )
return false;
if(x == )
return true; int origin = x;
int reverse = ;
while(x > )
{
reverse = (reverse*) + (x%);
x = x / ;
} return (reverse == origin);
}
};

leetcode笔记(四)9. Palindrome Number的更多相关文章

  1. Leetcode 题目整理-3 Palindrome Number & Roman to Integer

    9. Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. clic ...

  2. leetCode练题——9. Palindrome Number

    1.题目 9. Palindrome Number   Determine whether an integer is a palindrome. An integer is a palindrome ...

  3. 【LeetCode算法-9】Palindrome Number

    LeetCode第9题 Determine whether an integer is a palindrome. An integer is a palindrome when it reads t ...

  4. LeetCode记录之9——Palindrome Number

    LeetCode真是个好东西,本来闲了一下午不想看书,感觉太荒废时间了就来刷一道题.能力有限,先把easy的题目给刷完. Determine whether an integer is a palin ...

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

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

  6. LeetCode(9)Palindrome Number

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

  7. 【leetcode❤python】 9. Palindrome Number

    #回文数#Method1:将整数转置和原数比较,一样就是回文数:负数不是回文数#这里反转整数时不需要考虑溢出,但不代表如果是C/C++等语言也不需要考虑class Solution(object):  ...

  8. Palindrome Number - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Palindrome Number - LeetCode 注意点 负数肯定是要return false的 数字的位数要分奇数和偶数两种情况 解法 解法一: ...

  9. 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...

  10. leetcode bug & 9. Palindrome Number

    leetcode bug & 9. Palindrome Number bug shit bug "use strict"; /** * * @author xgqfrms ...

随机推荐

  1. PAT 1055 The World's Richest

    #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #i ...

  2. Python爬虫之图片懒加载技术、selenium和PhantomJS

    一.引入 2.概要 图片懒加载 selenium phantomJs 谷歌无头浏览器 3.回顾 验证码处理流程 一.今日详情 动态数据加载处理 1.图片懒加载 什么是图片懒加载? 案例分析:抓取站长素 ...

  3. easyui datagrid 获取行数据某个字段

    首先要能获取datagrid 的row对象 即:var row = $('#datagrid').datagrid('getData').rows[index]; 之后我们就可以通过类似row.nam ...

  4. JavaScript性能优化小知识总结(转)

    JavaScript的性能问题不容小觑,这就需要我们开发人员在编写JavaScript程序时多注意一些细节,本文非常详细的介绍了一下JavaScript性能优化方面的知识点,绝对是干货. 前言 一直在 ...

  5. python闭包&装饰器&偏函数

    什么是闭包? 首先还得从基本概念说起,什么是闭包呢?来看下维基上的解释: 在计算机科学中,闭包(Closure)是词法闭包(Lexical Closure)的简称,是引用了自由变量的函数.这个被引用的 ...

  6. Selenium2学习(十二)-- alert\confirm\prompt

    前言 不是所有的弹出框都叫alert,在使用alert方法前,先要识别出到底是不是alert.先认清楚alert长什么样子,下次碰到了,就可以用对应方法解决. alert\confirm\prompt ...

  7. zan-framework mysql连接

    ①根据文档内容要配置sqlmap连接池的读写白名单 http://doc.zanphp.io/zh/libs/connection_pool.html 示例代码 // demo.demo.demo_s ...

  8. CSS z-index的用法

    理清 position及z-index的用法: static :  无特殊定位,对象遵循HTML定位规则absolute :  将对象从文档流中拖出,使用left,right,top,bottom等属 ...

  9. ring0 ShadowSSDTHook

    SSDT:主要处理 Kernel32.dll中的系统调用,如openProcess,ReadFile等,主要在ntoskrnl.exe中实现(微软有给出 ntoskrnl源代码) ShadowSSDT ...

  10. 分享一个settings.xml

    <?xml version="1.0" encoding="UTF-8"?> <settings> <localRepositor ...