[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 ...
随机推荐
- 关于SCANF 与 GETS(C语言)
SCANF遇到空格会自动停止录入字符串,而GETS不会,GETS可以用于输入带空格的字符串
- java多线程无锁和工具类
1 无锁 (1) cas (compare and swap) 设置值的时候,会比较当前值和当时拿到的值是否相同,如果相同则设值,不同则拿新值重复过程:注意,在设置值的时候,取值+比较+设值 是一条c ...
- SM4算法的c++实现
百度到的论文已给出算法. flag为1为解密,flag为0是加密. #include<bits/stdc++.h> using namespace std; typedef long lo ...
- [413D][搜索]D - Field expansion
http://codeforces.com/contest/799/problem/D 解题关键:因为3^11>100000,所以若只把2单独拿出,最多只需要暴力2^11次,故只需要dfs一下即 ...
- jquery获取ASP.NET服务器端控件dropdownlist和radiobuttonlist生成客户端HTML标签后的value和text值
—.获取dropdownlist的text(ddlList为服务器端dropdownlist的ID,生成name属性等于ddlList的select标签) $("#ddlList optio ...
- 网络应用(4):块的概念 | Range
分块来处理,也算是自然的想法,就是化整为零.而于对于文件的下载同样使用这个道理,既可整体下载,也可分块下载. 小程这里以http协议为例,来看一下块的概念与使用. http的range http1.0 ...
- haml
创建: 2019/05/23 文档: http://haml.info/docs/yardoc/file.REFERENCE.html 安装 安装 gem "haml" ...
- gulp使用文档
gulp的优势 易于使用:通过代码优于配置的策略,Gulp让简单的任务简单,复杂的任务可管理. 构建快速:利用 Node.js 流的威力,你可以快速构建项目并减少频繁的 IO 操作. 插件高质:Gul ...
- go培训课程都学什么?go语言框架学习:xorm框架知识介绍
xorm框架介绍 xorm是一个简单而强大的Go语言ORM库. 通过它可以使数据库操作非常简便. 通过xorm框架,开发者可以方便的使用各种封装好的方法来代替原生的sql语句.这样就降低了我们开发者使 ...
- c语言数组相关的计算
1.数组的创建:元素类型 数组名 [常量或者常量表达式] 如:int arr1[10];注:即使是被const修饰的变量也不能作为[]中的内容,它本质上依然属于变量,只是具有常量属性2.数组的初始化: ...