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. Manacher算法[O(n)]

    问题描述: 输入一个字符串,求出其中最大的回文子串.子串的含义是:在原串中连续出现的字符串片段.回文的含义是:正着看和倒着看相同,如abba和yyxyy. 算法基本要点: 首先用一个非常巧妙的方式,将 ...

  2. Excel的查询函数vlookup和index使用

    需求 有一些省市的区县,有600多条数据,只有名称,没有编码.现在要根据名称去3000多条数据里面查询. 如图,拿出一部分数据来演示 vlookup 使用vlookup,由于vlookup只能查询数据 ...

  3. PHP: isset与empty的区别

    PHP的isset()函数 一般用来检测变量是否设置 功能:检测变量是否设置 返回值: 若变量不存在则返回 FALSE 若变量存在且其值为NULL,也返回 FALSE 若变量存在且值不为NULL,则返 ...

  4. 初学者的困惑:OOP与一般编程的区别

    *在写<程序猿的思维修炼>随笔中,我们大概猜想到了,OOP的思想更趋于模块化,更独立,因此称为一个个对象,本次随笔将对OOP和一般编程的区别有更详细的解释 面向对象编程的含义: 面向对象编 ...

  5. Django(十二)视图--利用jquery从后台发送ajax请求并处理、ajax登录案例

    一.Ajax基本概念 [参考]:https://www.runoob.com/jquery/jquery-ajax-intro.html 异步的javascript.在不全部加载某一个页面部的情况下, ...

  6. (九)微信小程序---for指令

    对于数据是列表 wxml <view wx:for="{{dataList}}">{{index}}-{{item}}</view> 我们可以看到上面的代码 ...

  7. php 和 文本编辑器火狐的配置

    个人比较习惯的编辑器和浏览器配置 Sublime Ctrl+Shitf+P 输入 install 安装扩展: 点开菜单 -> view -> showConsole (或者按住 Ctrkl ...

  8. C++ 检测物理内存以及磁盘空间

    BOOL CheckResource() { MEMORYSTATUSEX statex; statex.dwLength = sizeof (statex); GlobalMemoryStatusE ...

  9. c++数据结构排序

    #include<stdio.h> #include<stdlib.h> #include<time.h> typedef int ElemType; typede ...

  10. Mysql 事务隔离级别分析

    Mysql默认事务隔离级别是:REPEATABLE-READ --查询当前会话事务隔离级别mysql> select @@tx_isolation; +-----------------+ | ...