LeetCode题解 #9 Palindrome Number

题目大意:给定一个整型(即int),判断其是否为回文数

首先负数肯定不是回文了,只要判断正数就好。

将数字不断%10/10一个个取出来,放到一个数组中。然后再从数组两头开始往中间比较,有不等的马上返回false就好。

public static boolean isPalindrome(int x) {

if(x<0)
return false;

int [] data = new int [13];
int count = 0;

int temp = 0;

while(x>0){

data[count]=x%10;
x/=10;
count++;

}

for(int i=0,j=count-1;i<j;i++,j--){

if(data[i]!=data[j])
return false;

}

return true;

}

改进办法,在做成数组的时候,最后还要从两头往中间遍历,虽然int的位数不长,但是毕竟还是O(n)

所以不用数组了,直接构造从右边开始构造一个数,然后直接比较就好了。

bool isPalindrome(int x) {

// 负数肯定不为回文数

if (x < 0) return false;

// 将其反过来存放

long y = 0, z = x;

while (x > 0) { y = y * 10 + x % 10; x = x / 10; }

// 返回判断结果

return y == z;

}

时间: 2024-12-25 23:20:26

LeetCode题解 #9 Palindrome Number的相关文章

[LeetCode 题解]:Palindrome Number

前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 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 c

【LeetCode】009 Palindrome Number

题目:LeetCode 009 Palindrome Number 题意:判断一个整数是否为回文数,不要用额外空间 思路:我不会不用额外空间的方法,需要利用一个长度为20以内的字符串.将整数先写入一个字符串,然后判断首位字符是否相等即可. 代码如下: 1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 string s = to_string(x); 5 int len = s.size(); 6 for(int i = 0;

LeetCode Problem 9: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 a

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

【LeetCode】9 - 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

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 co

leetcode第九题--Palindrome Number

Problem: 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

LeetCode 8: Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. 本题是判断一个数是否是回文数. 代码如下: bool isPalindrome(int x) { int max = x; int min = 0; while(max >0){ min *= 10; min+= max %10; max /=10; } return min==x; }

[LeetCode] No. 9 Palindrome Number

[题目] Determine whether an integer is a palindrome. Do this without extra space. [题目解析] 判断一个给定整数是否为回文数,回文数即121,11411这种正着和反着相同的数字,最小的回文数是0.实现思路可以比较直接,先对int进行reverse,这个可以参考 http://www.cnblogs.com/zzchit/p/5806956.html,然后和给定数字比较即可.但是这道题困难的在这里“Do this wit