Palindrome Number

Total Accepted: 19369 Total
Submissions: 66673My Submissions

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

推断一个数整数是不是回文?比如121,1221就是回文,好吧,直接利用前面写过的【Leet
Code】Reverse Integer——“%”你真的懂吗?

只是这里要考虑翻转后,数值溢出的问题,代码例如以下:

/*
//first method
class Solution {
public:
bool isPalindrome(int x)
{
long long temp = x;
long long ret = 0;
bool isNegative = false;
if (temp < 0)
{
return false;
}
while (temp)
{
ret = ret * 10 + temp % 10;
temp /= 10;
}
if(x == ret)
{
return true;
}
else
{
return false;
} }
};
*/

当然,我们还有更好的方法,事实上,初见这个题目,第一个想法是取头取尾进行比較,然后把头尾去掉,再循环,直到数值为个位数为止:

class Solution {
public:
bool isPalindrome(int x)
{
if (x < 0)
{
return false;
}
int divisor = 1;
while (x / divisor >= 10)
{
divisor *= 10;
}
while (x)
{
if (x / divisor != x % 10)
{
return false;
}
x = (x % divisor) / 10;
divisor /= 100;
}
return true;
}
};

【Leet Code】Palindrome Number的更多相关文章

  1. 【Leet Code】String to Integer (atoi) ——常考类型题

    String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions Implement ato ...

  2. 【POJ 1159】Palindrome

    [POJ 1159]Palindrome 近期各种题各种奇葩思路已经司空见惯了...又新出个滚动数组= = 该题另一点须要知道 最少须要补充的字母数 = 原序列S的长度 - S和S'的最长公共子串长度 ...

  3. 【LeetCode算法-9】Palindrome Number

    LeetCode第9题 Determine whether an integer is a palindrome. An integer is a palindrome when it reads t ...

  4. 【codeforces 805D】Minimum number of steps

    [题目链接]:http://codeforces.com/contest/805/problem/D [题意] 给你一个字符串; 里面只包括a和b; 让你把里面的"ab"子串全都去 ...

  5. 【转】【VS Code】配置文件Launch及快捷键

     Ctrl+shift+p,然后输入launch,点击第一个选项即可配置. 之后选择More即可 具体配置可修改为: { "version": "0.2.0", ...

  6. 【CF#303D】Rotatable Number

    [题目描述] Bike是一位机智的少年,非常喜欢数学.他受到142857的启发,发明了一种叫做“循环数”的数. 如你所见,142857是一个神奇的数字,因为它的所有循环排列能由它乘以1,2,...,6 ...

  7. 【POJ 3974】 Palindrome

    [题目链接] http://poj.org/problem?id=3974 [算法] 解法1 : 字符串哈希 我们可以分别考虑奇回文子串和偶回文子串,从前往后扫描字符串,然后二分答案,检验可以用哈希 ...

  8. 【leetcode】Palindrome Number

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

  9. 【leetcode】Palindrome Number (easy)

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

随机推荐

  1. python开发_tempfile

    python中的tempfile模块,是为创建临时文件(夹)所提供的 如果你的应用程序需要一个临时文件来存储数据,但不需要同其他程序共享,那么tempfile模块来创建临时文件(夹)是个不错的选择 其 ...

  2. Codeforces Round #300 B. Quasi Binary 水题

    B. Quasi Binary Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/538/probl ...

  3. mysql max_allowed_packet 设置

    mysql根据my.cnf中max_allowed_packet的大小来限制接收到的数据包大小. 据官网描述,如下图. 数据包的值范围为1k~1G, 只能是1024的倍数,不能整除1024的,会向下取 ...

  4. HDU 4577 X-Boxes

    X-Boxes Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  5. Fixed DC-DC Regulator Output Uses A Digitally Controlled Potentiometer

    http://electronicdesign.com/print/passives/fixed-dc-dc-regulator-output-uses-digitally-controlled-po ...

  6. [Android Studio] Android Studio如何删除module(转载)

    转载地址:http://blog.csdn.net/hyr83960944/article/details/37519299 当你想在Android Studio中删除某个module时,大家习惯性的 ...

  7. JVM 虚拟机字节码指令表

    把JVM虚拟机字节指令表整理了一下,方便搜索,偶尔复习下 纯手工整理,可能存在一些问题,如果发现请及时告之我会修正 字节码 助记符 指令含义 0x00 nop None 0x01 aconst_nul ...

  8. 利用WMI检测电脑硬件信息,没办法显示cpu的信息

    但你要给某些系统或软件加密时,需要了解到服务器的硬件信息时,系统和软件会利用WMI检测硬件信息, 而有时我们会遇到检测不到CPU的型号信息,如图 此时的解决方法: 1.确定“服务”里启动了WMI 2. ...

  9. C/C++下scanf的%匹配以及过滤字符串问题

    最近在写一个测试的小程序,由于用到了sscanf函数对字符串进行标准读入,而sscanf在很多方面都与scanf比较相像,于是对scanf进行了一番测试,遇到了一系列基础性的问题,恶补基础的同时也体现 ...

  10. Fragment 生命周期 事务 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...