[LeetCode 题解]:Palindrome Number
前言
【LeetCode 题解】系列传送门: http://www.cnblogs.com/double-win/category/573499.html
1.题目描述
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.
2. 题意
判断一个整数是否是回文数。要求使用常量空间。
提示:
(1)负数是否是回文数?
(2)注意常量空间复杂度。将数字转换成字符串是不可行的。
3. 思路
如果将整个数字转换再判断是否是回文数,那么就可能出现反转之后的数字越界。
延续这个思路,能否通过某些技巧来避免越界呢?先看下面的例子:
(1) 1234321 / 12344321
将数字分成等长的两部分: 1234 和 4321。那么可以看出 4321 反转之后的数字为1234. 两者相等,所以1234321为回文数。
(2) 12345321
将数字分成等长的两部分: 1234 和 5321。那么可以看出 5321 反转之后的数字为1235.
由于1234!=1235,所以12345321不是回文数。
从上面两个例子可以看出。在处理一个数字是否是回文数的过程中,没有必要将整个数字反转。而只需要判断数字的前后等长的两部分时候相等即可。
那么如何在常量空间复杂度内,将数字分成前后两部分呢?
记 需要判断的数字为x,其前一部分为firstpart=x,后一部分为secondpart=0.
采取依次取firstpart的末位,将其添加到secondpart的尾部的方式,直到firstpart<=secondpart.
firstpart | secondpart |
1234321 | 0 |
123432 | 1 |
12343 | 12 |
1234 | 123 |
123 | 1234 |
当firstpart<secondpart时,只需反过来将secondpart最后一位转移到firstpart末位即可。
tmp=1234%10=4;
firstpart=firstpart*10+tmp=123*10+4=1234。
此时secondpart也为1234.
因此1234321为回文数。
4: 解法
class Solution {
public:
bool isPalindrome(int x) {
int first=x,second=0,tmp;
if(x==0) return true; //zero
if(x<0|| x%10==0) return false; //negative number or the number is dividable by 10 while(first>second){ // reverse the number
tmp=first%10;
second= second*10+tmp;
first/=10;
}
if(first==second) return true;
else{ // handle the number with odd digits
tmp = second%10;
first=first*10+tmp;
if(first==second) return true;
else return false;
}
return false;
}
};
[LeetCode 题解]:Palindrome Number的更多相关文章
- leetcode题解||Palindrome Number问题
problem: Determine whether an integer is a palindrome. Do this without extra space. click to show sp ...
- LeetCode题解——Palindrome Number
题目: 判断一个数字是不是回文数字,即最高位与最低位相同,次高位与次低位相同,... 解法: 求出数字的位数,然后依次求商和求余判断是否相等. 代码: class Solution { public: ...
- Leetcode 9. Palindrome Number(水)
9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...
- Leetcode练习题 Palindrome Number
9. Palindrome Number Question: Determine whether an integer is a palindrome. An integer is a palindr ...
- 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 (回文数字)
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.(不要使用额外的空间) Some hints: Co ...
- [LeetCode][Python]Palindrome Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/palindr ...
- LeetCode——9. Palindrome Number
一.题目链接:https://leetcode.com/problems/palindrome-number/ 二.题目大意: 给定一个整数,判断它是否为一个回文数.(例如-12,它就不是一个回文数: ...
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
随机推荐
- Java对称与非对称加密解密,AES与RSA
加密技术可以分为对称与非对称两种. 对称加密,解密,即加密与解密用的是同一把秘钥,常用的对称加密技术有DES,AES等 而非对称技术,加密与解密用的是不同的秘钥,常用的非对称加密技术有RSA等 为什么 ...
- logging的使用
[logging的使用] import logging # 创建一个logger logger = logging.getLogger('mylogger') logger.setLevel(logg ...
- avg(xxxxxx)什么时候能独自出现?
avg(xxxxxx)是作为求一组数据的平均数,需要有这组数据的总数和个数,所以通常配合着group by来使用, 比如: SELECT ID, AVG(GRADE) AS 平均数 FROM TEST ...
- 【校招面试 之 C/C++】第29题 C/C++ 关键字extern
1.extern "C" extern "C"的主要作用就是为了能够正确实现C++代码调用其他C语言代码.加上extern "C"后,会指示 ...
- day10:vcp考试
Q181. An administrator is deploying ESXi 6.x hosts using Auto Deploy and wants the image profile to ...
- adf 笔记
1>jsf在bean中如何获取url参数,注意bean的范围,如果存在分页,范围不能设置为request,否则第二次加载的时候参数会为空. 最小设置为view,在当前页面中一直有效. 方法一:F ...
- [转]Win7下PEiD已停止工作
转载自:http://www.programlife.net/peid-stop-working-under-win7.html PEID是一个很不错的工具,可查壳,查壳PE信息,借助插件还可以做一些 ...
- 无法启动MYSQL服务”1067 进程意外终止”解决的方法——汇总及终极方法
自己一開始依照百度经验里的方法--<MySQL下载安装.配置与使用(win7x64)>去安装和配置,可是到后面步骤总是出现1067代号的错误. 慢慢折腾去解决. 这里汇总各种导致mysql ...
- Laravel 5.5 Api
Laravel api token验证使用方法 从 Laravel 5.2 开始, Laravel 的将路由的配置进行了分拆, 在 routes 目录下有 web.php 和 api.php 两个路由 ...
- OSGi 系列(三)之 bundle 详解
OSGi 系列(三)之 bundle 详解 1. 什么是 bundle bundle 是以 jar 包形式存在的一个模块化物理单元,里面包含了代码,资源文件和元数据(metadata),并且 jar ...