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. Extjs 下拉框

    刚刚熟练了easyui控件的使用,又開始了如今的这个项目. 这个项目是个半成品.前端使用的是Extjs控件,jsp中没有代码.就引用了非常多的js...于是乎有种不知所措了呀. . . 说实话特别的不 ...

  2. hadoop基础学习---数据管理策略

    上图中的ABCDE都代表默认大小64M的数据块 nameNode与dataNode之间有一个心跳机制,datanode每隔多秒钟定期的发送心跳到nameNode

  3. 【Matlab】运动目标检测之“光流法”

    光流(optical flow) 1950年,Gibson首先提出了光流的概念,所谓光流就是指图像表现运动的速度.物体在运动的时候之所以能被人眼发现,就是因为当物体运动时,会在人的视网膜上形成一系列的 ...

  4. 基于pyteseract google ocr的图形验证码识别

    先灰化图片,把图片二值化,利用pytesseract包的pytesseract.image_to_string转换出文字.

  5. Android Bigmap的操作(绘制,裁剪)

    绘制 依据某一种颜色绘制Bitmap Bitmap resultBitmap = Bitmap.createBitmap(600,800,Bitmap.Config.ARGB_8888); resul ...

  6. day24<多线程>

    多线程(多线程的引入) 多线程(多线程并行和并发的区别) 多线程(Java程序运行原理和JVM的启动是多线程的吗) 多线程(多线程程序实现的方式1) 多线程(多线程程序实现的方式2) 多线程(实现Ru ...

  7. 【渗透测试学习平台】 web for pentester -5.代码执行

    Example 1 http://192.168.106.154/codeexec/example1.php?name=".system('uname -a');// Example 2 h ...

  8. 查看进程动态:top

    top命令用于查看进程动态,即进程使用系统资源的情况,常见用法如下: [root@localhost ~]$ top # 动态查看进程使用资源的情况,每三秒刷新一次 [root@localhost ~ ...

  9. Effective C++ —— 资源管理(三)

    条款13 : 以对象管理资源 假设有如下代码: Investment* createInvestment(); //返回指针,指向Investment继承体系内的动态分配对象,调用者有责任删除它 vo ...

  10. android编译make错误——"javalib.jar invalid header field”、"classes-full-debug.jar 错误 41 "

    错误:读取 out/target/common/obj/JAVA_LIBRARIES/core-tests_intermediates/javalib.jar 时出错:invalid header f ...