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 ...
随机推荐
- Memcached原理深度分析详解
Memcached是 danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,用于在动态系统中减少数据库负载,提升性能.关于这个东 西,相信很多人都用过,本文意在通 ...
- UCenter 通信失败 和 无法同步登陆的调试方法
1. 看请求 2./uc_server/control/admin/app.php echo "\$url = $url <br />\n \$status = $status& ...
- ubuntu timezone
change timezone sudo dpkg-reconfigure tzdata synchronize time sudo ntpdate time.windows.com
- 特征向量-Eigenvalues_and_eigenvectors#Graphs
https://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors#Graphs A {\displaystyle A} ...
- install zabbix-agent on CENTOS
in ubuntu--https://www.digitalocean.com/community/tutorials/how-to-install-zabbix-on-ubuntu-configur ...
- aspx页面Page_Load和aspx页面上控件Page_Load事件执行顺序
今天公司的同事问了我一个问题,就是页面的Load方法和控件上的Load方法执行顺序的问题,看完了这个图片的递归调用之后大家就笑了,吼吼.
- 在express项目中有效组织和使用mongoose
平凡之路 1.创建express项目 express mongooseExpress 2.最简express var express = require("express"); v ...
- java对象中继承和变量初始化顺序浅析
先上例子代码 public class F { int age = 5; public F() { print(); } public void print() { System.out.printl ...
- 【C++】利用指针实现通过函数改变多个参数的值
写惯了python,对于C++的语法越来越生疏,不同于python中函数可以return多个变量,C++的函数要想返回多个参数可以利用指针实现. 因为在函数内部的变量都是局部变量,所以当参数传入函数中 ...
- Introducing shard translator
Introducing shard translator by Krutika Dhananjay on December 23, 2015 GlusterFS-3.7.0 saw the relea ...