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 ...
随机推荐
- CodeForces 489D Unbearable Controversy of Being (搜索)
Unbearable Controversy of Being 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/B Descrip ...
- uva 10274 Fans and Gems(隐式图搜索+模拟)
Fans and Gems Input: Standard Input Output: Standard Output Tomy's fond of a game called 'Fans and G ...
- [置顶] 函数传递不定参数理解-c语言
感性认识 Typedef char *va_list;/*这个在<stdatg.h>中有定义*/ #define va_start(ap,p) (ap=(char*)(&(p)+1 ...
- MFC 视图、文档、框架(通讯)
CMainFrame * pMainWnd=(CMainFrame*)AfxGetApp()->m_pMainWnd;//主框架 CChildFrame * pChild = (CChildFr ...
- Enterprise Library 中加密数据库连接字符串
看了SHY520写的关于Data Access Application Block的文章,写得不错,忽略了一点就是如何去加密数据库连接字符串,这儿我简单的介绍一下.我们知道,在Enterprise L ...
- corpus academic writing
http://micusp.elicorpora.info/ http://corpus.byu.edu/coca/ http://rcpce.engl.polyu.edu.hk/RACorpus/
- C++ Button右键弹出式菜单
Button右键弹出式菜单 关键点 用类来实现 的 实现过程 新建1个类 类名CButtonPopMenu 基类CButton 新建1个菜单资源 IDR_MENU1 // ButtonPopMenu ...
- AES加密算法(C++实现,附源代码)
先搞定AES算法,基本变换包含SubBytes(字节替代).ShiftRows(行移位).MixColumns(列混淆).AddRoundKey(轮密钥加) 其算法一般描写叙述为 明文及密钥的组织排列 ...
- Nginx目录保护、防盗链、限速及多域名处理
http://www.opsers.org/server/nginx-directory-protection-anti-hotlinking-processing-speed-and-multi-d ...
- fscanf()函数具体解释
曾经解析有规律的文件的时候要么用正則表達式,要么就是傻傻的自己敲代码来解析有规律的文件.今天突然发现c的库函数中有一个现成的能够解析有规律的文件的函数,就是fscanf()函数.哎 曾经自己做了这么多 ...