题目描述

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. Bootstrap入门及其常用内置实现

    BootStrap是一个专门做页面的 1.BS是基于HTML CSS JS 的一个前端框架(半成品) 2.预定义了一套CSS样式与JQurey实现 3.BS和Validation类似,都是JQ的插件, ...

  2. nginx新增简单下载站点

    配置nginx server { autoindex on;# 显示目录 autoindex_exact_size on;# 显示文件大小 autoindex_localtime on;# 显示文件时 ...

  3. Java多线程sleep和wait的区别,总结得非常好。

    我们都知道sleep是让线程休眠,到时间后会继续执行,wait是等待,需要唤醒再继续执行,那么这两种方法在多线程中的表现形态,它们各有什么区别呢? 可以总结为以下几点. 使用上 从使用角度看,slee ...

  4. java oop第10章_JDBC03(MVC分层模式)

    引言:在进行程序开发的时候,为了更加利于程序的管理我们引入了新的开发模式MVC分层模式,即按功能将程序代码分别分为M(Model模型).V(View视图).C(Controller控制器)三个组成部分 ...

  5. windows 和 linux 多线程

    学习了几天多线程技术,做个总结,便于记忆. 一般 多线程传递参数 为 void*  所以会有一个强制转换过程  (int*) (void *)等,传递多个参数选择 结构体指针.为了避免多个线程访问数据 ...

  6. Effective C++之条款1:视C++为一个语言联邦

    C++中的sub-languages有如下四种: C Object-Oriented C++:   (classes ,encapsulation(封装),inheritance(继承),polymo ...

  7. GitHub排名TOP30的机器学习开源项目

    对于机器学习者来说,阅读开源代码并基于代码构建自己的项目,是一个非常有效的学习方法.看看以下这些Github上平均star为3558的开源项目,你错了哪些? 1. FastText:快速文本表示和文本 ...

  8. Centos6.5升级安装openssh7.7p1

    Centos6.5升级安装openssh7.7p1  关于OpenSSH漏洞   2016年1月14日OpenSSH发布官方公告称, OpenSSH Client 5.4~7.1 版本中未公开说明的功 ...

  9. leetcode- 距离顺序排序矩阵单元格

    C++解法: #include <iostream> #include <vector> #include <map> #include <algorithm ...

  10. Servlet - request对象相关

    request对象中封装了当前请求的所有请求信息, request对象由Tomcat服务器创建, 并作为实参传递给处理请求的Servlet的service()方法 1. 获取请求头数据 // 获取请求 ...