Determine whether an integer is a palindrome. Do this without extra space.

判断一个整数是不是回文整数(例12321)。不能使用多余的空间。

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.

一些提示:

复数不是回文;如果你希望将整数转为字符串,注意不要使用多余的空间;

如果你希望反转整数,请注意“反转”可能导致溢出。你如何解决这个问题?

思路1:

可将此数按位从低到高依次取出,再从高到低反向排列,得到的新数字和原数字比较,一致则是回文数,否则不是。

至于提示中提到反转整数可能会出现int越界的问题,在此题中不会出现。因为如果一个int是回文整数,那么它和自身的反转相等,因此反转后不会越界。如果一个int不是回文数,反转后出现越界的情况,也不会影响结果的判定。

 class Solution {
public:
bool isPalindrome(int x) {
int res = ;
int tmp_x = x; if (x<)
return false; while (tmp_x != ) {
res = res * + tmp_x % ;
tmp_x = tmp_x / ;
} if (res == x) {
return true;
} else {
return false;
} }
};

思路2:

可以将整数中,需要进行比较的首尾数字,不断取出并比较。

 class Solution {
public:
bool isPalindrome(int x) {
if (x < )
return false; int sig = ; while (x / sig >= ) {
sig *= ;
} while (x!=) {
if (x / sig == x % ) {
x = x % sig / ;
sig /= ;
} else {
return false;
}
} return true;
}
};

上面代码在函数中对x进行了修改,为了避免x被修改:

 class Solution {
public:
bool isPalindrome(int x) {
if (x < )
return false; int high = , low = ; while (x / high >= ) {
high *= ;
} while (high > low) {
if (x / high % == x / low % ) {
high /= ;
low *= ;
} else {
return false;
}
} return true;
}
};

附录:

算法中“不使用多余空间”的含义

【Leetcode】【Easy】Palindrome Number的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List

    9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...

  5. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  6. C# 写 LeetCode easy #9 Palindrome Number

    9.Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it ...

  7. leetcode:Reverse Integer 及Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...

  8. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  9. 【LeetCode每天一题】Palindrome Number( 回文数字)

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

  10. 【leetcode刷题笔记】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

随机推荐

  1. LeetCode934.shortest bridge【dfs+bfs】

    一.题面 在给定的二维二进制数组 A 中,存在两座岛.(岛是由四面相连的 1 形成的一个最大组.) 现在,我们可以将 0 变为 1,以使两座岛连接起来,变成一座岛. 返回必须翻转的 0 的最小数目.( ...

  2. P4174 [NOI2006]最大获利

    传送门 把用户群和中转站都看成点 用户群权值为正,中转站权值为负 为了获得用户群的权值,我们不得不一起获得中转站负的权值 发现就是裸的最大权闭合子图 那么从用户群连边向中转站,边值INF 从 S 连向 ...

  3. Linux(1)-CentOS7下解决ifconfig command not found

    第一步: 输入ip addr 确认IP地址是否设置正常,设置好如下所示,如果没有获取到IP地址则设置一个即可. 第二步 确认sbin目录是否存在. cd /sbin 第三步 确认ifconfig命令是 ...

  4. [转] Node.js的线程和进程

    [From] http://www.admin10000.com/document/4196.html 前言 很多Node.js初学者都会有这样的疑惑,Node.js到底是单线程的还是多线程的?通过本 ...

  5. java.lang.ref.Reference<T>

    //看之前先要知道java里面的四种引用.package com.zby.ref; import sun.misc.Cleaner; /** * 引用对象的抽象基础类.这个类定义了所有引用对象的公共操 ...

  6. Java 写入pdf文件

    import java.awt.Color; import java.io.File; import java.io.FileOutputStream; import com.lowagie.text ...

  7. 转 AIX7.2+11.2.0.4RAC实施

    参考 https://blog.csdn.net/alangmei/article/details/18310381 https://blog.csdn.net/smasegain/article/d ...

  8. oracle 基础知识(一)

    Oracle 用户.权限.角色管理 01.概念 用户:对数据库的访问需要以适当的身份通过验证,这就是用户的作用:每个Oracle用户都有自己的用户名和密码,并且拥有他们所创建的任意表.视图和其他资源, ...

  9. python list常见用法

    来至builtins.py: def extend(self, iterable): # real signature unknown; restored from __doc__ "&qu ...

  10. Python 中的反射和自省

    本文主要介绍Python中的反射和自省,以及该机制的简单应用 熟悉Java的程序员,一定经常和Class.forName打交道.即使不是经常亲自调用这个方法,但是在很多框架中(spring,eclip ...