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 ...
随机推荐
- UWP 协议启动
1.创建通过协议启动的项目 AppDemo 2.在项目的Package.appxmanifest 文件里 Extensions 目录下 <Extensions> <uap:Exten ...
- main函数解析
原文链接:http://parisliu2008.blog.163.com/blog/static/95070867200951510412959/ main参数 2009-06-15 10:41:2 ...
- Shiro从数据表中初始化资源和权限
之前在测试的Web工程下,我们在applicationContext.xml中配置了shiroFilter资源拦截器信息: <bean id="shiroFilter" cl ...
- highcharts例子
直接看代码 <script language="JavaScript"> $(document).ready(function() { $.ajax({ type:'p ...
- SweetAlert详解
官方给出的SweetAlert介绍是:SweetAlert可以替代JavaScript原生的alert和confirm等函数呈现的弹出提示框,它将提示框进行了美化,并且允许自定义,支持设置提示框标题. ...
- Rx (Reactive Extensions)
The Reactive Extensions (Rx) is a library for composing asynchronous and event-based programs using ...
- 04 Django-ORM多表操作(进阶)
一.创建模型 下面我们通过图书管理系统,来设计出每张表之间的对应关系. 通过上图关系,来定义一下我们的模型类. from django.db import models class Book(mo ...
- MySQL-字符类型与约束条件
创建表完整的语法: create table 表名(字段名1 类型[(宽度) 约束条件],字段名2 类型[(宽度) 约束条件],字段名3 类型[(宽度) 约束条件]); 注意事项: 1. 在同一张表中 ...
- 莫烦大大TensorFlow学习笔记(9)----可视化
一.Matplotlib[结果可视化] #import os #os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf i ...
- 面试题1-----SVM和LR的异同
1.异(加下划线是工程上的不同) (1)两者损失函数不一样 (2)LR无约束.SVM有约束 (3)SVM仅考虑支持向量. (4)LR的可解释性更强,SVM先投影到更高维分类再投影到低维空间. (5)S ...