/*
查看网上的思路有两种:
1.每次取两边的数,然后进行比较
2.取数的倒置数,进行比较
*/
public boolean isPalindrome1(int x) {
if (x<0) return false;
//以四位数为例,取左边的数用的方法是/1000,取右边的数用的是%10
//注意每次取完两遍要更新数和位数
int len = 1;
int temp = x;
while (temp>=10)
{
len*=10;
temp/=10;
}
while (x!=0)
{
int left = x/len;
int right = x%10;
if (left!=right) return false;
len/=100;
//更新x的方法是先取出后几位,再去掉最右边
//%用来留下后边的,/用来留下前边的
x = (x%len)/10;
}
return true;
}
public boolean isPalindrome2(int x)
{
if (x<0) return false;
int a = 0;
int b = x;
while (b!=0)
{
a = a*10+b%10;
b%=10;
}
return (a==x);
}

[LeetCode]9. Palindrome Number判断回文数字的更多相关文章

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

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

  2. LeetCode 9 Palindrome Number(回文数字判断)

    Long Time No See !   题目链接https://leetcode.com/problems/palindrome-number/?tab=Description   首先确定该数字的 ...

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

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

  4. [LeetCode] 9. Palindrome Number 验证回文数字

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

  5. 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...

  6. [LeetCode] Palindrome Number 验证回文数字

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

  7. [Leetcode] Palindrome number 判断回文数

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

  8. LeetCode OJ Palindrome Number(回文数)

    class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ...

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

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

随机推荐

  1. keeplived高可用集群

    什么是Keeplived? Keepalived是Linux下面实现VRRP备份路由的高可靠性运行件.基于Keepalived设计的服务模式能够真正做到主服务器和备份服务器故障时IP瞬间无缝交接.二者 ...

  2. Python中判断一个中文是否中文数字的方法

    Python内置功能非常强大,在字符串内置函数中提供了一个判断字符串是否全数字的方法,而且这个方法不只是简单判断阿拉伯数字,包括中文数字和全角的阿拉伯数字都认识,这个函数就是字符串的isnumeric ...

  3. Python中sorted(iterable, *, key=None, reverse=False)函数参数定义中的独立星号(*)的含义

    老猿在 <Python中函数的参数带星号是什么意思?>中介绍了Python函数中参数带星号的含义,而在实际使用和Python的标准文档中,会看到某写函数(如sorted(iterable, ...

  4. PyQt学习随笔:QTableWidget的visualRow、visualColumn、logicalRow、logicalColumn(可见行、逻辑行、可见列、逻辑列)相关概念及方法探究

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概念 关于逻辑行logicalRow.列logicalColumn和可见行visualRow.列 ...

  5. C# operator 关键字的用法

    operator 只要是运算符都能重载 operator 关键字的主要作用是用来重载运算符的,还可以用于类或结构中类型的自定义转换. 下面看个例子 class Feige { //定义两个全局变量 i ...

  6. Kubernetes 教程:在 Containerd 容器中使用 GPU

    原文链接:https://fuckcloudnative.io/posts/add-nvidia-gpu-support-to-k8s-with-containerd/ 前两天闹得沸沸扬扬的事件不知道 ...

  7. Codeforces Edu Round 59 A-D

    A. Digits Sequence Dividing 注意特殊情况,在\(n = 2\)时除非\(str[1] >= str[2]\),否则可以把第一个数划分下来,剩下的数直接当成一组,一定满 ...

  8. 廖雪峰官网学习js 数组

    indexOf( )    某字符的位置 slice 相当于string 的substring 切片 a = ['a','b',1,2,3] (5) ["a", "b&q ...

  9. Json处理方式记录

    1.可以直接使用Parse方法 JObject jObject = JObject.Parse(res); string mediaId = jObject["media_id"] ...

  10. DBeaver连接MySQ报错

    遇错情况:第一次使用DBaver连接MySQL遇到以下问题: 报错信息:Public Key Retrieval is not allowed 截图如下: 解决方案步骤: 一.已有连接的情况:F4或者 ...