LeetCode9 Palindrome Number
题意:
Determine whether an integer is a palindrome. Do this without extra space. (Easy)
分析:
自己考虑的方法是利用Reverse Integer,然后查看rev与x是否相等,注意负数和0的判断(这里WA了一次)
代码1:
class Solution {
private:
int reverse(int x) {
long long result = ;
while (x != ) {
int temp = x % ;
x /= ;
result = result * + temp;
if (result > 0x7FFFFFFF || result < -0x7FFFFFFF) {
return ;
}
}
return result;
}
public:
bool isPalindrome(int x) {
if (x < ) {
return false;
}
int rex = reverse(x);
if (rex == && x != ) { //溢出(不是因为等于0而得到rex = 0)
return false;
}
if (rex == x) {
return true;
}
return false;
}
};
查看讨论区,有只比较一半的解法,循环中的思路和reverse integer类似,更为简洁,实现如下;
代码2:
class Solution {
public:
bool isPalindrome(int x) {
if (x < || (x % == && x != ) ) {
return false;
}
int sum = ;
while (x > sum) {
int temp = x % ;
sum = sum * + temp;
x /= ;
}
if (x == sum || x == sum / ) {
return true;
}
return false;
}
};
LeetCode9 Palindrome Number的更多相关文章
- leetcode9 Palindrome Number(按进阶要求)
题目描述 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same ...
- 65. Reverse Integer && Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, re ...
- 有趣的数-回文数(Palindrome number)
文章转自http://blog.163.com/hljmdjlln@126/blog/static/5473620620120412525181/ 做LC上的题"Palindrome num ...
- 9. Palindrome Number
/* Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers ...
- No.009 Palindrome Number
9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...
- 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List
9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...
- leetcode 第九题 Palindrome Number(java)
Palindrome Number time=434ms 负数不是回文数 public class Solution { public boolean isPalindrome(int x) { in ...
- HDU 5062 Beautiful Palindrome Number(数学)
主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5062 Problem Description A positive integer x can re ...
- Reverse Integer - Palindrome Number - 简单模拟
第一个题目是将整数进行反转,这个题实现反转并不难,主要关键点在于如何进行溢出判断.溢出判断再上一篇字符串转整数中已有介绍,本题采用其中的第三种方法,将数字转为字符串,使用字符串比较大小的方法进行比较. ...
随机推荐
- iOS打开手机QQ与指定用户聊天界面
开发中遇到一个联系客服qq的需求,找到这么一个实现方法,先记录下来.大概的原理就是,iOS启动第三方应用是采用schema模式的,这有点像url,打开不同的界面使用不同的地址.但这个url怎么得来的还 ...
- matlab 画锥体
>> plot3(x,y,z); >> [x,y,z]=cylinder([ ],)
- 转】Eclipse在线安装SVN
原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4354199.html 感谢! 对于,搞大数据的博主我,svn是需要了解的,很多源码包! 一.SVN在线安装 下面 ...
- WebRTC源码分析:音频模块结构分析
一.概要介绍WebRTC的音频处理流程,见下图: webRTC将音频会话抽象为一个通道Channel,譬如A与B进行音频通话,则A需要建立一个Channel与B进行音频数据传输.上图中有三个Chann ...
- [iOS微博项目 - 2.6] - 获取微博数据
github: https://github.com/hellovoidworld/HVWWeibo A.新浪获取微博API 1.读取微博API 2.“statuses/home_time ...
- 实时监控mysql数据库变化
对于二次开发来说,很大一部分就找找文件和找数据库的变化情况 对于数据库变化.还没有发现比较好用的监控数据库变化监控软件. 今天,我就给大家介绍一个如何使用mysql自带的功能监控数据库变化 1.打开数 ...
- 把我的Java项目部署到Linux系统
以前,还未毕业,凭借自己三脚猫的功夫,只会在Windows环境中使用tomcat容器把项目跑起来. 以前的操作是,利用Eclipse把项目导出成War包,放到tomcat的webApp文件夹中,鼠标点 ...
- easyui datagrid 的分页刷新按钮
datagrid 刷新bug: 情形: 当用户A,B 同时操作 datagrid时(记录1,记录2.记录3).如果A如果删除记录1, B此时已选中了记录1 ,记录2 , 这时B点击分页中的刷新按 ...
- OC键值观察KVO
什么是KVO? 什么是KVO?KVO是Key-Value Observing的简称,翻译成中文就是键值观察.这是iOS支持的一种机制,用来做什么呢?我们在开发应用时经常需要进行通信,比如一个model ...
- 7. 泛化(Generalization)
什么是泛化关系?用一个例子简单的说:假设A是B和C的父类,B.C具有公共类(父类)A,说明A是B.C的一般化(概括,也称泛化),B.C是A的特殊化. 在编程上,泛化关系(Generalization) ...