9. Palindrome Number

题目:

Determine whether an integer is a palindrome. Do this without extra space.

click to show spoilers.

Some hints:

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.

其实就是判断一个数字是不是回文数字。

一开始的思路是找到开始的数字和最后的数字进行比对,发现自己有点天真,哈哈哈,又不是字符串啦,不好比较。

正确的思路就是求出它的相反的那个数,然后判断他们两是不是相等的。

负数的情况感觉应该特判一下都不是回文,没有特判也过了,下面是代码:

 class Solution {
public:
bool isPalindrome(int x) {
int reverse=;
int temp=x;
while(temp>)
{
reverse=reverse*+temp%;
temp=temp/;
}
if(x==reverse)
return true;
else
return false;
}
};

leetcode题解 9. Palindrome Number的更多相关文章

  1. [LeetCode 题解]:Palindrome Number

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Determine ...

  2. 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...

  3. leetcode bug & 9. Palindrome Number

    leetcode bug & 9. Palindrome Number bug shit bug "use strict"; /** * * @author xgqfrms ...

  4. leetcode 第九题 Palindrome Number(java)

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

  5. 【LeetCode】9. Palindrome Number (2 solutions)

    Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click t ...

  6. C# 写 LeetCode easy #9 Palindrome Number

    9.Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it ...

  7. 【LeetCode】9. Palindrome Number 回文数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...

  8. LeetCode(9) - Palindrome Number

    题目要求判断一个整数是不是回文数,假设输入是1234321,就返回true,输入的是123421,就返回false.题目要求in-place,思路其实很简单,在LeetCode(7)里面我们刚好做了r ...

  9. 【一天一道LeetCode】#9. Palindrome Number

    一天一道LeetCode系列 (一)题目 Determine whether an integer is a palindrome. Do this without extra space. Some ...

随机推荐

  1. HTML中的置换元素和非置换元素

    我们都知道,行内元素不能够定义宽度和高度,但 img,input,button等标签作为行内元素却可以定义宽高,为什么呢?这就牵扯到了置换元素和非置换元素. 置换元素: w3c官方解释:“An ele ...

  2. HDU 3月ACM模拟赛T10 COUNT

    题面 //来自TKJ的友好提供 ^_^ 然后我又被卡了半天不知道怎么转移N3 于是听CWY给我讲 终于会了 (虽然他讲的不是很清楚QAQ 但是凭着我们之间根本不存在的默契竟然达成了心智联通) 重点在于 ...

  3. [luogu P3960] [noip2017 d2t3] 队列

    [luogu P3960] [noip2017 d2t3] 队列 题目描述 Sylvia 是一个热爱学习的女♂孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Syl ...

  4. react和vue的不同

    React严格上只针对MVC的view层,Vue则是MVVM模式 virtual DOM不一样,vue会跟踪每一个组件的依赖关系,不需要重新渲染整个组件树.而对于React而言,每当应用的状态被改变时 ...

  5. html和css进阶

    html和css进阶 相对地址与绝对地址 网页上引入或链接到外部文件,需要定义文件的地址,常见引入或链接外部文件包括以下几种: <!-- 引入外部图片 --> <img src=&q ...

  6. 详解Linux下swig 3.0.12的手动安装过程

    详解Linux下swig 3.0.12的手动安装过程 首先 从http://www.linuxfromscratch.org/blfs/view/cvs/general/swig.html上下载swi ...

  7. 跟随我在oracle学习php(13)

    常用的css样式 [class~="col-6"]:选择我所有类名中包含有col-6独立单词的元素 [class*="col-"]:选择所有类名中含有" ...

  8. utf8与utf8mb4的区别

    最近在写一个爬虫的多线程脚本,在异步插入数据库的时候总有部分数据插入失败,原因竟然是编码的问题.扪心自问,mysql最通用的中文字符编码就是utf-8了,通常情况下,utf-8作为中文编码是司空见惯的 ...

  9. Java 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

    import java.util.Scanner; /** *有一对兔子,从出生后第3个月起每个月都生一对兔子, *小兔子长到第三个月后每个月又生一对兔子,假如兔子 *都不死,问每个月的兔子总数为多少 ...

  10. python修炼第六天

    越来越难了....现在啥也不想说了,撸起袖子干. 1 面向对象 先来个例子: 比如人狗大战需要有狗,人所以创建两个类别模子def Person(name,sex,hp,dps): dic = {&qu ...