[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 ...
随机推荐
- 使用ecstore-sdk开发包制作易开店和启明星模板
前言: 尽管商派官网有模板开发教程,但是诸多方面太过笼统.我等平庸之辈,纵使细心研读,潜心修炼,亦未能品味练功境界,领悟其中真谛. 商派有云,此九阳真经不用您挥刀****本人却感觉此教程令人抓狂,无人 ...
- python之递归锁【Rlock】
# 递归锁:就是一把锁中还有一把小锁,比如学校的大门口有一个大锁,学校里的 #每个教室也有一把小锁,以后所有的锁都用rlock就可以了,不要用lock,尤其是多层锁的时候,必须要用递归锁 import ...
- LSTM Accuracy
Training iter #1: Batch Loss = 1.234543, Accuracy = 0.29866665601730347PERFORMANCE ON TEST SET: Batc ...
- 源码安装php时出现configure: error: xml2-config not found. Please check your libxml2 installation
1.检查是否安装了libxml 包 > rpm -qa|grep libxml2 2.如果没有则安装 > yum install libxml2 > yum install libx ...
- linux 动态库 静态库 函数覆盖
本文讨论了linux动态库 静态库中函数的覆盖问题. 测试目的: 同名函数,分别打成动态库libdync_lib.so与静态库libstatic_lib.a,并把libstatic_lib.a打到另 ...
- 用AI制作炫酷效果
PART1:制作第一个效果 步骤一:新建一个800*600的画布. 骤二:从工具栏选“矩形工具”,创建一个800*600的矩形.白色的是画布,浅红色(我的AI之前保留的填充颜色,每个人都不一样)的是你 ...
- Ui设计流行趋势,对颜色的探讨
设计风向转换的趋势越来越短,在设计圈中,流行设计的跟新换代更是快.在设计时间越来越短的今天,在经理领导不断催促的时下,如何准确的把握当下的流行趋势,如何在设计之初就能定好设计的基调.这对于还是刚入设计 ...
- cnn 反向bp这个地方怎么推导??
- Monte carlo
转载 http://blog.sciencenet.cn/blog-324394-292355.html 蒙特卡罗(Monte Carlo)方法,也称为计算机随机模拟方法,是一种基于"随机数 ...
- 二进制搭建kubernetes多master集群【一、使用TLS证书搭建etcd集群】
上一篇我们介绍了kubernetes集群架构以及系统参数配置,参考:二进制搭建kubernetes多master集群[开篇.集群环境和功能介绍] 下面本文etcd集群才用三台centos7.5搭建完成 ...