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 ...
随机推荐
- 036 01 Android 零基础入门 01 Java基础语法 04 Java流程控制之选择结构 03 嵌套if结构
036 01 Android 零基础入门 01 Java基础语法 04 Java流程控制之选择结构 03 嵌套if结构 本文知识点:Java中的嵌套if结构 什么是嵌套if结构? 概念: 嵌套if结构 ...
- 07 C语言常量
常量的定义 常量是指固定的值,固定值在程序执行期间不会改变.这些固定值,又叫做字面量. 常量可以是任意的基本数据类型,比如整数常量.浮点常量.字符常量,或字符串字面值,也有枚举常量. 不要搞得太复杂, ...
- Intel HEX格式
来来 !! come baby ! 只强调一点这篇文章有checksum的算法,是我最喜欢地!! 参考:https://blog.csdn.net/extlife/article/details/ ...
- Centos最小化安装后,不能使用yum命令的解决办法
刚刚最小化方式安装了CentOS 7 后,想查看一下config,却发现没有config文件,就想用yum下载一个,但是发现yum不能正常工作!!! 一,输入安装X Window命令,安装出错!! 在 ...
- Springboot应用使用Docker部署
首先准备好springboot应用,然后打包,我这里已经准备好了一个jar包 然后上传到服务器,准备一个目录用于存放jar包和Dokerfile文件 编写Dokerfile文件 我这里写的很简单,就简 ...
- shell-字符串多操作符综合实践多案例
1. 字符串测试举例 提示:下面的$file并未定义,而$file1 在上面测试中已定义. 范例1:单条件字符串测试: [root@test-1 ~]# file1=/etc/services ...
- vmware 安装tools
kali linux 更换成国内源后 安装tools命令 apt install open-vm-tools-desktop fuse -y 需重启 reboot
- linux(centos8):centos8.1安装(详细过程/图解)(vmware fusion/CentOS-8.1.1911-x86_64)
一,centos是什么? CentOS(Community Enterprise Operating System,中文意思是社区企业操作系统)是Linux发行版之一, 它是来自于Red Hat En ...
- 第八章 nginx基础介绍
一.nginx概述 nginx是一个开源且高性能.可靠的http web服务.代理服务. 开源:直接获取源代码 高性能:支持海量并发 可靠:服务稳定 二.nginx特点 1.高性能高并发 性能高,支持 ...
- npm 注册淘宝镜像
临时使用 npm --registry https://registry.npm.taobao.org install express 1 2.持久使用 npm config set registry ...