题意:

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

  1. leetcode9 Palindrome Number(按进阶要求)

    题目描述 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same ...

  2. 65. Reverse Integer && Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, re ...

  3. 有趣的数-回文数(Palindrome number)

    文章转自http://blog.163.com/hljmdjlln@126/blog/static/5473620620120412525181/ 做LC上的题"Palindrome num ...

  4. 9. Palindrome Number

    /* Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers ...

  5. No.009 Palindrome Number

    9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...

  6. 【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 ...

  7. leetcode 第九题 Palindrome Number(java)

    Palindrome Number time=434ms 负数不是回文数 public class Solution { public boolean isPalindrome(int x) { in ...

  8. HDU 5062 Beautiful Palindrome Number(数学)

    主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5062 Problem Description A positive integer x can re ...

  9. Reverse Integer - Palindrome Number - 简单模拟

    第一个题目是将整数进行反转,这个题实现反转并不难,主要关键点在于如何进行溢出判断.溢出判断再上一篇字符串转整数中已有介绍,本题采用其中的第三种方法,将数字转为字符串,使用字符串比较大小的方法进行比较. ...

随机推荐

  1. INTEL XDK 真机调试

    需要安装 1.google服务框架 2.google play 3.app preview

  2. NS_ENUM & NS_OPTIONS

    When everything is an object, nothing is. So, there are a few ways you could parse that, but for the ...

  3. python sleep

    Python Sleep休眠函数 Python 编程中使用 time 模块可以让程序休眠,具体方法是time.sleep(秒数),其中"秒数"以秒为单位,可以是小数,0.1秒则代表 ...

  4. JDBC Connection

    [ http://shift-alt-ctrl.iteye.com/blog/1967020]   关于JDBC中关于Connection的两个疑问:   1.Connection实例是线程安全的吗? ...

  5. [XAF]如何在非按钮事件中打开视图

    private static void OpenDetailView(XafApplication app) { IObjectSpace os = app.CreateObjectSpace(); ...

  6. [hihoCoder]#1039 : 字符消除

    Description 小Hi最近在玩一个字符消除游戏.给定一个只包含大写字母"ABC"的字符串s,消除过程是如下进行的: 1)如果s包含长度超过1的由相同字母组成的子串,那么这些 ...

  7. [读书笔记]ASP.NET的URL路由引擎

    作用 一般的URL: 举例:http://www.myapp.com/app.aspx?id=2&sessionid=29320xafafa02fa0zga0g8a0z 缺点: 不美观,不清晰 ...

  8. 事件委托&jQuery on

    例如: <h2>Great Web resources</h2> <ul id="resources"> <li><a hre ...

  9. Ehcache(04)——设置缓存的大小

    http://haohaoxuexi.iteye.com/blog/2116749 设置缓存的大小 目录 1     CacheManager级别 2     Cache级别 3     大小衡量 4 ...

  10. Android - Facebook KeyHash 設定

    转自:http://www.dotblogs.com.tw/newmonkey48/archive/2014/04/17/144779.aspx App要使用Facebook 分享時,設要在Faceb ...