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. c++ c++ 与 Java

    1.c++ c++ 如果1个类的定义中包含另一个类,那么在stdafx.h中 被包含的类必须放在包含类的前面,不然编译器找不到被包含类,c++没有包的概念,所以包含头文件时要注意顺序,而java不存在 ...

  2. phpcms v9如何更改分页显示条数?

    默认显示页码数有10条,比如想更改成显示3条,例如这样 上一页 1 2 3...34 下一页 更改phpcms\libs\functions\global.func.php,找到分页函数,大概在665 ...

  3. easyui-textbox 只读设置取消

    <script> $(function () { $("#txt_beginAmount").attr('readonly', true); $("#txt_ ...

  4. JavaScript------生成Guid方法

    转载: http://blog.csdn.net/limm33/article/details/51536529 代码: function newGuid() { var guid = "& ...

  5. 超全面的JavaWeb笔记day06<Schema&SAX&dom4j>

    1.Schema的简介和快速入门(了解) 2.Schema文档的开发流程(了解) 3.Schema文档的名称空间(了解) 4.SAX解析原理分析(*********) 5.SAX解析xml获得整个文档 ...

  6. Java 类设计----Java类的继承

    Java类的继承 为描述和处理个人信息,定义类Person: public class Person { public String name; public inat age; public Dat ...

  7. php-新特性,生成器的创建和使用

    mark 一下~ http://laravelacademy.org/post/4317.html

  8. java.lang.OutOfMemoryError 错误分类

    java.lang.OutOfMemoryError: Java heap space原因:Heap内存溢出,意味着Young和Old generation的内存不够.解决:调整java启动参数 -X ...

  9. php学习一:语法规则

    1.书写规则 在html中嵌入php的时候,需要有结束语,即<?php ...?>,在靠近结束符号的最后一个语句可以不用写分号: 但是在单独的php中,最后可以不用以?>来结尾; 2 ...

  10. ExtJS 6.2 基础使用

    一. 安装: 下载两个压缩包:分别是 ext-6.2.0-gpl(这个是ExtJS 的SDK文件,创建新项目的时候需要用). SenchaCmd-6.5.2-windows-64bit (这个下载下来 ...