9_Palindrome Number
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: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: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?
版本一: 思想是把数字拆成一位一位的数组, 首位比较, 好像转换成字符串了, 违反Follow up了
// 偶数位时回文数的退出是关键,
class Solution {
public:
bool isPalindrome(int x) {
bool ret = false;
vector<int> position;
int begin = 0, end = 0;
if (x < 0 ) {
return ret;
}
else if (0 == x) {
return true;
}
else {
while (0 != x) {
position.push_back(x % 10);
x /= 10;
}
end = position.size() - 1;
while (begin != end) {
if (position[begin] != position[end]) {
return ret;
}
// 偶数个位数时终止循环, 否则判断之后数组访问越界, 位置不能换
if ((begin + 1) == end) {
break;
}
begin++;
end--;
}
ret = true;
return ret;
}
}
};
版本二: 借助7_Reverse Integer翻转数字的思想, 比较原数字和翻转之后的数字是否相同进行判断
// 借助[7_Reverse Integer](https://www.cnblogs.com/hesper/p/10397725.html)翻转数字的思想
// 比较原数字和翻转之后的数字是否相同进行判断
class Solution {
public:
bool isPalindrome(int x) {
int y = x;
//int tmp = 0; // int 类型x翻转 后数字范围可能大于int最大值
long tmp = 0;
vector<int> position;
int begin = 0, end = 0;
if (x < 0 ) {
return false;
}
else {
while (0 != x) {
tmp = tmp*10 + x%10;
x /= 10;
}
if (tmp == y) {
return true;
}
else {
return false;
}
}
}
};
LeetCode精简版
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0)
return false;
long long result = 0;
int temp = x;
while(temp) {
result *= 10;
result += temp % 10;
temp /= 10;
}
if ((int)result == x)
return true;
return false;
}
};
9_Palindrome Number的更多相关文章
- JavaScript Math和Number对象
目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Eclipse "Unable to install breakpoint due to missing line number attributes..."
Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...
- 移除HTML5 input在type="number"时的上下小箭头
/*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...
- iOS---The maximum number of apps for free development profiles has been reached.
真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...
- 有理数的稠密性(The rational points are dense on the number axis.)
每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [LeetCode] Number of Boomerangs 回旋镖的数量
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
随机推荐
- 《To C产品经理进阶》
我所说的,都是错的. To C产品设计和To B产品设计对一个优秀的产品经理的洞察能力.架构能力有共通的要求. 实际产品设计过程中,To C产品往往是从商业思维思考,侧重用户研究,思考用户心智,由产品 ...
- Solon详解(11)- Mybatis 与 Solon 相亲相爱
Solon详解系列文章: Solon详解(一)- 快速入门 Solon详解(二)- Solon的核心 Solon详解(三)- Solon的web开发 Solon详解(四)- Solon的事务传播机制 ...
- Github个人首页美化指北
当你尝试去创建一个与你Github用户名相同的仓库时,会发现这是Github为你预留的特殊仓库,用来作为你的Github Profile,这个仓库就相当于一个可以显示在你Github个人页的READM ...
- V4L2摄像头应用编程(转)
Video for Linuxtwo(Video4Linux2)简称V4L2,是V4L的改进版.V4L2是linux操作系统下用于采集图片.视频和音频数据的API接口,配合适当的视频采集设备和相应的驱 ...
- MeteoInfoLab脚本示例:获取气团轨迹每个节点的气象数据
读取HYSPLIT输出的轨迹数据文件和相应时间的气象数据文件,生成轨迹图层,循环每条轨迹的节点,读出该节点的经度.纬度.气压.时间,通过对气象数据插值获得该节点的气象数据.脚本程序: #------- ...
- std::hash
std::hash 由于C++11引入了哈希表数据结构std::unordered_map和std::unordered_set,所以对于基本类型也实现了标准的哈希函数std::hash,标准并没有规 ...
- 16.深入k8s:Informer使用及其源码分析
转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 由于这部分的代码是在client-go 中,所以使用的源码版本是client-go 1. ...
- go 结构体与方法
go 结构体与方法 go 结构体相当于 python 中类的概念,结构体用来定义复杂的数据结构,存储很多相同的字段属性 结构体的定义 1.结构体的定义以及简单实用 package main imp ...
- docker19.03限制容器使用的内存资源
一,指定内存大小的参数: [root@localhost liuhongdi]# docker run -idt --name kafka2 --hostname kafka2 -m 200M --m ...
- Windows Server 2003 Enterprise Edition SP2
SN: MPQ6X-3MCCF-47H9T-TKC2F-T69WM