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


题目标签:Math

  题目给了我们一个int x, 让我们判断它是不是回文数字。

  首先,负数就不是回文数字,因为有个负号。

  剩下的都是正数,只要把数字 reverse 一下,和原来的比较,一样就是回文。

  当有overflow 的时候,返回一个负数就可以了(因为负数肯定不会和正数相等)。

  

Java Solution:

Runtime beats 61.47%

完成日期:06/12/2017

关键词:Palindrome

关键点:reverse number

 class Solution
{
public boolean isPalindrome(int x)
{
if(x < 0)
return false; int pd = reverse(x); return x == pd ? true : false;
} public int reverse(int num)
{
int res = 0; while(num != 0)
{
int tail = num % 10;
int newRes = res * 10 + tail; if((newRes - tail) / 10 != res) // check overflow
return -1; // -1 will not equal to positive number res = newRes;
num = num / 10;
} return res;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 9. Palindrome Number (回文数字)的更多相关文章

  1. 【LeetCode每天一题】Palindrome Number( 回文数字)

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

  2. leetcode 9 Palindrome Number 回文数

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

  3. [LeetCode]9. Palindrome Number回文数

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

  4. Leetcode 3——Palindrome Number(回文数)

    Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过 ...

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

    这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...

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

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

  7. [LeetCode] Prime Palindrome 质数回文数

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

  8. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. LeetCode Valid Palindrome 有效回文(字符串)

    class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...

  10. 9. Palindrome Number 回文 my second leetcode 20170807

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

随机推荐

  1. 冒泡 [Python]

    冒泡Python class BubbleSort: def __init__(self): self.initArr() def initArr(self): self.arrInfo = [60, ...

  2. Ps 快捷键全解

    一.工具箱(多种工具共用一个快捷键的可同时按[Shift]加此快捷键选取)矩形.椭圆选框工具 [M]移动工具 [V]套索.多边形套索.磁性套索 [L]魔棒工具 [W]裁剪工具 [C]切片工具.切片选择 ...

  3. Spring与Struts2集成开发

    Struts2和Spring都是不错的开源框架,Spring与Struts2集成开发,把二者结合在一起使用,开发效果更佳,效率杠杠的.下面介绍一下如何将Spring与Struts2集成在一起开发.分七 ...

  4. Markdown(github)语法

    << 访问 Wow!Ubuntu NOTE: This is Simplelified Chinese Edition Document of Markdown Syntax. If yo ...

  5. AdMob设计工具google web designer

    一.google web designer工具中文文档: https://support.google.com/webdesigner?hl=zh-Hans#topic=3227692 我用的版本:应 ...

  6. 前端axios发送的数据后端接收不到(没有自动依赖注入)可能的原因

    前端请求头content-type没有进行正确设置,后端无法解析该类型数据,比如说: 后端若想接收json类型的数据,则需要配置相应的转换器,(spring中可使用@RequestBody注解),若没 ...

  7. Linux kernel memory-faq.txt

    ## Linux kernel memory-faq.txt What is some existing documentation on Linux memory management? Ulric ...

  8. java计算两地距离(公里)

    //目标经度,目标纬度,自己经度,自己纬度 public static double getDistance(double lon1, double lat1, double lon2, double ...

  9. Sublime Text 3 快捷键(转载)

    本文转自:https://segmentfault.com/a/1190000002570753 (欢迎阅读原文,侵删) Sublime Text 3 快捷键精华版 Ctrl+Shift+P:打开命令 ...

  10. 通过request对象获取客户端的相关信息

    通过request对象获取客户端的相关信息 制作人:全心全意 通过request对象可以获取客户端的相关信息.例如HTTP报头信息.客户信息提交方式.客户端主机IP地址.端口号等等. request获 ...