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 ...
随机推荐
- python在一个画布上画多个子图
转载:https://blog.csdn.net/qq_26449287/article/details/103022636 matplotlib 是可以组合许多的小图, 放在一张大图里面显示的. 使 ...
- Windows下安装VScode,并使用,以及中文配置
转载:https://blog.csdn.net/x15011238662/article/details/85094006 首先明确一点,VScode是开发Go应用的基础编辑器,是Microsoft ...
- 使用响应扩展的响应面(Rx)
下载demo - 196 KB 下载source - 98 KB 表的内容 系统要求反应面一个简单的计时器从事件中收集数据序列使用更复杂的查询订阅您希望完成的面最终考虑历史 介绍 "Rx&q ...
- Springboot集成logback,控制台日志打印两次,并且是不同的线程打印的
背景 在搭建一个新项目的时候,从公司别的项目搞了个logback-spring.xml的配置过来,修改一下启动项目的时候发现 所有的日志都输出了两次 并且来自于不同的线程,猜测是配置重复了,但是仔细检 ...
- ansible-playbook-jinja2管理nginx配置文件
1. 案例1:创建jinja2的nginx的主配置文件 1) 编写jinja2的nginx的主配置文件 1 [root@test-1 jinja2]# vim /ansible/jinja2/tes ...
- 微信小程序适配iPhone X
1.获取设备型号 App({ // 全局数据 globalData: { // 其他数据定义 ... isIPX: false, // 当前设备是否为 iPhone X }, // 小程序启动入口 o ...
- ARM-Linux S5PV210 UART驱动(转)
ARM-Linux S5PV210 UART驱动(3)----串口核心层.关键结构体.接口关系 尽管一个特定的UART设备驱动完全可以按照tty驱动的设计方法来设计,即定义tty_driver并实现t ...
- 2016年 实验三 B2C模拟实验
实验三 B2C模拟实验 [实验目的] 掌握网上购物的基本流程和B2C平台的运营 [实验条件] ⑴.个人计算机一台 ⑵.计算机通过局域网形式接入互联网. (3).奥派电子商务应用软件 [知识准备] 本实 ...
- if-else和if if的对比
- 【转载】opencvVS2019配置方法
环境: 系统:win10系统截至2020920版本 opencv版本:3.0.0版本 IDE:宇宙最强IDEA最新版本2019社区版 教程: 1.下载opencv安装包官网下载链接:https://o ...