题意:

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. 多台服务器最好加上相同的machineKey

      <machineKey validationKey="6E993A81CF4BDCA1C1031528F55DADBB8AF1772A" decryptionKey=&q ...

  2. 如果Apache Spark集群中没有分布式系统,则会?

    若当连接到Spark的master之后,若集群中没有分布式文件系统,Spark会在集群中每一台机器上加载数据,所以要确保Spark集群中每个节点上都有完整数据. 通常可以选择把数据放到HDFS.S3或 ...

  3. Codeforces Round #368 (Div. 2) B. Bakery (模拟)

    Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...

  4. Unity3D Keynote

    [Unity3D Keynote] 1.场景文件扩展名为.unity. 2.up为Y正方向,down为Y负方向,right为X正方向,left为X负方向,forward为Z正方向,back为z负方向. ...

  5. [iOS微博项目 - 3.1] - 发微博界面

    github: https://github.com/hellovoidworld/HVWWeibo   A.发微博界面:自定义UITextView 1.需求 用UITextView做一个编写微博的输 ...

  6. HDU2838Cow Sorting(树状数组)

    题目意思是说给你一列数,每次可以将相邻的两个数交换,这一步的代价是这两个数的和,求将所有数排好序的最少代价. 题解: 我们可以这么思考,由于每次都是交换相邻的两个数,所以将一个数放到它自己的位置去后, ...

  7. HDU 5707 Combine String (DP,LCS变形)

    题意:给定三个字符串,问你第三个是不是由第一个和第二个组成的. 析:当时比赛是没有做出来啊...一直WA,就是没有判断长度,第一个和第二个和是不是和第三个一样,这个忘记... 我们用d[i][j]表示 ...

  8. NSInvocation Basics

    In this article I'm going to cover the basics and usages of NSInvocation. What is NSInvocation? Appl ...

  9. How Tomcat Works(十二)

    tomcat容器通过一个称为Session管理器的组件来管理建立的Session对象,该组件由org.apache.catalina.Manager接口表示:Session管理器必须与一个Contex ...

  10. installshield Basic 工程每次安装完提示重启电脑

     将Sequence中的ScheduleReboot Action的Condition清空即可.