一天一道LeetCode系列

(一)题目

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

space.

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.

(二)解题

题目需要判断一个整数是不是回文数。需要注意一下几点:、

1、负数不是回文数

2、回文数满足反转后与原数相等,在反转整数那一题里面提到需要考虑越界问题,所以需要将int转换成long来处理。

代码比较简单,如下:

class Solution {
public:
    bool isPalindrome(int x) {
        if(x<0) return false;
        else{
            long n = 0;
            long temp = x;//考虑到反过来越界
            long lx = x;
            while(temp)
            {
                n = n*10+temp%10;//反转int数
                temp = temp/10;
            }
            if(n==lx) return true; //与原数相等则返回true
            else return false;
        }
    }
};

【一天一道LeetCode】#9. Palindrome Number的更多相关文章

  1. 【一天一道LeetCode】#191. Number of 1 Bits

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

  2. Leetcode练习题 Palindrome Number

    9. Palindrome Number Question: Determine whether an integer is a palindrome. An integer is a palindr ...

  3. Leetcode 9. Palindrome Number(水)

    9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...

  4. leetcode 9 Palindrome Number 回文数

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

  5. LeetCode 9. Palindrome Number (回文数字)

    Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...

  6. Leetcode 9. Palindrome Number(判断回文数字)

    Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...

  7. [LeetCode][Python]Palindrome Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/palindr ...

  8. 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]

    题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  9. [LeetCode] 9.Palindrome Number - Swift

    Determine whether an integer is a palindrome. Do this without extra space. 题目意思:判断一个整数是否是回文数 例如:1232 ...

  10. [LeetCode] 9. Palindrome Number 验证回文数字

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

随机推荐

  1. PGM:部分观测数据

    http://blog.csdn.net/pipisorry/article/details/52599451 基础知识 数据缺失的三种情形: 数据的似然和观测模型 Note: MLE中是将联合概率P ...

  2. Java进阶(四十二)Java中多线程使用匿名内部类的方式进行创建3种方式

    Java中多线程使用匿名内部类的方式进行创建3种方式 package cn.edu.ujn.demo; // 匿名内部类的格式: public class ThreadDemo { public st ...

  3. SSL协议相关证书文件

    密钥: 我理解是公钥+私钥的统称. 密钥对: 公钥(证书)和私钥成对存在.通信双方各持有自己的私钥和对方的公钥.自己的私钥需密切保护,而公钥是公开给对方的.在windows下,单独存在的公钥一般是后缀 ...

  4. Linux telnet远程登录操作

    telnet  (如果不行 可以却换root帐户试试 su - root) 1.安装telnet-server     sudo dpkg -i xinetd_1%3a2.3.14-7ubuntu3_ ...

  5. Spring的DataSource配置、将Hibernate配置全部写到Spring配置

    DataSource可以集中管理数据库连接,减少维护工作量,使部署更简单: Spring的DataSource配置:(Spring数据源配置)这里使用dbcp,还有很多其他的如c3p0,jdbc,jn ...

  6. FFmpeg源代码简单分析:libswscale的sws_getContext()

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  7. ExtJS学习(一)Ext自定义类实现

    工作中项目需要extjs,所以学习一下,做个笔记防止遗忘了.以后回忆起来也方便. 首先下载extjs官网地址:http://extjs.org.cn/ 下载以后的目录结构: 先写一个入门的程序吧自定义 ...

  8. JavaScript介绍-javaScript学习之旅(一)

    javaScript简介 1.javaScript是互联网上最流行的脚本语言,这门可用于web和html,更可广泛用于服务器端,pc端,移动端. 2.javaScript脚本语言: javaScrip ...

  9. linux service 简单易懂贴

    service用于管理Linux操作系统中服务的命令 1.不是在所有linux发行版本中都有.主要是在redhat.fedora.mandriva和centos中. 2. 命令位于/sbin目录下,用 ...

  10. Oracle 总账年终结算流程

     1.Oracle 总账应用中年终结算流程包含在开启/关闭期间程序里.当用户开启新一年的第一个期间,开启/关闭期间程序中的"gloire" 流程会完成传送所有收入及支出(损益表 ...