/*
查看网上的思路有两种:
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. 创建实验楼课程app模块以及配置图片路径

    1.创建course模型 1.1 创建用户模型course python ../manage.py startapp course # 创建course模型 1.2 在setting.py中注册cou ...

  2. 简单dp水题

    #include <bits/stdc++.h> using namespace std; #define limit (100 + 5)//防止溢出 #define INF 0x3f3f ...

  3. 第7.14节 Python类中的实例方法详析

    第7.14节 Python类中的实例方法详析 一.    实例方法的定义 在本章前面章节已经介绍了类的实例方法,实例方法的定义有三种方式: 1.    类体中定义实例方法 第一种方式很简单,就是在类体 ...

  4. 什么是Python迭代器?

    迭代器(Iterator):迭代器可以看作是一个特殊的对象,每次调用该对象时会返回自身的下一个元素,从实现上来看,一个迭代器对象必须是定义了__iter__()方法和next()方法的对象. Pyth ...

  5. PyQt(Python+Qt)学习随笔:Qt Designer中部件的windowIcon属性

    windowIcon对象为部件对象的属性,但只有窗口对象有效,其他派生对象如pushButtong对象无效. 在windowIcon对象上有如下子属性设置: 这几个子属性实际上是QIcon类中继承的. ...

  6. 如何利用Excel快速批量生成想要的代码

    如何利用Excel快速批量生成想要的代码 使用场景 在HTML DOM Video 对象这个页面 我想要将所有的中文描述和对应的属性(共32个属性)打印出来--console.log(descript ...

  7. GYCTF Web区部分WP

    目录: Blacklist Easyphp Ezsqli FlaskApp EasyThinking 前言: 这次比赛从第二天开始打的,因为快开学了所以就没怎么看题目(主要还是自己太菜)就只做出一道题 ...

  8. P6772 [NOI2020]美食家

    题目大意 给你一个 \(n\) 个点,\(m\) 条边的有向图,每条边有一个权值 \(w_i\) ,每个节点有一个权值 \(a_i\) . 你从节点 \(1\) 出发,每经过一个节点就可以获得该点的权 ...

  9. 题解-CF1239D Catowice City

    CF1239D Catowice City 有 \(n\) 个人和 \(n\) 只猫.有 \(m\) 对人猫友谊,即第 \(u_i\) 个人认识第 \(v_i\) 只猫,保证第 \(i\) 个人和第 ...

  10. cocosCreator微信小游戏排行榜思路

    cocosCreator制作微信小游戏排行榜实现方案: 游戏认知:项目分为主域和子域,主域就是游戏主程部分,子域为单独处理微信排行榜公共域数据的. 游戏主域里创建一个节点,添加WXSubContext ...