problem:

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

click to show spoilers.

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.

thinking:

(1)之前求过int型逆序反转。这里检验一个数是否为一个回文数,能够得到一点启示。

(2)逆序反转主要涉及溢出问题的处理,并且反转的空间复杂度能够忽略不计,满足题目要求。

code:

class Solution {
public:
bool isPalindrome(int x) {
int b=0;
int a=x;
bool flag=true;
if(a<0)
{
bool flag=false;
a=-x;
}
while(a)
{
b*=10;
b+=a%10;
a=a/10;
}
if(b>2147483647)
return false;
if(!flag)
b=-b;
return (b==x); }
};

leetcode题解||Palindrome Number问题的更多相关文章

  1. LeetCode题解——Palindrome Number

    题目: 判断一个数字是不是回文数字,即最高位与最低位相同,次高位与次低位相同,... 解法: 求出数字的位数,然后依次求商和求余判断是否相等. 代码: class Solution { public: ...

  2. Leetcode 9. Palindrome Number(水)

    9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...

  3. Leetcode练习题 Palindrome Number

    9. Palindrome Number Question: Determine whether an integer is a palindrome. An integer is a palindr ...

  4. leetcode 9 Palindrome Number 回文数

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

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

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

  6. Leetcode 9. Palindrome Number(判断回文数字)

    Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...

  7. [LeetCode][Python]Palindrome Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/palindr ...

  8. LeetCode——9. Palindrome Number

    一.题目链接:https://leetcode.com/problems/palindrome-number/ 二.题目大意: 给定一个整数,判断它是否为一个回文数.(例如-12,它就不是一个回文数: ...

  9. 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]

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

随机推荐

  1. mongodb Shell 启动

    开始运行mongodb 准备 上篇说过,通过brew安装的程序目录在 /usr/local/Cellar下面 下面,我们先看一下 mongodb的可执行程序命令 cd /usr/local/Cella ...

  2. Java学习1_一些基础1——16.5.4

    每个java程序中都必须有一个main方法,格式为: public class ClassName { public static void main(String[] args) { program ...

  3. 大数除法(除数在int范围内)

    #include<iostream> #include<cstdio> #include<cstring> #define N 1000 using namespa ...

  4. vi 命令学习(二)

    [选中文本(可视模式)] v 可视模式 从光标位置开始按正常模式选择文本 V 可视行模式 选中光标经过的完整行 ctrl + v 可视块模式 垂直方向选中文本 [ 撤销和恢复撤销] u undo 撤销 ...

  5. Java必知必会的20种常用类库和API

    转载:https://blog.csdn.net/u011001084/article/details/79216958 个人感觉工具类对日常开发是很重要的,所以推荐一下这篇文章,虽然有的类库过时了 ...

  6. java基础学习日志--异常案例

    package test7; public class InvalidScroreException extends Exception { public InvalidScroreException ...

  7. ORACLE 查看当前用户信息(用户,表视图,索引,表空间,同义词,存储过程,约束条件)

    1.用户 查看当前用户的缺省表空间 SQL>select username,default_tablespace from user_users; 查看当前用户的角色 SQL>select ...

  8. 【Codeforces 1037D】Valid BFS?

    [链接] 我是链接,点我呀:) [题意] 让你判断一个序列是否可能为一个bfs的序列 [题解] 先dfs出来每一层有多少个点,以及每个点是属于哪一层的. 每一层的bfs如果有先后顺序的话,下一层的节点 ...

  9. 清空所有Session

    //清空所有Session request.getSession().invalidate();

  10. HDU 5291 Candy Distribution

    Candy Distribution Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...