9. Palindrome Number

官方的链接:9. Palindrome Number

Description :

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.

问题描述

判断一个数是否是回文数

思路

最直接简单的方法时直接反转数字进行判断,但是要考虑溢出的问题,而且需要处理整数的所有位数。根据回文数(都属于自然数)的定义,回文数是对称的,只需反转一半的数字即可。

注意问题:

  1. 特殊值问题:大于0的数字,且个位数是0,明显不是。小于0的负数,明显不是
  2. 数字是奇数位:反转的数字/10=前半部分
  3. 数字是偶数位:反转的数字=前半部分
  4. 因此关键的判断条件就是:反转的数字 < 前半部分

回文数-维基百科
回文数-百度百科

其他的可以參考官网的讨论,自己总结并写代码

[github-here]

 public class Q9_PalindromeNumber {
public boolean isPalindrome(int x) {
if (x < 0 || (x > 0 && x % 10 == 0)) {
return false;
}
int revX = 0;
while (revX < x) {
revX = revX * 10 + x % 10;
x /= 10;
}
return (revX == x || revX / 10 == x);
}
}

Q9:Palindrome Number的更多相关文章

  1. [LeetCode 题解]:Palindrome Number

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Determine ...

  2. No.009:Palindrome Number

    问题: Determine whether an integer is a palindrome. Do this without extra space. 官方难度: Easy 翻译: 不使用额外空 ...

  3. leetcode:Palindrome Number

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

  4. LeetCode专题-Python实现之第9题:Palindrome Number

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  5. leetcode:Reverse Integer 及Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...

  6. 65. Reverse Integer && Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, re ...

  7. 有趣的数-回文数(Palindrome number)

    文章转自http://blog.163.com/hljmdjlln@126/blog/static/5473620620120412525181/ 做LC上的题"Palindrome num ...

  8. No.009 Palindrome Number

    9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...

  9. 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List

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

随机推荐

  1. 前端构建工具gulp超详细配置, 使用教程(图文)

    流程 1. 输入命令(可以使用git bash或者命令控制台cmd) npm install -g gulp 安装全局gulp命令 2. 创建一个项目文件夹, 当前项目文件夹下输入命令npm init ...

  2. 手把手教你如何玩转CLion

    声明:配置是基于CLion的2019.1版本 〇.CLion简介 一.安装 \(JetBrains\)官方下载地址:CLion2019.3 百度网盘:CLion2019.1 个人觉得还是2019.1版 ...

  3. 016.Oracle数据库,取本季度第一天,取本季度最后一天

    /*取本季度第一天,取本季度最后一天*/ SELECT trunc(sysdate, 'Q') AS 本季度第一天 , add_months(trunc(sysdate, ) AS 本季度最后一天 F ...

  4. 洛谷题解P1047 校门外的树

    题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,…,L,都种有 ...

  5. FC 与 FB 与 OB 的区别,时间标记冲突与一致性检查 有详细的步骤

    关键字1 组织块的程序是由用户自己编写. 关键字2 时间标记冲突与一致性检查 有详细的步骤. 关键字3 FC 与 FB 与 OB 的区别?   (一)功能 功能块 区别 ? FB 和FC均为 用户编写 ...

  6. CAN分帧发送程序说明

    试验平台 仅仅 需要一台主机 一台 周立功 CAN 助手, 一个232 助手就OK ICAN 协议 资源节点地址 电脑 我认为是0x01 51单片机主机的地址 是 0x1f 建立连接的 功能码 是0x ...

  7. C++路径的整理

    写C++,路径的问题一直都让人很头疼,抽空整理一些方法:也许以后会用到: 1."./" 加不加都一样,就是指当前目录 2."../" 表示当前目录的上级目录,即 ...

  8. CentOS 6.x 重置root 密码

    1.重启,进入启动界面,快速按e,进入GNU GRUB界面. 2.选择第二项,按e,进行编辑. 3.在末尾输入1或single,回车,返回上一界面,还是选第二项,按b,进入单用户模式. 此时输入命令  ...

  9. 如何更改RStudio(或R)中的默认目录

    方法一: Session -> Set Working Directory -> Choose Directory ... or shortcut (Ctrl+Shift+H) 方法二 s ...

  10. Flutter Windows下AndroidStudio环境搭建

    目前同类产品比较知名的有ReactNative,Flutter还有国内那家了uniapp了,流畅度理论上Flutter最快 官网:https://flutter.dev/docs/get-starte ...