一天一道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;
        }
    }
};

最新文章

  1. HTML 学习笔记 JQuery(DOM 操作)
  2. jQuery的deferred对象使用详解——实现ajax线性请求数据
  3. hdu 4274 2012长春赛区网络赛 树形dp ***
  4. Java语言概述
  5. Zabbix3.0 自动微信报障
  6. Ibatis学习总结7--SqlMapClient 执行 SQL 语句
  7. 用 xampp 在 windows/Linux 下搭建代理服务器
  8. SQL Server 2012清除连接过的服务器名称历史
  9. 初学JavaScript(入门一)
  10. 14.2.5.5 Change Buffer
  11. COM-TEAM
  12. Crystal Report 纵向排列,多列格式化
  13. multipath多路径实验01-构建iscsi模拟环境
  14. 团队作业4——第一次项目冲刺(Alpha版本) Day1
  15. 【BZOJ3529】数表(莫比乌斯反演,树状数组)
  16. springMvc(一)
  17. 使用labelme制作自己的数据集
  18. 《Java大学教程》—第7章 类的实现
  19. Linux更改目录及其子目录、文件的访问权限
  20. 数据库的连接使用——使用ADO.NET连接数据库

热门文章

  1. Tomcat7源码环境搭建
  2. 前端CSS技术全解(一)
  3. Android的Intent机制详解
  4. [LaTex]插图
  5. Dynamics CRM 站点地图中URL的&amp;号编码问题
  6. OpenMP与MPI联合编程
  7. Citrix 桌面虚拟化解决方案与VMware桌面虚拟化解决方案对比
  8. FFmpeg获取DirectShow设备数据(摄像头,录屏)
  9. MOAC中“MO:安全性配置文件“对于开发者
  10. 从websphere6.1迁移到weblogic10.3的问题总结