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


读入一个字符串str,输出字符串str中的连续最长的数字串

输入描述:

个测试输入包含1个测试用例,一个字符串str,长度不超过255。


 

输出描述:

在一行内输出str中里连续最长的数字串。

示例1

输入

abcd12345ed125ss123456789

输出

123456789

方法一:

import java.util.Scanner;


public class Test0 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        String pai=sc.nextLine();
        
        int[] jilu={-1,-1,0};
        int begin=-1,last=-1,count=0;
        if(pai.charAt(0)>=30&&pai.charAt(0)<=39){
            begin=0;
            count=1;
        }
        for(int i=1;i<pai.length();i++){
            char c0=pai.charAt(i-1);
            
            char c1=pai.charAt(i);
            if((c0>=48&&c0<=57)&&(c1>=48&&c1<=57)){
                
                count++;
                
                if(i==pai.length()-1){
                    last=i+1;
                    if(jilu[2]==0||count>=jilu[2]){
                        jilu[0]=begin;
                        jilu[1]=last;
                        jilu[2]=count;}    
                }
            }
            else if((c0>=48&&c0<=57)){
                last=i;
                
                if(jilu[2]==0||count>=jilu[2]){
                jilu[0]=begin;
                jilu[1]=last;
                jilu[2]=count;}
            }
            else if((c1>=48&&c1<=57)){
                begin=i;
                count=1;
                if(i==pai.length()-1){
                    last=i+1;
                    if(jilu[2]==0||count>=jilu[2]){
                        jilu[0]=begin;
                        jilu[1]=last;
                        jilu[2]=count;}    
                }
            }
            else ;
        }
        String str;
        if(jilu[2]>0)
         str=pai.substring(jilu[0],jilu[1]);
        else str="";
        System.out.println(str);
    }

}
 

方法 二:


import java.util.Scanner;
public class Test1{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String[] arr = s.split("[\\D]+");
String result="";
int max = 0;
for(int i=0;i<arr.length;i++){
if(arr[i].length()>max){
max=arr[i].length();
result = arr[i];
}
}
System.out.println(result);
}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值