Leetcode: Palindrome Numbers
Determine whether an integer is a palindrome. Do this without extra space.
尝试用两头分别比较的方法,结果发现无法解决1000021这种问题
public class Solution {
public boolean isPalindrome(int x) {
if(x<0) return false;
int lastbit=x%10;
int firstbit;
int num=x;
int bits=0;
while(num/10!=0){
num=num/10;
bits++;
}
firstbit=num;
if(firstbit!=lastbit) return false;
else{
x=x-firstbit*((int)(Math.pow(10,bits)));
x=x/10;
if(x==0) return true;
else return isPalindrome(x);
}
}
}
查看了论坛的解答,看到一个很好的solution, 它怎么想到13行的 div/=100的,给跪了,这样刚好解决了中间有0的问题,比如1021会被判定为false, 而121会被判定为true。 解答详见:http://leetcode.com/2012/01/palindrome-number.html
public class Solution {
public boolean isPalindrome(int x) {
if (x < 0) return false;
int div = 1;
while (x / div >= 10) {
div *= 10;
}
while (x != 0) {
int l = x / div;
int r = x % 10;
if (l != r) return false;
x = (x % div) / 10;
div /= 100;
}
return true;
}
}
Leetcode: Palindrome Numbers的更多相关文章
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- POJ2402/UVA 12050 Palindrome Numbers 数学思维
A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example,the ...
- LeetCode: Palindrome Partition
LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- Palindrome Numbers(LA2889)第n个回文数是?
J - Palindrome Numbers Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu ...
- Uva - 12050 Palindrome Numbers【数论】
题目链接:uva 12050 - Palindrome Numbers 题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然 ...
- LeetCode: Palindrome 回文相关题目
LeetCode: Palindrome 回文相关题目汇总 LeetCode: Palindrome Partitioning 解题报告 LeetCode: Palindrome Partitioni ...
- E - Palindrome Numbers
题目链接:https://vjudge.net/contest/237394#problem/E A palindrome is a word, number, or phrase that read ...
- UVa 12050 - Palindrome Numbers (回文数)
A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example, th ...
随机推荐
- mysql查询昨天本周上周上月
昨天 $yestoday = date("Y-m-d 00:00:00",strtotime('-1day'));$today = date("Y-m-d 00:00:0 ...
- C#Form窗体通过代码改变尺寸
通过Size属性不能得到正确的窗体尺寸, 怎么办? 还需要设置 MaximumSize 属性和你的 size属性尺寸一样. this.FormBorderStyle = FormBorderStyle ...
- mysql binlog恢复
MySQL Binary Log也就是常说的bin-log, ,是mysql执行改动产生的二进制日志文件,其主要作用有两个: * 数据回复 * 主从数据库.用于slave端执行增删改,保持与maste ...
- php--linux环境下的主从复制
1.编辑数据库配置文件my.cnf,一般在/etc/目录下. #vi /etc/my.cnf 在[mysqld]的下面加入下面代码:[第一步查看本文件夹中代码是否已经存在,存在不需要进行添加] 只是修 ...
- php-- Linux图形界面与字符界面切换
转自:http://blog.163.com/wang_ly2442/blog/static/9494340720128355054551/ 1. 启动时进入字符界面,后来想切换到图形界面:使用sta ...
- 导出证书Cer文件为Pem格式的步骤
(1)先导出Push Services的证书,比如我们命名为“magic_cert.p12”,注意导出时会让你输入密码. (2)再导出Push Services证书的密钥(Private Key),比 ...
- [LeetCode]题解(python):052-N-Queens II
题目来源 https://leetcode.com/problems/n-queens-ii/ Follow up for N-Queens problem. Now, instead outputt ...
- sqlserver 视图能否有变量
不能,sqlserver 视图一般不能有变量,也不能带存储过程
- SQLSERER给表加自增列
alter table 表名 add 列名 int IDENTITY(1,1) NOT NULL
- ibatis传入数组或List
小结一下ibatis框架下,传入参数为数组类型或者是List类型的sql写法.标签里面都不需要表名 1.传入字符串数组,不需要标明parameterClasss,数组和List类型对象一样都可以用&l ...