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

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

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

using System;
using System.Collections.Generic;
using System.Text;

namespace HJ92
{
    internal class Program
    {
        static void Main(string[] args)
        {

            List<string> list = new List<string>();

            while (true)
            {
                var inputStr = Console.ReadLine();
                if (string.IsNullOrEmpty(inputStr))
                {
                    break;
                }
                list.Add(GetMaxNumberCount(inputStr));
            }

            foreach (var item in list)
            {
                Console.WriteLine(item);
            }

        }

        private static string GetMaxNumberCount(string s)
        {
            int max = 0;

            List<string> list = new List<string>();

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < s.Length; i++)
            {
                if (char.IsDigit(s[i]))
                {
                    sb.Append(s[i]);
                    continue;
                }
                if (sb.Length > 0)
                {
                    if (sb.Length > max)
                    {
                        max = sb.Length;
                    }
                    list.Add(sb.ToString());
                    sb.Clear();
                }
            }

            if (sb.Length > 0)
            {
                if (sb.Length > max)
                {
                    max = sb.Length;
                }
                list.Add(sb.ToString());
                sb.Clear();
            }

            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Length == max)
                {
                    sb.Append(list[i]);
                }
                if (i == list.Count - 1)
                {
                    sb.Append(',');
                    sb.Append(max);
                }
            }
            return sb.ToString();
        }
    }
}

全部评论

相关推荐

头像 会员标识
今天 14:38
浙江大学 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
11-06 16:50
门头沟学院 Java
用微笑面对困难:word打字比赛二等奖的我,也要来凑合凑合
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务