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 ...
随机推荐
- Android WiFi热点完全研究(自定义创建、跳转系统界面设置、读取配置、切换,Android6.0适配)
前言: WiFi热点设置页面的安全性选项在Android 4.x上有“无”.“WPA PSK”.“WPA2 PSK”三个选项,在Android 5.0(含)之后去掉了WPA PSK选项(部分手机厂家会 ...
- boost的单例模式
template <typename T> struct singleton_default { private: struct object_creator { ...
- spring boot的项目结构问题
问题:spring boot项目能够正常启动,但是在浏览器访问的时候会遇到404的错误,Whitelable Error Page 404 分析及解决方案:首先Application文件要放在项目的外 ...
- 应运而生! 双11当天处理数据5PB—HiStore助力打造全球最大列存储数据库
阿里巴巴电商业务中历史数据存储与查询相关业务, 大量采用基于列存储技术的HiStore数据库,双11当天HiStore引擎处理数据记录超过6万亿条.原始存储数据量超过5PB.从单日数据处理量上看,该系 ...
- mongoDB 介绍(特点、优点、原理)
介绍:MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. 特点:高性能.易部署.易使用,存储数据非常方便. 主要功能特性有: Ø 面 ...
- JZOJ5804. 【2018.08.12提高A组模拟】简单的序列
性质:每个位置的前缀和必须大于0,总和=0.以此dp即可. #include <iostream> #include <cstdio> #include <cstring ...
- CENTOS 7发送邮件测试
centos7作为126邮箱客户端发送邮件测试. 首先安装客户端软件: yum install sendmail mailx -y 配置邮箱设置: 开启smtp发件协议 配置授权码,写入配置文件. 追 ...
- OSI层次介绍
1.应用层:为应用软件提供接口,使应用程序能够使用网络服务. 2.表示层:①数据的解码和编码,②数据的加密和解密,③数据的压缩和解压缩. 3.会话层:建立.维护.管理应用程序之间的会话. 功能:对话控 ...
- 03.IO读写-1.IO介绍
1 文件操作介绍 in: 输入,读入.从硬盘中读到内存 out: 输出.从内存写到硬盘 文件的作用: 数据存储 2 文件的打开与关闭 2.1 打开文件 在Python,使用open函数,可以打开一个已 ...
- 酒店管理系统(功能结构图、E-R图、用例图)
功能结构图 用例图 状态图 流程图 数据字典 er图