[leetcode] Palindrome Number(不使用额外空间)
本来推断回文串是一件非常easy的事情,仅仅须要反转字符串后在与原字符串相比較就可以。这道题目明白说明不能使用额外的空间。那么使用将其分解连接成字符串的方法便不是可行的。仅仅好採用数学的方式: 每次取最高位和最低位相比較,总的位数能够用一个while先处理出来,循环直至取余和除数相等。
详细见代码:
class Solution {
public:
bool isPalindrome(int x) {
if(x<0) //special due
return false;
if(x<10)
return true;
int curMod=0;
int test=x;
while(test)
{
curMod++;
test/=10;
}
curMod--;// bit num
int left=pow(10,curMod*1.0),right=10;
while(right<=left)
{
if(x%right!=x/left)
return false;
x=x%left,x/=10;
left/=100;
}
return true;
}
};
[leetcode] Palindrome Number(不使用额外空间)的更多相关文章
- [LeetCode] Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- LeetCode——Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- [Leetcode]Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. 这题貌似解法挺多,直接用简单的把数倒置,没有考虑数 ...
- leetcode:Palindrome Number【Python版】
一次AC 题目要求中有空间限制,因此没有采用字符串由量变向中间逐个对比的方法,而是采用计算翻转之后的数字与x是否相等的方法: class Solution: # @return a boolean d ...
- [Leetcode] Palindrome number 判断回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- leetcode Palindrome Number python
class Solution(object): def isPalindrome(self, x): """ :type x: int :rtype: bool &quo ...
- [LeetCode]Palindrome Number 推断二进制和十进制是否为回文
class Solution { public: bool isPalindrome2(int x) {//二进制 int num=1,len=1,t=x>>1; while(t){ nu ...
- 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...
随机推荐
- 深入理解void以及void指针的含义
void的含义 void即“无类型”,void *则为“无类型指针”,可以指向任何数据类型. void指针使用规范 ①void指针可以指向任意类型的数据,亦即可用任意数据类型的指针对void指针赋值. ...
- 八 rowkey设计 几种方法
简单来讲,rowkey就是 KeyValue 中的key rowkey设计之 尽量散列设计 RowKey 如第三部分第六中讲到,如果数据都是有序的存储到一个特定的范围内,将会存 ...
- Qt编译OpenGL程序遇到的问题
软件版本号: Qt 4.8.5 依照网上的例程(http://www.qiliang.net/old/nehe_qt/lesson01.html),跑了一下基于Qt Creator的OpenGL.因为 ...
- [Recompose] Create Stream Behaviors to Push Props in React Components with mapPropsStream
Rather than using Components to push streams into other Components, mapPropsStream allows you to cre ...
- Thinking in UML 学习笔记(三)——UML核心视图之类图
类图的作用:用于展示系统中的类及其相互之间的关系. UML在解决面向对象的方法中对类理解为三个层次,各自是:概念层.说明层.实现层.在UML中,从開始的需求到终于设计类,类图也是环绕这三个层次的观点进 ...
- python list的+,+=,append,extend
面试题之中的一个. def func1(p): p = p + [1] def func2(p): p += [1] p1 = [1,2,3] p2 = [1,2,3] func1(p1) func2 ...
- 传说用户发来的请求是在JIoEndpoint的accept函数中接收的,是tomact与外界交互的分界点
传说用户发来的请求是在JIoEndpoint的accept函数中接收的, 这是tomact与外界交互的分界点,所以来研究一下, >>>>>>>>> ...
- 织梦DedeCMS判断简略标题为空时则显示完整标题
使用织梦DedeCMS系统程序开发网站中,我们会遇到很多因网页版面设计限定的宽度,使文章标题需要进行字数限制,通常做法是在a标签中加入一个title属性,让鼠标放上去的时候显示完整标题.但是标题被剪裁 ...
- centos yum 安装php7.2
yum -y remove php* rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm r ...
- R与并行计算
本文在Creative Commons许可证下发布 什么是并行计算? 并行计算,准确地说应该包括高性能计算机和并行软件两个方面.不过,近年来随着个人PC机,廉价机群,以及各种加速卡(NVIDIA GP ...