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 ...
随机推荐
- vue-留言板-bootstrap
最近看完入门API,看完视频自己写了个留言板,因为主要是学习vue,所以就复习了一下bootstrap,布局更简单,先看看样式吧. 简单清晰的布局,先说一下功能, 1.输入用户名密码点击提交放入表格 ...
- DevExpress 如何读取当前目录下文件,加载至grid
DBFileName=DevExpress.Utils.FileHelper.FindingFileName(Appliaction.StartupPath,"Data\\Product&g ...
- java json转义引号
String jsonMapStr = "{\"system\":\"1,\\\"2\\\",3\",\"createD ...
- mysql 各项操作流程
启动mysql:进入命令行输入:net start mysql 如果失败则显示:服务名无效,需跳转到指定Bin目录下进行启动mysql, 成功则进行下一步:登陆 :mysql -uroot -proo ...
- CorelDRAW 2019新品发布,行业大咖就差你了
近日,由苏州思杰马克丁软件公司独家代理的CorelDRAW 2019将在苏州开启一场设计上的饕餮盛宴,您报名了么? 不管您是专业的设计师还是热爱设计的狂热粉丝,都将有机会参与到我们的活动中,为了这场盛 ...
- redis RDB快照和AOF日志持久化配置
Redis持久化配置 Redis的持久化有2种方式 1快照 2是日志 Rdb快照的配置选项: save 900 1 // 900内,有1条写入,则产生快照 save 300 1000 ...
- javascript事件列表解说
javascript事件列表解说 事件 浏览器支持 解说 一般事件 onclick IE3.N2 鼠标点击时触发此事件 ondblclick IE4.N4 鼠标双击时触发此事件 onmousedown ...
- URL编码及解码
为什么要对URL进行编码? 一般来说,网页URL只能使用英文.数字.还有一些特定的字符.根据网络标准RFC 1738做了硬性规定: 只有字母和数字[0-9a-zA-Z].一些特殊符号"$-_ ...
- WEBGL学习【七】画布绘图
主要是对WEBGL的绘图部分进行了进一步加强的认识和理解 <!DOCTYPE HTML> <html lang="en"> <head> < ...
- Gym-101615D Rainbow Roads 树的DFS序 差分数组
题目链接:https://cn.vjudge.net/problem/Gym-101615D 题意 给一棵树,每个边权表示一种颜色. 现定义一条彩虹路是每个颜色不相邻的路. 一个好点是所有从该节点开始 ...