Determine whether an integer is a palindrome. Do this without extra space.

简介:

看到这个题目的时候,首先不认识 Determine这个单词。尴尬英文不好没办法,查了下是确认的意思,然后不懂 palindrome这个单词, 查了下是回文的意思。

问题是 回文是个什么东西,官方解释: palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. 回文

尽管英文不好,可是这个英文解释还是看懂了的。意思就是从前读到后面和倒过来读是一样的。

然后又不理解后面那句 do this without extra space. 大概意思是实现的时候不能使用其它空间,事实上还是不懂。

不知道第二个方法里的,Math.pow()这种方法的调用算不算使用其它空间。

public class palindrome {
	
	//using with extra space
	public static boolean check(int x){
		String temp = Integer.toString(x);
		boolean flag = true;
		for(int i=0;i<temp.length()/2;i++){
			char a = temp.charAt(i);
			char b = temp.charAt(temp.length()-i-1);
			if(a!=b){
				flag = false;
			}
		}
		return flag;
	}
	
	//using without extra space
	public static boolean check2(int x){
		if(x<0)
			return false;
		int n=1;
		int temp = x;
		while(temp/10!=0){
			temp=temp/10;
			n++;
		}
		for(int i=0;i<n/2;i++){
			int a = i;
			int b = n-1-i;
			if(getInt(x,a)!=getInt(x,b)){
				return false;
			}
		}
		return true;
	}
	// 比方 896698 这个数字。要获取百位,用896698除以100。得到8966然后取余10得到6。即为百位的数值
	private static int getInt(int x,int i){
		int a =  (int) Math.pow(10, i);
		return (x/a)%10;
	}
}





本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5154517.html,如需转载请自行联系原作者

相关文章
|
10月前
|
SQL
Parameter ‘id‘ not found. Available parameters are [collection, list]
Parameter ‘id‘ not found. Available parameters are [collection, list]
137 0
Optional int parameter ‘id‘ is present but cannot be translated into a null value due to being ……
Optional int parameter ‘id‘ is present but cannot be translated into a null value due to being ……
186 0
Parameter ‘bookID‘ not found. Available parameters are [param1, bookId]
Parameter ‘bookID‘ not found. Available parameters are [param1, bookId]
107 0
|
Windows
Illegal character in opaque part at index
Illegal character in opaque part at index
LeetCode 301. Remove Invalid Parentheses
删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果。 说明: 输入可能包含了除 ( 和 ) 以外的字符。
50 0
LeetCode 301. Remove Invalid Parentheses
成功解决ValueError: Number of passed names did not match number of header fields in the file
成功解决ValueError: Number of passed names did not match number of header fields in the file
成功解决DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change
成功解决DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change
成功解决TypeError: Value passed to parameter 'paddings' has DataType float32 not in list of allowed valu
成功解决TypeError: Value passed to parameter 'paddings' has DataType float32 not in list of allowed valu
成功解决ValueError: Parameter values for parameter (n_estimators) need to be a sequence.
成功解决ValueError: Parameter values for parameter (n_estimators) need to be a sequence.
成功解决ValueError: Parameter values for parameter (n_estimators) need to be a sequence.