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,如需转载请自行联系原作者

相关文章
|
SQL
Parameter ‘id‘ not found. Available parameters are [collection, list]
Parameter ‘id‘ not found. Available parameters are [collection, list]
242 0
|
JSON 数据格式
遇到【Unexpected character (‘“‘ (code 34)): was expecting comma to separate Object entries】的解决办法
遇到【Unexpected character (‘“‘ (code 34)): was expecting comma to separate Object entries】的解决办法
遇到【Unexpected character (‘“‘ (code 34)): was expecting comma to separate Object entries】的解决办法
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 ……
323 0
LeetCode 301. Remove Invalid Parentheses
删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果。 说明: 输入可能包含了除 ( 和 ) 以外的字符。
73 0
LeetCode 301. Remove Invalid Parentheses
|
Windows
Illegal character in opaque part at index
Illegal character in opaque part at index
成功解决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
|
算法
On the Correct and Complete Enumeration of the Core Search Space
在之前的文章中我们讨论了基于graph的DP-based算法,来解决join ordering的枚举问题。 这些DP算法通过join predicate描述的连通性,解决了枚举可能的表组合问题,但join graph本身(即使hypergraph)是无法完整的描述join语义的,因为连通边本身无法描述不同类型的join语义,例如left outer join/semi join/anti join...,因此即使找到了所谓的csg-cmp-pair,也不一定是有效的plan。 这篇paper讨论的就是这个问题,当枚举出一个csg-cmp-pair (S1 o S2),如何判断这是有效的join
460 0
On the Correct and Complete Enumeration of the Core Search Space
成功解决lightgbm.basic.LightGBMError: Parameter max_depth should be of type int, got “0.02“
成功解决lightgbm.basic.LightGBMError: Parameter max_depth should be of type int, got “0.02“
成功解决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