自己写的。用一个数组存储最长子串的开始坐标
在字符串中找出连续最长的数字串
https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str=in.nextLine();
            int max_len=0;
            int temp_len=0;
            int temp_start=0;
            int[] starts=new int[str.length()];  //最长数字子串们的开始坐标
            for(int i=0;i<str.length();i++){
                if(str.charAt(i)>='0' && str.charAt(i)<='9' && i!=str.length()-1){
                    if(temp_len==0){  //数字子串的起点
                        temp_start=i;
                    }
                    temp_len++;
                }else{
                    if(str.charAt(i)>='0' && str.charAt(i)<='9' && i==str.length()-1){
                        temp_len++;   //当字符串最后一位是数字时
                    }
                    if(temp_len>max_len){  
                        max_len=temp_len;
                        starts[0]=temp_start;  //有新的最大长度时,当前只有一个最长子串,
                        for(int j=1;j<starts.length;j++){
                            starts[j]=0;
                        }
                    }else if(temp_len==max_len){
                        for(int j=1;j<starts.length;j++){ //有相同最大长度的子串时,添加该子串的起始坐标
                            if(starts[j]==0){
                                starts[j]=temp_start;
                                break;
                            }
                        }
                    }
                    temp_len=0;
                }
            }
            String res="";
            res+=str.substring(starts[0],starts[0]+max_len);  //截取最长子串
            for(int i=1;i<starts.length;i++){
                if(starts[i]!=0){
                    res+=str.substring(starts[i],starts[i]+max_len); //拼接最长子串们
                }
            }
            System.out.println(res+","+max_len);
        }
    }
}
 深信服公司福利 738人发布
深信服公司福利 738人发布 投递百度等公司10个岗位
投递百度等公司10个岗位
