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.

推断一个整数是否是回文数。不要是使用而外的空间。

首先想到的是例如以下的方法,即若是个位数。则返回true,否则就前后相应的位置推断是否相等。

	public static boolean isPalindrome(int x) {
if(x / 10 == 0 && x >= 0)
return true;
String str = x + "";
char ch[] = str.toCharArray();
for(int i=0;i<ch.length / 2;i++){
if(ch[i] != ch[ch.length - i - 1])
return false;
}
return true;
}

也能够将原来的数逆一下,再与原来的数进行比較。

	public static boolean isPalindrome(int x) {
int reverse = 0,temp = Math.abs(x);//考虑了负数,转成正数处理 ?
while( x != 0 )
{
reverse = reverse * 10;
reverse = reverse + x%10;
x = x/10;
}
return reverse == temp;
}

LeetCode——Palindrome Number的更多相关文章

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

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

  2. [Leetcode]Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. 这题貌似解法挺多,直接用简单的把数倒置,没有考虑数 ...

  3. leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】

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

  4. [Leetcode] Palindrome number 判断回文数

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

  5. leetcode Palindrome Number python

    class Solution(object): def isPalindrome(self, x): """ :type x: int :rtype: bool &quo ...

  6. leetcode:Palindrome Number【Python版】

    一次AC 题目要求中有空间限制,因此没有采用字符串由量变向中间逐个对比的方法,而是采用计算翻转之后的数字与x是否相等的方法: class Solution: # @return a boolean d ...

  7. [LeetCode]Palindrome Number 推断二进制和十进制是否为回文

    class Solution { public: bool isPalindrome2(int x) {//二进制 int num=1,len=1,t=x>>1; while(t){ nu ...

  8. [leetcode] Palindrome Number(不使用额外空间)

    本来推断回文串是一件非常easy的事情,仅仅须要反转字符串后在与原字符串相比較就可以.这道题目明白说明不能使用额外的空间.那么使用将其分解连接成字符串的方法便不是可行的.仅仅好採用数学的方式: 每次取 ...

  9. Palindrome Number - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Palindrome Number - LeetCode 注意点 负数肯定是要return false的 数字的位数要分奇数和偶数两种情况 解法 解法一: ...

随机推荐

  1. 【MYSQL】导入中文后乱码问题

    http://fatkun.com/2011/05/mysql-alter-charset.html

  2. 第二章 入门(MyBatis)

    本章将会以简略的步骤告诉你如何安装和创建 MyBatis-Spring,并构建一个简单的数据访问事务性的应用程序. Installation 要使用 MyBatis-Spring 模块,你只需要包含 ...

  3. centos7,yum安装工具报错

    1.问题描述:yum安装gcc和其他的工具时一直报错: 2.问题解决: 网上看到有类似文章: No more mirrors to try. 得知这可能是错误的缓存源导致,直接两个命令解决: yum ...

  4. css之导航菜单的制作

    通过设置<a>的背景改变样式,通过a:hover改变交互效果,改变文字颜色color 纵向 <!DOCTYPE html> <html> <head lang ...

  5. 常用帝国cms标签收录

    帝国网站管理系统V6.6版-数据字典 :  http://www.phome.net/doc/manual/extend/html/dbdoc/index.html 帝国模板网:http://www. ...

  6. redisTools-IdGenerator

    public class IdGenerator : RedisToolBase { //redis客户端对象 private static readonly NedisClient client = ...

  7. git for c#, commit本地,pushserver

    //ok private static void push() { string wkDir = @"E:\DotNet2010\单位project\Git.Client\lib2Test\ ...

  8. swift学习笔记之控制流

    控制流: 1.if语句 let count = { print("yes") }else{ print("no") } 2.switch语句 (1)Swift中 ...

  9. python文件和目录操作方法大全(含实例)【python】

    转自:http://www.jb51.net/article/48001.htm 一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法. 1.得到当前工作目录,即当前Py ...

  10. Maven(一)-- 基础知识

    一.Maven的基本概念 Maven(翻译为"专家","内行")是跨平台的项目管理工具.主要服务于基于Java平台的项目构建,依赖管理和项目信息管理. 1.项目 ...