题目描述

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. apache httpd 2.4 配置

    [authz_core:error] [pid 19562] [client 10.0.0.22:45424] AH01630: client denied by server configurati ...

  2. linux模范配置文件格式

    模范配置文件 #--------------------------------------------------------------------- # Global settings #--- ...

  3. CentOS6.5下RPM包安装MySQL5.6.35

    基本环境:VMware10.0+CentOS6.5+MySQL5.6.35 .查看操作系统相关 shell> cat /etc/issue shell> uname -a .创建需要下载r ...

  4. HXY烧情侣

    题目描述 众所周知,HXY已经加入了FFF团.现在她要开始喜(sang)闻(xin)乐(bing)见(kuang)地烧情侣了.这里有n座电影院,n对情侣分别在每座电影院里,然后电影院里都有汽油,但是要 ...

  5. 2018-2-13-win10-uwp-音频

    title author date CreateTime categories win10 uwp 音频 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23 ...

  6. usermod -修改使用者帐号

    总览 SYNOPSIS usermod [-c comment] [-d home_dir [-m]] [-e expire_date] [-f inactive_time] [-g initial_ ...

  7. 面对对象(JS)

    面对对象的三大特征:封闭.继承.多态 七大基本原则:    1.单一职责    2.开闭原则    3.里氏替换    4.依赖倒置    5.接口隔离    6.迪米特法则    7.01组合/聚合 ...

  8. CSIC_716_20191206【并发编程理论基础】

    进程:正在执行的一个过程,进程是对正在执行过程的一个抽象.区别于程序, 进程的三种状态:  进程是动态的. 就绪态ready:   进程具备运行状态,等待操作系统分配处理器 运行状态running:进 ...

  9. sublime Text3安装及配置与解决安装插件失败

    1.下载sublime Text3的官网:https://www.sublimetext.com/32.安装Package Control   下载Package Contoral地址: 链接:htt ...

  10. Kotlin Doc

    { https://www.runoob.com/kotlin/kotlin-eclipse-setup.html }