题目描述

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.

My solution(10ms,36.5MB)

整形转换成字符型,然后进行颠倒转换

class Solution {
public boolean isPalindrome(int x) {
if(x<0){
return false;
}else{
String a = x + "";
String aReverse = new StringBuilder(a).reverse().toString();
if(a.equals(aReverse)){
return true;
}else{return false;}
}
}
}

Other solution(6ms,35.1MB):

这个方法很神奇啊!Ψ( ̄∀ ̄)Ψ

先排除负数,然后把整数分成两个部分,x为前一半,rev为后一半,例:32499423

并且rev还是已经倒转过后的数字,即3249

最后比较一下两个是否相同


如果是奇数个数字,例:12321

则rev为123,x为12

然后比较123/10=12和12是否相等

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

LeetCode第九题—— Palindrome Number(判断回文数)的更多相关文章

  1. [Leetcode] Palindrome number 判断回文数

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

  2. 从0开始的LeetCode生活—9. Palindrome Number(回文数)

    题目大意: 判断输入的数字是不是回文数.所谓回文数就是正反读都一样的数字,比如说11,121,1221这样子的数字.负数不会是回文数. 解题思路: 思路一:如果这个数是负数,则返回false,否则用一 ...

  3. Leetcode 9. Palindrome Number(判断回文数字)

    Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...

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

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

  5. leetcode 第九题 Palindrome Number(java)

    Palindrome Number time=434ms 负数不是回文数 public class Solution { public boolean isPalindrome(int x) { in ...

  6. LeetCode OJ Palindrome Number(回文数)

    class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ...

  7. palindrome number(回文数)

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

  8. 9. Palindrome Number[E]回文数

    题目 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same b ...

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

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

随机推荐

  1. Spring Boot多数据库配置

    #datasourcespring.datasource.url=jdbc:mysql://120.26.246.185:3306/gaea?&useSSL=falsespring.datas ...

  2. [待解决]报错:JSON parse error: Unexpected character

    {"code":"9999","message":"JSON parse error: Unexpected character ...

  3. PyUnit (unittest) 的用法

    PyUnit(unittest) 是 Python 自带的单元测试框架,用于编写和运行可重复的测试.PyUnit 是 xUnit 体系的一个成员,xUnit 是众多测试框架的总称,PyUnit 主要用 ...

  4. 怎样在Cocos2d-x中使用Lua脚本

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013321328/article/details/25699545 笔者使用的是Cocos2d-x ...

  5. mysql 记录(record)

    以下内容来源于<mysql内核:Innodb存储引擎 卷1> 简单介绍物理记录和大记录.仅为理解mysql 索引基础 存储结构这一章节而写. mysql的默认存储引擎为Innodb.Inn ...

  6. JS获取CkEditor在线编辑的内容

    参考博文:[实践]获取CKEditor的html文本.纯文本.被选中的内容及赋值 1.获取CKEditor被选中的内容 var mySelection = CKEDITOR.instances.WOR ...

  7. 关闭swap

    #(1)临时关闭swap分区, 重启失效;   swapoff  -a #(2)永久关闭swap分区 sed -ri 's/.*swap.*/#&/' /etc/fstab

  8. (一)环境搭建——Django

    实验环境准备: 安装django # cmd中 pip install Django 第一个django项目HelloWorld # 在D:/Python test 下创建一个helloworld项目 ...

  9. mysql 学习之1 mysql的基本语法

    转载一位csdn中 乍得12138前辈的 转载:https://blog.csdn.net/qq_26200347/article/details/79781882

  10. redis安装到本地服务的方法

    要安装Redis,首先要获取安装包. Windows的Redis安装包需要到以下GitHub链接找到. 链接:https://github.com/MSOpenTech/redis 打开网站后,找到R ...