输出连续最长的数字串 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;
}
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值