LeetCode第九题—— Palindrome Number(判断回文数)
题目描述
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(判断回文数)的更多相关文章
- [Leetcode] Palindrome number 判断回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- 从0开始的LeetCode生活—9. Palindrome Number(回文数)
题目大意: 判断输入的数字是不是回文数.所谓回文数就是正反读都一样的数字,比如说11,121,1221这样子的数字.负数不会是回文数. 解题思路: 思路一:如果这个数是负数,则返回false,否则用一 ...
- Leetcode 9. Palindrome Number(判断回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- leetcode 第九题 Palindrome Number(java)
Palindrome Number time=434ms 负数不是回文数 public class Solution { public boolean isPalindrome(int x) { in ...
- LeetCode OJ Palindrome Number(回文数)
class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ...
- palindrome number(回文数)
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- 9. Palindrome Number[E]回文数
题目 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same b ...
- 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...
随机推荐
- WSGI是什么?
WSGI是什么? WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway Interface ,是为 Python 语言定义 ...
- Pandas之read_excel()和to_excel()函数解析
read_excel() 加载函数为read_excel(),其具体参数如下. read_excel(io, sheetname=0, header=0, skiprows=None, skip_fo ...
- Docker搭建 oracle
1-1.docker run -d -p 11521:1521 --name sf2_oracle11g 镜像ID # -p:端口映射,此处映射主机端口 1-2.查看启动 docker logs - ...
- python_django_urls基础配置
url配置:请求地址与views函数的匹配 首先,指定根级url配置文件,默认为setting.py中的ROOT_URLCONF='项目名.urls'(俺们也不用去修改啥) 我们urls有两个,一个是 ...
- ElementUI的使用
一.安装 1.安装 在项目目录下执行 npm i element-ui -S 2.引入 在 main.js 中写入以下内容: import ElementUI from 'element-ui'; i ...
- d3操作svg路径动画,及dom移动
图片跟随路径循环运动,dom也跟着路径运动(利用实时获取坐标位置的方法,改变transform) 1,准备路径 a,自己脑补路径 b,在ps上画好,然后在保存成png-24图片,背景透明,在网站htt ...
- (转载)openwrt nginx
ln -s ../../../feeds/packages/net/fcgiwrap/ fcgiwrap 本帖最后由 cjd6568358 于 2018-6-4 11:21 编辑 刚又把路由器重置重新 ...
- J2SE基础-环境配置
学习资料:毕向东视频 1.为何配置Path? 使用javac编译文件时,先找path里设置的java路径. 如果不配置Path,在命令提示行中,则只能进入bin目录后,才能执行javac,jar等命令 ...
- thinkphp 数据缓存
在ThinkPHP中进行缓存操作,一般情况下并不需要直接操作缓存类,因为系统内置对缓存操作进行了封装,直接采用S方法即可,例如: 缓存初始化 // 缓存初始化 S(array('type'=>' ...
- poi之Excel(在线生成)下载
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. poi之Excel下载 @RequestMappi ...