题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
class Program
{
static void Main(string[] args)
{
string line1;
var inputList = new List<string>();
while (!string.IsNullOrWhiteSpace(line1 = Console.ReadLine()))
{
inputList.Add(line1);
}
foreach (var item in inputList)
{
var newStr = Regex.Replace(item, $"[^\\d]", "#").Replace("##", "#");
var li = newStr.Split("#");
var all_item = li.GroupBy(a=>a.Length).OrderByDescending(a=>a.Key).ToList();
var res_item = li.Where(a => a.Length == all_item[0].Key).ToList();
Console.WriteLine(string.Join("", res_item)+","+res_item[0].Length);
}
}
}
查看5道真题和解析
