Java [leetcode 9] Palindrome Number
问题描述:
Determine whether an integer is a palindrome. Do this without extra space.
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.
解题思路:
首先判断是否为负数,若是负数,直接返回false。否则继续下面判断:
从两头开始往中间读数,分别判断即可。
代码如下:
public class Solution {
public boolean isPalindrome(int x) {
int divisor = 1;
int left;
int right;
int temp = x;
if (x < 0)
return false;
while (temp / 10 > 0) {
divisor *= 10;
temp = temp / 10;
} while (x != 0) {
left = x / divisor;
right = x % 10;
if (left != right)
return false;
x = (x % divisor) / 10;
divisor = divisor / 100;
}
return true;
}
}
Java [leetcode 9] Palindrome Number的更多相关文章
- Leetcode练习题 Palindrome Number
9. Palindrome Number Question: Determine whether an integer is a palindrome. An integer is a palindr ...
- Leetcode 9. Palindrome Number(水)
9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...
- 【JAVA、C++】LeetCode 009 Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. 解题思路一: 双指针法,逐位判断 Java代码如下 ...
- LeetCode 9. Palindrome Number (回文数字)
Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- leetcode:Palindrome Number
Question: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Cou ...
- Leetcode 9. Palindrome Number(判断回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...
- [LeetCode][Python]Palindrome Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/palindr ...
随机推荐
- select&pselect/poll&ppoll/epoll
select/pselect, poll和epoll的区别 select,epoll,poll比较 select,poll,epoll进化 Handling of asynchronous event ...
- C语言字符串与字符数组
字符串儿与字符数组 字符数组的定义: Char buffer[]; 字符数组初始化: Char buffer1[]="hello world"; 利用scanf输入一个字符串儿 代 ...
- 【信息学奥赛一本通】第三部分_队列 ex2_3produce 产生数
给出一个整数n(n<=2000)(代码可适用n<=10^31)和k个变换规则(k<=15). 规则:1.1个数字可以变换成另1个数字: 2.规则中右边的数字不能为零. BFS #in ...
- ios App 加急审核
下面进入正题.提交完成后进入加急审核页面. 链接:https://developer.apple.com/appstore/contact/appreviewteam/index.html 在i wo ...
- UITextField监听文字输入事件
[textField addTarget:self action:@selector(textFieldDidChange:)forControlEvents:UIControlEventEditin ...
- linux内核分析之进程地址空间管理
1.struct task_struct 进程内核栈是操作系统为管理每一个进程而分配的一个4k或者8k内存大小的一片内存区域,里面存放了一个进程的所有信息,它能够完整的描述一个正在执行的程序:它打开的 ...
- (转)为什么adrl r2,mem_cfg_val这里不用ldr r2,=mem_cfg_val
网址:http://blog.csdn.net/glorin/article/details/6327083 memsetup:mov r1, #MEM_CTL_BASEadrl r2,mem_cfg ...
- PAT-乙级-1017. A除以B (20)
1017. A除以B (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求计算A/B,其中A是不超过 ...
- Titan DB的一些问题
使用熟悉一点的系统来测试TitanDB,HBASE+ES,记录下来一些小tips. 1.首先TitanDB支持的Hadoop只有1.2.1,所以Hbase自然也只能取到0.98,虽然官网上提供了tit ...
- struts2 标签的使用之一 s:if
struts2 的web 项目中为了方便的编写jsp,标签是最好的选择 1:struts2 标签库的定义在**-core-版本号.jar META-INF 路径下找到struts-tags.tld文件 ...