[LeetCode]9. Palindrome Number回文数
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input: 121
Output: true
Example 2:
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:
Coud you solve it without converting the integer to a string?
还是回文的问题,这题没有给出数字的区间,不过我们不用担心,回文数正反一样所以反过来也不会溢出的,我们可以利用之前的整数翻转的方法,如果回文reverse之后肯定相等
另外根据题意负数肯定是不回文的
class Solution {
public boolean isPalindrome(int x) {
if(x<0) return false;
if(x!=reverse(x)) return false;
return true;
}
public int reverse(int x) {
int res = 0,tag=0;
while (x != 0) {
tag = res * 10 + x % 10;
if (tag / 10 != res) return 0;
res = tag;
x = x / 10;
}
return res;
}
}
[LeetCode]9. Palindrome Number回文数的更多相关文章
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- Leetcode 3——Palindrome Number(回文数)
Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过 ...
- 【LeetCode】Palindrome Number(回文数)
这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...
- LeetCode Problem 9:Palindrome Number回文数
描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- 【LeetCode】9. Palindrome Number 回文数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...
- [LeetCode] Prime Palindrome 质数回文数
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
- Palindrome Number 回文数
判断一个数字是否是回文数,尝试不用其他额外空间. 注意: 负数也有可能成为回文数吗? 如果你想让int转为string,注意不用其他空间这个约束. 你也可以翻转一个int,但是有可能会溢出. ...
- 【LeetCode】9 Palindrome Number 回文数判定
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- 9. Palindrome Number 回文数的判断
[抄题]: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the sam ...
随机推荐
- 开启struts2自带的开发模式常量
在以前的开发中,当修改一些配置时总是不能及时地更新到服务器,我们总会重新部署或重启来更新改变的内容,在struts2中可以通过一个常量来达到此目的.即在struts.xml中的<struts&g ...
- assert.strictEqual()
assert.strictEqual(actual, expected[, message]) 使用全等运算符(===)测试 actual 参数与 expected 参数是否全等. // 格式 ass ...
- 6、R语言绘制带errorbar 的柱状图
转载:http://www.cnblogs.com/xudongliang/p/7283733.html data <- data.frame(mean = c(10, 15), sd = c( ...
- 使用python内置模块os和openpyxl搜索指定文件夹下Excel中的内容
在指定路径下,搜索Excel文件中包含的指定内容,首先需要遍历指定路径,得到该路径下所有Excel文件的绝对/相对路径:然后读取Excel中内容,将文件中的每个单元格的值与要搜索的内容进行判断(正则比 ...
- window.parent ,window.top,window.self 详解及parent和opener的区别
window.parent ,window.top,window.self 详解 在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层fr ...
- ES Docs-3:Modifying Data
Modifying Data Indexing/Replacing Documents curl -XPUT 'localhost:9200/customer/external/1?pretty' - ...
- 【linux下-远程访问mysql数据库报错问题】
虚拟机跑Linux项目用到MySQL数据库,可是远程连接MySQL时总是报出erro 2003: Can't connect to MySQL server on '211.87.***.***' ( ...
- Windows10安装node.js,vue.js以及创建第一个vue.js项目
[工具官网] Node.js : http://nodejs.cn/ 淘宝NPM: https://npm.taobao.org/ 一.安装环境 1.本机系统:Windows 10 Pro(64位)2 ...
- 某人视频中提到的 Spark Streaming 优化的几点事项
某人,并未提他的名字,是因为看的视频是1年前的,视频里他吹得厉害.我看视频时,查了一下他在视频里说的要做到的东西,结果上网一查,就看到了很多人说他骗了钱后,就不管交了学费的人了.真假无从查起.但是无风 ...
- hdu 1729 Stone Game
Stone Game HDU - 1729 题意: 给定n个箱子,每个箱子的容量为si,每个箱子里最初有ci个石子,每次放入石子不能超过放入前的石子数的平方,谁无法继续放入石子就算输. /* 这是 ...