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. HDU 4611 Balls Rearrangement 数学

    Balls Rearrangement 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4611 Description Bob has N balls ...

  2. CDOJ 1048 Bob's vector 三分

    Bob's vector 题目连接: http://acm.uestc.edu.cn/#/problem/show/1048 Description Bob has a vector with mm ...

  3. jmeter用beanshell调用自己写的jar进行MD5加密

    1.先在eclipse里面写好MD5的加密文件,用eclipse执行一遍,确保文件不会报错 Str2MD5.java 内容如下: package hehe.md5; import java.secur ...

  4. vultr购买主机前的测速地址

    https://www.vultr.com/faq/ 拉倒最下面,有个地区测速,每个点开之后ping,看延迟再进行购买,因为对应不同的宽带速度不一样. 参考: https://pdf-lib.org/ ...

  5. 破解ZendStudio 10.1

    破解文件的网盘地址: http://pan.baidu.com/share/link?shareid=3562282358&uk=1543766223  

  6. 让Firefox支持offsetX、offsetY

    //计算光标相对于第一个定位的父元素的坐标 function coordinate(e){ var o = window.event || e, coord, coord_X, coord_Y; co ...

  7. Android SDK最小需求

    As a minimum when setting up the Android SDK, you should download the latest tools and Android platf ...

  8. One switched-capacitor IC simultaneously inverts, doubles, and halves the input voltage.

  9. Spring MVC - Hello World示例

    以下示例演示如何使用Spring MVC框架编写一个简单的基于Web的Hello World应用程序.首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发一个 ...

  10. Go -- 调用dll库

    package main import ( "syscall" "unsafe" ) func main(){ h, err := syscall.LoadLi ...