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.

分析:

回文数,并且题目要求不能使用额外的空间。

即,不能使用回文串的方法。

本题很简单,算出x的倒置数a,比较a是否和x相等就行了。

  1. class Solution {
  2. public:
  3. bool isPalindrome(int x) {
  4. //算出x的倒置数a,比较a是否和x相等就行了
  5. int a = , b = x;
  6. while(b > ){
  7. a = a * + b % ;
  8. b /= ;
  9. }
  10. if(a == x)
  11. return true;
  12. else
  13. return false;
  14.  
  15. }
  16. };

网上其他的代码,其思路:每次提取头尾两个数,判断它们是否相等,判断后去掉头尾两个数

  1. class Solution {
  2. public:
  3. bool isPalindrome(int x) {
  4.  
  5. //negative number
  6. if(x < )
  7. return false;
  8.  
  9. int len = ;
  10. while(x / len >= )
  11. len *= ;
  12.  
  13. while(x > ) {
  14.  
  15. //get the head and tail number
  16. int left = x / len;
  17. int right = x % ;
  18.  
  19. if(left != right)
  20. return false;
  21. else {
  22. //remove the head and tail number
  23. x = (x % len) / ;
  24. len /= ;
  25. }
  26. }
  27.  
  28. return true;
  29. }
  30. };

Leetcode 9. Palindrome Number(判断回文数字)的更多相关文章

  1. [LeetCode]9. Palindrome Number判断回文数字

    /* 查看网上的思路有两种: 1.每次取两边的数,然后进行比较 2.取数的倒置数,进行比较 */ public boolean isPalindrome1(int x) { if (x<0) r ...

  2. LeetCode 9 Palindrome Number(回文数字判断)

    Long Time No See !   题目链接https://leetcode.com/problems/palindrome-number/?tab=Description   首先确定该数字的 ...

  3. LeetCode 9. Palindrome Number (回文数字)

    Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...

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

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

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

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

  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 判断回文数

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

  8. LeetCode OJ Palindrome Number(回文数)

    class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ...

  9. LeetCode 9. Palindrome Number(回文数)

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

随机推荐

  1. Spring入门(10)-Spring JDBC

    Spring入门(10)-Spring JDBC 0. 目录 JdbcTemplate介绍 JdbcTemplate常见方法 代码示例 参考资料 1. JdbcTemplate介绍 JdbcTempl ...

  2. get和eq的区别

    <p style="color:yellow">绯雨</p> //使用eq来获得第一个p标签的color值: console.log().css(" ...

  3. UI进阶 SQLite错误码

    #define SQLITE_OK 0 /* 成功 | Successful result */ /* 错误码开始 */ #define SQLITE_ERROR 1 /* SQL错误 或 丢失数据库 ...

  4. C++中void型指针

    问题由来: PX_FORCE_INLINE void* operator new(size_t size, const char* handle, const char * filename, int ...

  5. OS X Git连接github

    1. 运行到.local 2. cd ~/.ssh查看文件是否存在 3. ssh-keygen(创建public & private key) 4. 或者运行如下命令:cd ~/.ssh &a ...

  6. LinkedList类

    LinkedList类 LinkedList类和ArrayList,Vector基本相同,都有增.删.改.查等方法.LinkedList是继承List接口. import java.util.*; p ...

  7. Codeforces Beta Round #85 (Div. 1 Only) A. Petya and Inequiations 贪心

    A. Petya and Inequiations Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  8. Android4.4 蓝牙源码部分分析

    最近GOOGLE发布了Android4.4,看了一下源码:4.4的蓝牙打开流程这一部分还是有些变化的,从界面上看蓝牙开关就是设置settings里那个switch开关,widget开关当然也可以,起点 ...

  9. Asp.net 使用正则和网络编程抓取网页数据(有用)

    Asp.net 使用正则和网络编程抓取网页数据(有用) Asp.net 使用正则和网络编程抓取网页数据(有用) /// <summary> /// 抓取网页对应内容 /// </su ...

  10. 关于dispatchTouchEvent, onInterceptTouchEvent, onTouchEvent的分发机制浅析

    虽说这个问题不是很难...动动手就能看出答案...但是似乎不太容易理解...几次尝试把这个问题说明白....但是好像感觉说不明白....(顿时想起了那句话----说不明白就是自己还不明白! 我怎么可能 ...