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.

思路:

不能用额外的空间,数字还有可能过大。 处理方法是把数字截为两半,后半段翻转与前半段对比

class Solution {
public:
bool isPalindrome(int x) {
int y = ; //把x的后半段反过来
if(x > && x % == ) //如果大于0的数字以0结尾 一定不是回文
return false;
if(x >= && x < ) //个位数一定是回文
return true;
while(y <= x)
{
if(y == x || (x / > && y == x / )) //偶数个数字 和奇术个数字都要考虑
return true;
y = y * + x % ;
x = x / ; //把x后面给y了的截掉
}
return false;
}
};

同样思路,更简洁的代码:

class Solution {
public:
bool isPalindrome(int x) {
int i = ;;
if ((x % == && x != ) || x < ) return false;
while (i < x) {
i = i * + x % ;
x = x / ;
}
return (i == x || i / == x);
}
};

【leetcode】Palindrome Number (easy)的更多相关文章

  1. 【leetcode】Palindrome Number

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

  2. 【LeetCode】Palindrome Number(回文数)

    这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...

  3. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  4. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  5. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  6. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  7. 【LeetCode】Palindrome Partitioning 解题报告

    [题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

  8. 【leetcode】Happy Number(easy)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  9. 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...

随机推荐

  1. 关于web请求中 获取真实IP

    HttpRequest request = HttpContext.Current.Request; if (!string.IsNullOrEmpty(request.ServerVariables ...

  2. 32位系统下使用4GB内存

    64位系统的驱动还有不少缺陷,果断重装回32位系统,但是4gb的内存,明显是浪费啊. 所以必须利用起来. 我没有采用不稳定的破解内核的做法,采用了虚拟硬盘的做法.因为个人觉得这样其实利用效率更高. 方 ...

  3. (转)Web2.0 大型互联网站点的架构

    这种资料.向来可遇不可求啊 WikiPedia 技术架构学习分享 http://www.dbanotes.net/opensource/wikipedia_arch.html YouTube 的架构扩 ...

  4. javascript 笔记——setTimeout的参数问题

    setTimeout("xxx",500) 双引号中的作用域不捕捉局部变量,因此会报错误 如果你需要在双引号中可以在外部定义一个变量 var now; window.onload ...

  5. windbg远程调试

    1, A,调试机. B,被调试机. 2, 在B机上安装windbg,公共符号文件,程序的PDB都要复制过来. 公共符号文件位置设置在于A机相同的位置. windbg–server tcp:port=5 ...

  6. 阅读《Oracle内核技术揭秘》的读书笔记

    阅读<Oracle内核技术揭秘>,对oracle的内存结构.锁.共享池.undo.redo等整理成了如下的思维导图:

  7. 文件存储之-内存文件系统tmpfs

    前言 我们都知道,对于单台服务器来说,除了 CPU ,内存就是我们存储数据最快的设备.如果可以把数据直接存储在内存中,对于性能的提升就不言而喻了.那么我们先来讲讲如何使用内存来存储文件. 首先,我们先 ...

  8. SQL SERVER 遇到Unable to find the requested .Net Framework Data Provider. It may not be installed. (System.Data)

    今天新装的SQLSERVER 2012 EXPRESS 用于客户端程序 安装完成后打开登陆SQLSERVER 一切正常 当查看表定义.视图结构时,弹出一下内容 Unable to find the r ...

  9. 【转】Qt使用自带的windeployqt 生成exe来发布软件

    集成开发环境 QtCreator 目前生成图形界面程序 exe 大致可以分为两类:Qt Widgets Application  和 Qt Quick Application.下面分别介绍这两类exe ...

  10. Python入门二:函数

    一.函数的定义和使用 1.基本结构: def 函数名(参数): """ 文档字符串 """ 函数体 返回值 2.函数名: 和变量名命名规则一 ...