【LeetCode】009. 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.
题解:
easy题
Solution 1
class Solution {
public:
bool isPalindrome(int x) {
if (x < )
return false;
int base = ;
int tmp = x;
while (tmp > ) {
tmp /= ;
base *= ;
} while (x) {
int head = x / base;
int tail = x % ;
if (head != tail)
return false;
x = (x % base) / ;
base /= ;
} return true;
}
};
Solution 2
class Solution {
public:
bool isPalindrome(int x) {
if (x < )
return false;
int base = ;
while (x / base > ) {
base *= ;
} while (x) {
int head = x / base;
int tail = x % ;
if (head != tail)
return false;
x = (x % base) / ;
base /= ;
} return true;
}
};
【LeetCode】009. Palindrome Number的更多相关文章
- 【LeetCode】9. Palindrome Number (2 solutions)
Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click t ...
- 【LeetCode】9. Palindrome Number 回文数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...
- 【leetcode】9. Palindrome Number
题目描述: Determine whether an integer is a palindrome. Do this without extra space. 解题分析: ^_^个人觉得这道题没有什 ...
- 【leetcode】 9. palindrome number
@requires_authorization @author johnsondu @create_time 2015.7.13 9:48 @url [palindrome-number](https ...
- 【LeetCode】9 Palindrome Number 回文数判定
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
随机推荐
- web测试点梳理
前言 前面一篇文章讲解了app测试一些功能点.那么相应的也梳理一下web测试相关的功能的测试点吧,此篇文章只是给你们一个思路,如果要涉及web端每个测试点,基本不可能实现的,所以只是提供一个设计的思路 ...
- IEEE802.11数据帧在Linux上的抓取 80211格式转8023帧格式
转:http://blog.csdn.net/dog250/article/details/7749372 终于得到了梦寐的<802.11无线网络权威指南>,虽然是复印版本,看起来也一样舒 ...
- python日志操作logging
步骤: 1.定义一个日志收集器 my_logger = logging.getLogger("kitty") 2.设定级别.默认为warning:debug,,info,error ...
- 20145240《Java程序设计》第七周学习总结
20145240<Java程序设计>第七周学习总结 教材学习内容总结 12.1认识Lambda语法 12.1.1Lambda语法概览 在java中引入了Lambda的同时,与现有API维持 ...
- codeforces Codeforces Round #318 div2 A. Bear and Elections 【优先队列】
A. Bear and Elections time limit per test 1 second memory limit per test 256 megabytes input standar ...
- tomcat添加登录用户名密码
tomcat版本 apache-tomcat-7.0.55.tar.gz 编辑 TOMCAT_HOME/conf/tomcat-users.xml在tomcat-users里面添加 <tomca ...
- JAVA获取webapp路径
1.使用ServletContext获取webapp目录 在Servlet中 String path = getServletContext().getRealPath("/"); ...
- 1.mysql导论
虽然之前用过mysql一年多,但大多只是会用,深入了解的不多.所以想利用平时时间 系统的总结总结. 一.什么是数据库:(数据库软件) 1).什么是数据库(软件):数据库(DB:DataBase ...
- 【P2405】方格取数问题加强版(费用流)
考虑如何建图.还是老样子先拆点,然后把每两个点之间连接两条边,一条流量为1,费用为-点权,处理是否走这个点.一条流量无限,没有费用,因为哪怕一个点选过了,它的地方还是可以重复走过去的. 然后把经由一个 ...
- 重定向【TLCL】
> 重定向标准输出 > ls-output.txt 清空或者创建一个新文件夹 >> ...