• 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;
    	}
    }
    



  • 相关阅读:
    HDFS客户端的权限错误:Permission denied
    使用JAVA API获取hadoop集群的FileSystem
    Hessian 接口使用示例总结(转载)
    Linux NFS服务器的安装与配置
    ContextLoaderListener可以不写嘛?
    SpringMvc @ResponseBody字符串中文乱码原因及解决方案
    Spring @RequestAttribute
    Spring @Pathvariable
    Spring @Valid
    Spring @ModelAttribute
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5154517.html
Copyright © 2020-2023  润新知