zoukankan      html  css  js  c++  java
  • 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;
    	}
    }
    



  • 相关阅读:
    PL/SQL 中查询CLOB字段内容
    ubuntu14.04 swap not avalible交换分区不能使用
    ubuntu14.04安装ia32-lib
    rtems资料分享
    NIR相机
    rsync详解
    SublimeText3使用技巧总结
    msm8610 lcd driver code analysis
    Qualcomm Android display架构分析
    LCD framebuffer驱动设计文档
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5154517.html
Copyright © 2011-2022 走看看