本来推断回文串是一件非常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(不使用额外空间)的更多相关文章

  1. [LeetCode] Palindrome Number 验证回文数字

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  2. leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  3. LeetCode——Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...

  4. [Leetcode]Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. 这题貌似解法挺多,直接用简单的把数倒置,没有考虑数 ...

  5. leetcode:Palindrome Number【Python版】

    一次AC 题目要求中有空间限制,因此没有采用字符串由量变向中间逐个对比的方法,而是采用计算翻转之后的数字与x是否相等的方法: class Solution: # @return a boolean d ...

  6. [Leetcode] Palindrome number 判断回文数

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  7. leetcode Palindrome Number python

    class Solution(object): def isPalindrome(self, x): """ :type x: int :rtype: bool &quo ...

  8. [LeetCode]Palindrome Number 推断二进制和十进制是否为回文

    class Solution { public: bool isPalindrome2(int x) {//二进制 int num=1,len=1,t=x>>1; while(t){ nu ...

  9. 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...

随机推荐

  1. Linux 常用命令:系统状态篇

    前言 Linux常用命令中,有些命令可以用于查看系统的状态,通过了解系统当前的状态,能够帮助我们更好地维护系统或定位问题.本文就简单介绍一下这些命令. 1. 查看系统运行时间--uptime 有时候我 ...

  2. Git 服务器更换了IP的解决方法

    1.找到项目根目录中的.git文件夹 2..git文件夹里有一个config文件 3.用记事本打开后,修改为服务器的新ip就行了.

  3. Unity调用Android的两种方式:其一、调用jar包

    unity在Android端开发的时候,免不了要调用Java:Unity可以通过两种方式来调用Android:一是调用jar.二是调用aar. 这篇文章主要讲解怎么从无到有的生成一个jar包,然后un ...

  4. Hadoop学习总结(1)——大数据以及Hadoop相关概念介绍

    一.大数据的基本概念 1.1.什么是大数据 大数据指的就是要处理的数据是TB级别以上的数据.大数据是以TB级别起步的.在计算机当中,存放到硬盘上面的文件都会占用一定的存储空间,例如: 文件占用的存储空 ...

  5. Spring 容器(一)

    Source null instanceof Object 语法是正确的 Source定义得到输入流的方法 Resource继承Source,定义判断流是否存在.是否打开等方法 BaseResourc ...

  6. [Poi] Setup PostCSS and Tailwind with Poi

    This lesson walks through setting up a Poi project using PostCSS and the popular Tailwind library fo ...

  7. 【Oracle错误集锦】:ORA-00119 &amp; ORA-00132

    有时候老天就是爱和你开玩笑,昨天好不easy配置好Oracle.可以用PL/SQL正常登录使用,今天突然就不行了.而且错误十分诡异,没有提示什么错误代码.输入usernamepassword,点击登录 ...

  8. Android 数据库框架总结,总有一个适合你!

    一:OrmLite 简述: 优点: 1.轻量级:2.使用简单,易上手:3.封装完善:4.文档全面.缺点:1.基于反射,效率较低(本人还没有觉得效率低):2.缺少中文翻译文档 jar包 地址:http: ...

  9. 分享一个vueui axios-mock-adapter 中的用法

    import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; import { LoginUsers, Users ...

  10. 今日SGU 5.19

    SGU 142 题意:给你一个长度为n的串(由a,b组成),让你找出一个串不是n的子串,长度最下 收获:思维题,思路在代码里 #include<bits/stdc++.h> #define ...