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 ...
随机推荐
- 文件大小的友好输出及其 Python 实现
在数据库中存储时,使用 Bytes 更精确,可扩展性和灵活性都很高. 输出时,需要做一些适配. 1. 注意事项与测试代码 需要考虑 sizeInBytes 为 None 的场景. 除以 1024.0 ...
- Pox组件
最近在学习Pox,为了加深印象,对Pox wiki中的Pox组件写了些笔记. 按照组件的功能进行分类: L2层地址学习.洪泛 forwarding.hub forwarding.l2_lear ...
- python3 pyqt5 和eric5配置教程
一.大纲内容: 1.预备PC环境: 2.预备安装程序: 2.1.下载Python3.2 2.2.下载PyQt4 2.3.下载Eric5 3.安装配置步骤: 3.1.安装Pyhon3.2 3.2.安装P ...
- onclick控制元素显示与隐藏时,点击第一次无反应的原因
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- 安装Nuget上常用的包的命令
起因: Nuget图形化操作界面各种卡顿,或者有时干脆就连不上了.所以用命令还是很必须的. 常用命令: 安装 Entity Framework : PM> Install-Package Ent ...
- Sambar,实现Linux和Windows共享
我下载的是tar的jar包,不是rpm,rpm就不多说了.目的是让Windows能够共享Linux系统的文件夹 1.进入到source文件夹: 2../configure->make->m ...
- Lua基础之table详解
概要:1.table特性:2.table的构造:3.table常用函数:4.table遍历:5.table面向对象 原文地址:http://blog.csdn.net/dingkun520wy/art ...
- 基于Vuforia的Hololens图像识别
微软官方Hololens开发文档中有关于Vuforia的内容,https://developer.microsoft.com/en-us/windows/holographic/getting_sta ...
- 1042: [HAOI2008]硬币购物 - BZOJ
Description 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚ci硬币,买si的价值的东西.请问每次有多少种付款方法.Input 第一行 ...
- scrum敏捷开发
团队PM:袁佩佩 scrum敏捷开发计划制定: 确定项目实施具体阶段目标 确定项目相关任务分解 确定每日站立会议进行计划 确定项目计划总结日程 确定风险解决方案