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.

题解:

  easy题

Solution 1

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

Solution 2

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

【LeetCode】009. Palindrome Number的更多相关文章

  1. 【LeetCode】9. Palindrome Number (2 solutions)

    Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click t ...

  2. 【LeetCode】9. Palindrome Number 回文数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...

  3. 【leetcode】9. Palindrome Number

    题目描述: Determine whether an integer is a palindrome. Do this without extra space. 解题分析: ^_^个人觉得这道题没有什 ...

  4. 【leetcode】 9. palindrome number

    @requires_authorization @author johnsondu @create_time 2015.7.13 9:48 @url [palindrome-number](https ...

  5. 【LeetCode】9 Palindrome Number 回文数判定

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

  6. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  7. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  8. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  9. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

随机推荐

  1. loadrunder脚本篇——文件读写操作

     函数说明 函数原型: size_t fwrite( const void *buffer, size_t size, size_t count, FILE *file_pointer ); 参数说明 ...

  2. ag-grid

    使用: import { AgGridVue } from "ag-grid-vue"; <ag-grid-vue style="width:100%;height ...

  3. Ubuntu启动自动登录并启动程序

    最近在研究Ubuntu,需要在系统启动之后自动登录,并且启动某个程序. 手上拿到的系统只有一个空桌面,其他嘛也没有,鼠标右键也不管用.于是借助自己的虚拟机研究发现,自动启动程序配置文件在: /home ...

  4. Struts2笔记04——Hello World Example(转)

    原文地址:https://www.tutorialspoint.com/struts_2/ [注释]项目结构,次序估计有误 通过学习Struts2的架构,我们可以知道,在Struts2 web应用中, ...

  5. Windows定时任务没有执行

    最近部署网站首页静态化程序,需要定时执行的,由于部署在Windows上,为了方便直接用Windows计划任务做定时了.跑了一段时间发现.首页的静态html文件日期一直是老的,手动执行程序会更新,怀疑任 ...

  6. NIO、Servlet3.0、HTTP1.1

    J2EE 6和Glassfish 3V正式发布了,J2EE 6正式发布了Servlet3.0, 为了能更好的对WEB2.0提供支持, 3.0添加了异步处理的机制. HTTP1.1相对于HTTP1.0的 ...

  7. samba Doc

    Samba-HOWTO-Collection中文翻译版(2.20) 2013年08月23日 ⁄ 综合 ⁄ 共 19460字 ⁄ 字号 小 中 大 ⁄ 评论关闭 Samba计划文档 (初稿) Samba ...

  8. MySQL-重做日志 redo log -原理

    [redo log buffer][redo log file]-原理 目录: 1.重做日志写入过程图 2.相关知识点汇总图 3.redo_log_buffer 原理 4.redo_log_file ...

  9. Logger日志级别说明及设置方法、说明

    日志记录器(Logger)是日志处理的核心组件.log4j具有5种正常级别(Level).日志记录器(Logger)的可用级别Level (不包括自定义级别 Level), 以下内容就是摘自log4j ...

  10. .NET应用程序默认使用管理员身份打开

    1.在源码的Properties目录中找到 app.manifest(如果没有进入第二步,有跳过第二步) 2.如果没有app.manifest文件可以打开项目属性,找到安全性项,勾上启用 ClickO ...