ITeye博客

Palindrome Number

plan454 · 2015-02-13 22:08 · leetcode刷题 · 非技术 · Markdown

Determine whether an integer is a palindrome. Do this without extra space. 判断是否为回文数,比较简单,可以用两种方式做,一种是用java中的stringbuffer的一个反转函数,一种直接判断数字。后一种效率比较高。前者代码量少点。 1: public class Solution { public boolean isPalindrome(int x) { StringBuffer sb = new StringBuffer(); sb.append(x); StringBuffer sb1 = new StringBuffer(sb); sb.reverse(); return sb.toString().equals(sb1.toString()); } } 2: public class Solution { public boolean isPalindrome(int x) { int a = x; int b = 0;

	while (a>=b)
	{
		if(x%10 == 0 && x != 0){
    		return false;
    	}
	  if (a == b) return true;
	  b = 10 * b + a % 10;
	  if (a == b) return true;
	  a = a / 10;
	}
	return false;
}

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。