連續最長的數字串(好未來2017秋招筆試)

題目:

讀入一個字符串 str,輸出字符串 str 中的連續最長的數字串


輸入描述:

測試輸入包含 1 個測試用例,一個字符串 str,長度不超過 255。

輸出描述:

在一行內輸出 str 中裏連續最長的數字串。

輸入例子:

abcd12345ed125ss123456789

輸出例子:

123456789


java版本的代碼實現:

package cn.cat.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

	/** 連續最長的數字串(好未來2017秋招筆試)
	 * @Description: 
	 * @author gwj
	 * @Created 2017年9月20日 下午2:46:50 
	 * @param args
	 */
	public static void main(String[] args) {
		String str = "abcd12345ed125ss123456789";
		//使用正則來剝離出數值
		Pattern compile = Pattern.compile("\\d+");
		Matcher matcher = compile.matcher(str);
		String maxNumStr = "";
		while (matcher.find()) {
			if (matcher.group().length() > maxNumStr.length()) {
				maxNumStr = matcher.group();
			}
		}
		System.out.println(maxNumStr);
	}

}


發佈了48 篇原創文章 · 獲贊 10 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章