[Leetcode] Palindrome number 判断回文数
Determine whether an integer is a palindrome. Do this without extra space.
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
题意:判断一个整数是否为回文数。
思路:常规思路是,将数字转换成字符串或者用数组保存各位上的值,然后从两端开始遍历,判断是否为回文,或者将数反转以后,判断是否相等(这时要注意,若是数很大,怎么办,会溢出)。以上都不行,我就想,数为回文,要高位和低位上的值对应相等,那如何取得各个位上的值了?要是知道这个整数有多少位,就可以取余,或者求商得到对应位的值。按这个思路,我们首先求出的是这个数的位数,我们可以通过不断除10来得到位数。这里值得注意的是:每次比较完以后,如何找到下一对位置上值的比较。代码如下:
class Solution {
public:
bool isPalindrome(int x)
{
if(x<) return false;
if(x<) return true;
int base=;
while(x/base>=)
base*=;
while(x)
{
int lNum=x/base;
int rNum=x%;
if(lNum !=rNum)
return false;
x-=lNum*base;
x/=;
base/=;
}
return true;
}
};
[Leetcode] Palindrome number 判断回文数的更多相关文章
- Leetcode 9. Palindrome Number(判断回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...
- 【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] Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- palindrome number(回文数)
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- 9. Palindrome Number[E]回文数
题目 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same b ...
- LeetCode 9. Palindrome Number(回文数)
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- [LeetCode]9. Palindrome Number判断回文数字
/* 查看网上的思路有两种: 1.每次取两边的数,然后进行比较 2.取数的倒置数,进行比较 */ public boolean isPalindrome1(int x) { if (x<0) r ...
随机推荐
- Java实现文件的上传下载
文件上传,下载的方法: 上传代码 /** * 文件上传.保存 * * @param mapping * @param form * @param request * @param response * ...
- mysql计算排名
mysql计算排名,获取行号rowno 学生成绩表数据 SELECT * FROM table_score ORDER BY score DESC; 获取某个学生成绩排名并计算该学生和上一名学生成绩差 ...
- python的爬虫代理设置
现在网站大部分都是反爬虫技术,最简单就是加代理,写了一个代理小程序. # -*- coding: utf-8 -*- #__author__ = "雨轩恋i" #__date__ ...
- PyCharm使用秘籍视频
PyCharm使用视频上传至企鹅群公告 需要自行添加群获取
- C语言实例解析精粹学习笔记——35(报数游戏)
实例35: 设由n个人站成一圈,分别被编号1,2,3,4,……,n.第一个人从1开始报数,每报数位m的人被从圈中推测,其后的人再次从1开始报数,重复上述过程,直至所有人都从圈中退出. 实例解析: 用链 ...
- 4,由spring展开的串烧
一.什么是Spring框架?Spring框架有哪些主要模块? Spring框架是一个为Java应用程序的开发提供了综合.广泛的基础性支持的Java平台.Spring帮助开发者解决了开发中基础性的问题, ...
- 【jQuery】 常用函数
[jQuery] 常用函数 html() : 获取设置元素内的 html,包含标签 text() : 获取设置元素内的文本, 不包含标签 val() : 获取设置 value 值 attr() : 获 ...
- javac、jar使用实录
因项目管理部署需要,记录一下过程,以免下次忘记了,再次使用又需要重头再来,只记录正确的操作方式,可能会提到某些错误 建立项目所在目录F:\www 案例一 其下建立项目的java源文件的包目录结构.ja ...
- jenkins手动安装插件
插件下载地址: 搜索:https://plugins.jenkins.io/ 列表:https://updates.jenkins-ci.org/download/plugins/ 打开jenkins ...
- Qt 蓝牙部分翻译
这是我第一次尝试翻译技术文档,自己英语太烂,一直不敢尝试,感谢生活,让我勇敢迈出这第一步. 大部分都是直译,如有不妥,还请制导. Qt Bluetooth The Bluetooth API prov ...