輸出連續最長的數字串 Java實現

題目描述:輸出字符串中連續最長數字串,條件如果兩個串相同大小輸出後者

考察點:

1、string判斷是否是數字的方法(ASCII

2、用兩個字符串緩存最大值,和長串覆蓋短串

3、考察幾個判定條件(max置空的時機)

4、(魯棒性高)特殊情況:結尾就是數字,或結尾有一個字母,結尾有多個字母的情況都要保證算法可行!

class Untitled {
	public static void main(String[] args) {
		String str1="abc0128cdf8967";
		System.out.println(isNumeric(str1));
	}
	
	public static String isNumeric(String str){
		String result = ""; 
		String max = "";
		for(int i=0; i<str.length(); i++){
//  string判斷是否是數字的方法(ASCII)
		int chr=str.charAt(i);
		if(chr > 47 && chr < 58){
		   max = max + str.charAt(i);		
//  結尾是數字不走else的情況
		   if(i==str.length()-1){
		      result = max;
		   }
		   System.out.println("max"+max);
		}else{		
		  if(max.length() < result.length()){
		     max = "";
		  }else{
		      result = max;
			  max = "";
			  System.out.println("result"+result);
		  }
		}
	  
	}
	return result;
}
	
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章