题解 | #在字符串中找出连续最长的数字串#

在字符串中找出连续最长的数字串

http://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec

//简单明了,运行时间12ms
import java.util.*;
import java.io.*;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String s=null;
        while((s=br.readLine())!=null){
            char[] chars=s.toCharArray();
            int count=0;
            int max=0;
            StringBuilder sb=new StringBuilder();
            for(int i=0;i<chars.length;i++){
                if(chars[i]>='0'&&chars[i]<='9'){
                    count++;
                    if(max<=count) {
                        if(max<count) sb.delete(0,sb.length());
                        max=count;
                    }
                }else {
                    if(max==count)
                    for(int j=i-max;j<i;j++){
                            sb.append(chars[j]);
                        }
                    count=0;
                }
            }
            if(max==count){
                for(int j=chars.length-max;j<chars.length;j++){
                    sb.append(chars[j]);
                }
            }
            System.out.println(sb+","+max);
        }
    }
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务