No.009 Palindrome Number
9. Palindrome Number
- Total Accepted: 136330
- Total Submissions: 418995
- Difficulty: Easy
Determine whether an integer is a palindrome. Do this without extra space.
Some hints:
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
思路:
本题解题很简单,首先判断负数和0,然后我们计算得到最大基数,如果x为一个两位数,则基数base=100,三位数则为1000等,然后我们每次分别取这个数的最高位和最低位进行比较,如果不相等,则直接返回false,相等则去掉最左边和最右边的数后进行下一轮比较。代码如下:
public boolean isPalindrome(int x) {
if(x < 0){
return false ;
}
if(x == 0 ){
return true ;
}
int base = 1 ;
while(x/base >= 10){
base *= 10 ;
}
while(x != 0){
int leftDigit = x/base ;
int rightDigit = x%10 ;
if(leftDigit != rightDigit){
return false ;
}
x = x%base/10 ;
base /= 100 ;
}
return true ;
}
No.009 Palindrome Number的更多相关文章
- LeetCode--No.009 Palindrome Number
9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...
- 【JAVA、C++】LeetCode 009 Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. 解题思路一: 双指针法,逐位判断 Java代码如下 ...
- 【LeetCode】009. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- [Leetcode]009.Palindrome Number
public class Solution { public boolean isPalindrome(int x) { if (x<0 || (x!=0 && x%10==0) ...
- 009 Palindrome Number 判断一个正整数是否是回文数
详见:https://leetcode.com/problems/palindrome-number/description/ 实现语言:Java 方法一: class Solution { publ ...
- 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...
- 65. Reverse Integer && Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, re ...
- 有趣的数-回文数(Palindrome number)
文章转自http://blog.163.com/hljmdjlln@126/blog/static/5473620620120412525181/ 做LC上的题"Palindrome num ...
- 9. Palindrome Number
/* Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers ...
随机推荐
- NOR Flash擦写和原理分析 (一)
1. NOR FLASH 的简单介绍 NOR FLASH 是很常见的一种存储芯片,数据掉电不会丢失.NOR FLASH支持Execute On Chip,即程序可以直接在FLASH片内执行(这意味着存 ...
- Servlet细节
Servlet细节 线程不安全的做法: * 不要在Servlet中创建成员!创建局部变量即可! * 可以创建无状态成员! * 可以创建有状态的成员,但状态必须为只读的!(不提供set方法) 1.Ser ...
- [JS]学习Javascript闭包(Closure)
转自:阮一峰 闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 下面就是我的学习笔记,对于Javascript初学者应该是很有用的. 一.变量的 ...
- ADF_ADF Faces系列3_ADF数据可视化组件简介之建立Master-Detail
2013-05-01 Created By BaoXinjian
- dede图片横向滚动
<div id=demo style="overflow:hidden; width:960px;" > <table border=0 align=" ...
- git的使用(3) 多分支情况下的pull
当你存在多个分支的时候,你需要pull下来分支上面的内容,你需要指定分支进行同步命令: git pull origin branch (branch 是你的分支的名字)
- 获取mysql数据表中的列名
select COLUMN_NAME from information_schema.columns where table_name='table_name' DESCRIBE table_name ...
- powerdesigner中将表的name在生成建表sql时生成注释
1.为powerdesigner的表设置注释方法: powerdesigner默认没有注释: 设置方法: 选择那个表 右键- >Properties- >Columns- >Cust ...
- ubuntu安装mysql5.7
sudo apt-get updatesudo apt-get upgradesudo apt-get install mysql-server mysql-client #自动安装会装上5.7的 ...
- Gerrit清单库配置(转载)
From:http://fatalove.iteye.com/blog/1340334 gerrit清单库是用来配合repo使用的.清单库中列出了gerrit服务器上的其他版本库. 客户端通过repo ...