Leetcode 9. Palindrome Number(判断回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间)
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
分析:
回文数,并且题目要求不能使用额外的空间。
即,不能使用回文串的方法。
本题很简单,算出x的倒置数a,比较a是否和x相等就行了。
class Solution {
public:
bool isPalindrome(int x) {
//算出x的倒置数a,比较a是否和x相等就行了
int a = , b = x;
while(b > ){
a = a * + b % ;
b /= ;
}
if(a == x)
return true;
else
return false;
}
};
网上其他的代码,其思路:每次提取头尾两个数,判断它们是否相等,判断后去掉头尾两个数
class Solution {
public:
bool isPalindrome(int x) {
//negative number
if(x < )
return false;
int len = ;
while(x / len >= )
len *= ;
while(x > ) {
//get the head and tail number
int left = x / len;
int right = x % ;
if(left != right)
return false;
else {
//remove the head and tail number
x = (x % len) / ;
len /= ;
}
}
return true;
}
};
Leetcode 9. Palindrome Number(判断回文数字)的更多相关文章
- [LeetCode]9. Palindrome Number判断回文数字
/* 查看网上的思路有两种: 1.每次取两边的数,然后进行比较 2.取数的倒置数,进行比较 */ public boolean isPalindrome1(int x) { if (x<0) r ...
- LeetCode 9 Palindrome Number(回文数字判断)
Long Time No See ! 题目链接https://leetcode.com/problems/palindrome-number/?tab=Description 首先确定该数字的 ...
- LeetCode 9. Palindrome Number (回文数字)
Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...
- [LeetCode] 9. Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...
- [LeetCode] Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- [Leetcode] Palindrome number 判断回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- LeetCode OJ Palindrome Number(回文数)
class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ...
- LeetCode 9. Palindrome Number(回文数)
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
随机推荐
- [iOS基础控件 - 6.9.2] 静态单元格 QQ功能列表
使用storyboard设计静态的表格数据 A.实现步骤 1.控制器继承UITableViewController 2.在storyboard中使用TableViewController,删除原来 ...
- [C语言 - 4] 指针
存放变量地址的变量 int a = 1; int *p; p = &a; 在64位系统中,占用8个字节 直接引用 间接引用 *p : 指针指向的变量的值 不要使用未初始化的指针 1 ...
- FZU 2082 过路费 (树链剖分 修改单边权)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2082 树链剖分模版题,求和,修改单边权. #include <iostream> #include ...
- final static T
/** * An empty table instance to share when the table is not inflated. */ static final Entry<?,?& ...
- Serializable 序列化为文件
package test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...
- Listener 监听器
Listener的定义与作用 监听器Listener就是在application,session,request三个对象创建.销毁或者往其中添加修改删除属性时自动执行代码的功能组件. Listener ...
- CSS 边框的宽度
边框的宽度 您可以通过 border-width 属性为边框指定宽度. 为边框指定宽度有两种方法:可以指定长度值,比如 2px 或 0.1em:或者使用 3 个关键字之一,它们分别是 thin .me ...
- 比较器comparable与comparator的使用
在Java学习和使用里,工具类与算法类(collections和Arrays)也是我们使用比较多的,在它们里面就包含了comparable与comparator这两种比较器. 一.比较器的分类与概念 ...
- Java学习备忘录
1.工程名首字母可以小写,类名首字母大写 2.批量注释: Eclipse : ctrl+/ 如果不行 用 ctrl+7 或者 ctrl+shift+c Notepad++ : ctrl+q 或者 c ...
- 通过SCVMM分配iSCSI存储
除了使用基于SMB3.0应用程序的文件共享外,还可以使用iSCSI目标服务器的SAN存储,然后在SCVMM控制台中添加基于SMI-S类型的存储,步骤如下: 1.将一台安装了 iSCSI目标 功能的Wi ...