9. Palindrome Number[E]回文数
题目
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example1:
Input:121
Output:true
Example2:
Input:-121
Output:flase
Explanation:From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example3:
Input:10
Output:flase
Explanation:Reads 01 from right to left. Therefore it is not a palindrome.
C++思路
思路1:全部逆序
将数字全部逆转,与原数字比较。
思路2:逆转一半的数字
如何判断已经逆转到数字的一半呢?由于对于原数字是除以10,对于逆转的数字是乘以10,如果原来的数字已经小于新生成的数字,那么就表明已经到达数字的一半。此外还要分奇偶讨论,假设a是新生成的数字,b是原数字,则
- 如果是奇数,则判断a/10 == b
- 如果是偶数,直接判断 a == b
思路3:字符串分片处理(python)
Tips
回文判断方法
- 将数字逆序,并与原数字比较,查看是否一致
- 将数字后半部分逆转,将其与前半部分数字比较,查看是否一致。
c++
- 思路1
class Solution {
public:
bool isPalindrome(int x) {
if(x<0) //所有的负数都不是回文数
return false;
int a = x;
long b = 0;
while(a!=0){
b=b*10 + a%10;
a /= 10;
}
if(b == x)
return true;
else
return false;
}
};
- 思路2
class Solution {
public:
bool isPalindrome(int x){
//特殊情况
if(x < 0 || (x % 10 ==0 && x!=0)){
return 0
}
long a = 0;
int b = x;
while(a < b){
a = a * 10 + b % 10;
b /= 10;
}
return b = =a || b ==a/10;
}
};
Python
- 思路3
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
b = str(x)
converNum = b[::-1]
return converNum == b
- 思路1
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x<0:
return False
a = 0
b = x
while b>0:
a =a*10+b%10
b = b/10
return a==x
9. Palindrome Number[E]回文数的更多相关文章
- palindrome number(回文数)
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- [Leetcode] Palindrome number 判断回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 从0开始的LeetCode生活—9. Palindrome Number(回文数)
题目大意: 判断输入的数字是不是回文数.所谓回文数就是正反读都一样的数字,比如说11,121,1221这样子的数字.负数不会是回文数. 解题思路: 思路一:如果这个数是负数,则返回false,否则用一 ...
- LeetCode OJ Palindrome Number(回文数)
class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ...
- LeetCode 9. Palindrome Number(回文数)
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- 9. Palindrome Number(回文数)C++
将int转换为string,注意for循环判断条件的简化 class Solution { public: bool isPalindrome(int x) { ) return false; str ...
- Leetcode 9. Palindrome Number(判断回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...
- [LeetCode] Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
随机推荐
- 学习环境搭建2——安装django
下载django https://www.djangoproject.com/download/ 选择最新的版本Latest release,下载后解压.在含有setup.py的文件夹中执行如下命令: ...
- 修复wordpress插件编辑器漏洞
具体方法,将下面的代码添加到您的配置文件 wp-config.php中: define( 'DISALLOW_FILE_EDIT', true ); 以此关闭插件编辑器功能,一切就这么简单,漏洞也就不 ...
- 八叉树(Octree)Typescript 实现
Demo GitHub export class Octree { // 父&子树 private parent_node: any; private children_nodes: Octr ...
- [原创]Java常见笔试题知识点汇总
前天数梦工厂来学校招聘,笔试题比较有特点,全是Java题,基本就是Java的一些特点.凭记忆按照题目找到一些必备知识点 (1). try {}里有一个return语句,那么紧跟在这个try后的fina ...
- C#结束Explorer进程
private void Form1_Load(object sender, EventArgs e) { Process[] processes = Process.GetProcesses();/ ...
- 热重载 预编译 编译器 JS引擎 作用域
热重载就是页面每次改动,不需要手动去刷新,可自动刷新.保持vuex的状态. JS之预编译 JavaScript的预编译 编译器 JS引擎 作用域三者之间的关系 建议你先去看看你不知道的JavaScri ...
- java 文件夹不存在的解决方案
使用new File(path).mkdirs()创建所需路径,几十有多层不存在的路径也可以直接创建,切记方法名以s结尾,不带s的智能创建一层不存在的目录,不能自动创建多层目录结构.
- [bzoj3743 Coci2015] Kamp(树形dp)
传送门 Description 一颗树n个点,n-1条边,经过每条边都要花费一定的时间,任意两个点都是联通的. 有K个人(分布在K个不同的点)要集中到一个点举行聚会. 聚会结束后需要一辆车从举行聚会的 ...
- Linux下MATLAB安装及使用
安装过程 1.在在media目录下创建matlab文件夹,并挂载R2017b_glnxa64_dvd1.iso镜像文件 sudo mkdir /media/matlab sudo mount -t a ...
- gpio_request 原形代码
http://blog.csdn.net/maopig/article/details/7428561 其原型为 int gpio_request(unsigned gpio, const char ...