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. UVALive - 3942 左儿子trie DP

    题意:白书P209 本题用普通字典树会更快,为了练习还是尝试再敲一遍左儿子-右兄弟字典树(其实就是字典树上开前向星) dp[i]为满足[i...len)的分配方案数,转移方程为dp[i]=sum{dp ...

  2. php 的基本语法

    八种数据类型: 4种标量类型:boolean.integer.float.string 2种复合类型:array.object 2种特殊类型:resource.NULL 如果想看某个表达式的值和类型用 ...

  3. C++ GUI Qt4编程(09)-3.3spreadsheet-toolbar

    1. C++ GUI Qt4编程第三章,增加工具栏.状态栏和快捷键. 2. mainwindow.h /**/ #ifndef MAINWINDOW_H #define MAINWINDOW_H #i ...

  4. git push的一些坑

    在安装git的时候我们一般会自己设置一个用户名和邮箱,这个一般设置为全局的用户名,如下所示 git config --global user.name "xxx" git conf ...

  5. element-ui Form表单验证

    element-ui Form表单验证规则全解 element的form表单非常好用,自带了验证规则,用起来很方便,官网给的案例对于一些普通场景完全没问题,不过一些复杂场景的验证还得自己多看文档摸索, ...

  6. java移位操作注意事项

    来个浅显的 在java中,移位分为有符号移位和无符号移位,无符号移位的话不论左移还是右移,自动在低位或者高位补0 如果是有符号数进行移位的话,必须要先将

  7. Kafka使用多个分区时 consumer的Assign配置

    天天在给自己挖坑排坑... 因为要开多线程消费,所以分区加到了10,两个broker. Producer没有做特殊处理,所以是随机发到Partitions. 但是Consumer只做Subscribe ...

  8. Map接口常用实现类学习

    HashMap 1.6的HashMap:数组加单向链表结构 最重要的内部类Entry,全类名是java.util.HashMap.Entry,是个静态类,实现了Map.Entry接口.HashMap. ...

  9. CSAPP阅读笔记-gcc常用参数初探-来自第三章3.2的笔记-P113

    gcc是一种C编译器,这次我们根据书上的代码尝试着使用它. 使用之前,先补充前置知识.编译器将源代码转换为可执行代码的流程:首先,预处理器对源代码进行处理,将#define指定的宏进行替换,将#inc ...

  10. vue生命周期及使用 && 单文件组件下的生命周期

    生命周期钩子 这篇文章主要记录与生命周期相关的问题. 之前,我们讲到过生命周期,如下所示: 根据图示我们很容易理解vue的生命周期: js执行到new Vue() 后,即进入vue的beforeCre ...