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的更多相关文章

  1. JavaScript Math和Number对象

    目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...

  2. Harmonic Number(调和级数+欧拉常数)

    题意:求f(n)=1/1+1/2+1/3+1/4-1/n   (1 ≤ n ≤ 108).,精确到10-8    (原题在文末) 知识点:      调和级数(即f(n))至今没有一个完全正确的公式, ...

  3. Java 特定规则排序-LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  4. Eclipse "Unable to install breakpoint due to missing line number attributes..."

    Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...

  5. 移除HTML5 input在type="number"时的上下小箭头

    /*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...

  6. 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.免费应用程序调试最 ...

  7. 有理数的稠密性(The rational points are dense on the number axis.)

    每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.

  8. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  9. [LeetCode] Number of Boomerangs 回旋镖的数量

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

随机推荐

  1. Focal loss论文解析

    Focal loss是目标检测领域的一篇十分经典的论文,它通过改造损失函数提升了一阶段目标检测的性能,背后关于类别不平衡的学习的思想值得我们深入地去探索和学习.正负样本失衡不仅仅在目标检测算法中会出现 ...

  2. Java知识系统回顾整理01基础03变量06变量的作用域

    一.变量根据所处的位置对应不同的名称 变量处于不同的位置,有不同的名称 名称分别是  字段,属性 参数 局部变量 不同名称的变量,其作用域是不一样的 二.字段,属性,Field 当一个变量被声明在类下 ...

  3. C#入门——Console.Write()与Console.WriteLine()

    参考:https://blog.csdn.net/qujunyao/article/details/72884670 两者区别: Console.Write("abc"); 输出到 ...

  4. matlab中卷积convolution与filter用法

    转自:https://blog.csdn.net/dkcgx/article/details/46652021 转自:https://blog.csdn.net/Reborn_Lee/article/ ...

  5. 【题解】SP1812 【LCS2 - Longest Common Substring II 】

    \(\text{Suffix Tree:}\)我来啦我来啦 \(\text{Solution:}\) 题目要求求好几个串串的\(\text{LCS.}\) 由于串串的数量并不多,所以我们把它们塞到一个 ...

  6. Linux系统编程 —共享内存之mmap

    共享内存概念 共享内存是通信效率最高的IPC方式,因为进程可以直接读写内存,而无需进行数据的拷备.但是它没有自带同步机制,需要配合信号量等方式来进行同步. 共享内存被创建以后,同一块物理内存被映射到了 ...

  7. Jmeter之连接数据库

    1.下载mysql驱动文件 2.移动到Jmeter目录下的lib中 3.在配置文件中使用 jdbc:mysql://127.0.0.1:3306/fns_test_bak?useUnicode=tru ...

  8. Ubuntu通过Nginx安装Webdav

    使用KeePass保存密码,在个人服务器上安装WebDav协议. # nginx nginx-extras apache2-utils sudo aptitude install nginx ngin ...

  9. 【数量技术宅|金融数据分析系列分享】为什么中证500(IC)是最适合长期做多的指数

    更多精彩内容,欢迎关注公众号:数量技术宅.探讨数据分析.量化投资问题,请加技术宅微信:sljsz01 投资股票指数相比个股的优势 我们在投资股票的时候,如果持仓集中在一只或者有限几只股票上,恰好不幸遇 ...

  10. 【5】进大厂必须掌握的面试题-Java面试-spring

    spring面试问题 Q1.什么是spring? Spring本质上是一个轻量级的集成框架,可用于用Java开发企业应用程序. Q2.命名Spring框架的不同模块. 一些重要的Spring Fram ...