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 这 ...
随机推荐
- Jenkins和Docker
由于采用了Docker版的Jenkins,导致在Jenkins里无法调用Docker命令行工具进行Docker镜像构建 有三种解决方案: 1. 安装Docker插件,利用Jenkins插件进行构建 2 ...
- centos6.5下,使用虚拟ftp用户
因为业务的问题,有位客户的账户总是出现各种问题,本人对于acl的使用又不是很会,所以和同事一起研究了一下这个虚拟ftp用户. Centos6.5 1 需求 为了保证系统的安全性,现对系统中vsftpd ...
- 支付宝支付接口-app支付沙箱环境
说明 开发阶段 需要验证自己的app支付加签是否正确,这里使用沙箱环境来进行模拟,使用ali的沙箱测试app和沙箱钱包app进行校验 准备阶段 1.进入沙箱页面 2.找到app支付文档 https:/ ...
- Spring Boot 自定义注解,AOP 切面统一打印出入参请求日志
其实,小哈在之前就出过一篇关于如何使用 AOP 切面统一打印请求日志的文章,那为什么还要再出一篇呢?没东西写了? 哈哈,当然不是!原因是当时的实现方案还是存在缺陷的,原因如下: 不够灵活,由于是以所有 ...
- scip 练习2.20
(define (same-parity x . z) (define (q? y) (= (remainder y ) )) (define (o? y) (= (remainder y ) )) ...
- H5新增的postMessage跨域解决方案Demo
Demo背景:html中使用iframe嵌入了跨域的vue项目,在html中将参数传入到跨越的vue项目中. 向跨越的子窗口中发送数据 function sendMessage(data) { // ...
- Linux下可以运行bat文件么?
bat是批处理文件,在windows和linux上都可以使用. 不过在linux的命令行中不可以直接敲"xxx.bat",系统会去找所有的命令. 想要调用bat文件,需要写绝对路径 ...
- 通过队列实现进程间的通信(使用阻塞方式调用func函数)
#_author:来童星#date:2019/12/17#通过队列实现进程间的通信from multiprocessing import Poolimport osimport timedef fun ...
- linux中hadoop组件启动日志存放问题
如果是nohup xxxx &的话会在当前目录下生成一个nohup文件存放当前出现所有的日志,&表示后台启动: 当然你也可以将日志存放在>/dev/null表示当前日志存放的位置 ...
- 良田高拍仪集成vue项目
一.硬件及开发包说明: 产品型号为良田高拍仪S1800A3,集成b/s系统,适用现代浏览器,图片使用BASE64数据.开发包的bin文件下的video.flt文件需要和高拍仪型号的硬件id对应,这个可 ...