LeetCode——Palindrome Number

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,布布扣,bubuko.com

时间: 2024-11-05 07:11:26

LeetCode——Palindrome Number的相关文章

[LeetCode] [Palindrome Number 2012-01-04]

Determine whether an integer is a palindrome. Do this without extra space. if use recursive, like check the first dig and last dig, then remove them, check the rest, it will fail when digint like "1021", when remove the first and last one, the r

[LeetCode] Palindrome Number [13]

题目 Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using

[LeetCode] Palindrome Number 验证回文数字

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

(LeetCode)Palindrome Number -- 判断回文数

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 ex

LeetCode—Palindrome Number 数字是否是回文数字

Determine whether an integer is a palindrome. Do this without extra space. 检测当前数字是否是回文数字,同时不能增加额外的内存空间,这里一个注意的点就是 负数 都不可能是回文数字 然后是检测出来每一位数字进行比较 代码还是写得比较繁琐,主要的一个点就是数字的位数是基数位和偶数位的时候处理的过程是不同的 class Solution { public: int GetH(int x,int num)//获得对应位置的数字 {

LeetCode:Palindrome Number,Reverse Integer

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

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

本来判断回文串是一件很容易的事情,只需要反转字符串后在与原字符串相比较即可.这道题目明确说明不能使用额外的空间,那么使用将其分解连接成字符串的方法便不是可行的.只好采用数学的方式: 每次取最高位和最低位相比较,总的位数可以用一个while先处理出来,循环直至取余和除数相等. 具体见代码: class Solution { public: bool isPalindrome(int x) { if(x<0) //special due return false; if(x<10) return

LeetCode:Palindrome Number

基本思路:把 int 转换成string  借用stringstream  时间复杂度O(n) 1 class Solution { 2 public: 3 public: 4 bool isPalindrome(int x) { 5 6 stringstream ss; 7 ss<<x; 8 string str=ss.str(); 9 int sfront = 0; 10 int sback=str.length()-1; 11 while(sfront<sback) 12 { 13

leetcode:Palindrome Number【Python版】

一次AC 题目要求中有空间限制,因此没有采用字符串由量变向中间逐个对比的方法,而是采用计算翻转之后的数字与x是否相等的方法: 1 class Solution: 2 # @return a boolean 3 def isPalindrome(self, x): 4 o = x 5 ret = 0 6 flag = 1 7 if x < 0: 8 return False 9 while(x!=0): 10 ret = ret*10+x%10 11 x = x/10 12 return ret