猿问

查找字符串中有效子字符串的长度。C#

我正在尝试找出最长有效子字符串的长度。有效的子字符串是包含至少 1 个大写字母且没有数字的子字符串。我的代码不起作用有人可以帮忙谢谢。


class Program


{


    public static void Main(string[] args)

    {

        string S = "a02caa3ThisIsValid1bC2a";


        Console.WriteLine("The longest valid substring is {0}", solution(S));


        Console.ReadKey();

    }


    public static int solution(string S)

    {

        char[] stringarray = S.ToCharArray();

        int slength = S.Length;

        int result = 0;

     //   string resultstring = "";


        for (int i = 0; i < slength; i++)

        {

            char Z = stringarray[i];


            if(char.IsUpper(Z) || char.IsLower(Z) || !char.IsDigit(Z))

            {

                while (char.IsUpper(Z) || char.IsLower(Z) && !char.IsDigit(Z))

                {

                    result += 1;

                 //   resultstring = result.ToString();

                }

            }

        }         

        return result;

    }


}


桃花长相依
浏览 198回答 3
3回答

ibeautiful

这对我有用:public static int solution(string S){&nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; S&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Split("1234567890".ToCharArray()) // split on invalid characters&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Where(x => x.Any(y => char.IsUpper(y))) // keep only those portions containing an uppercase char&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(x => x.Length) // get the length of each string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Max(); // find the longest}这是基于问题中的代码的解决方案:public static int solution(string S){&nbsp; &nbsp; int result = 0;&nbsp; &nbsp; int tally = 0;&nbsp; &nbsp; bool foundUpper = false;&nbsp; &nbsp; for (int i = 0; i < S.Length; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; char Z = S[i];&nbsp; &nbsp; &nbsp; &nbsp; if (char.IsDigit(Z))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (foundUpper && tally > result)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = tally;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tally = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foundUpper = false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tally++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foundUpper |= char.IsUpper(Z);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (foundUpper && tally > result)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; result = tally;&nbsp; &nbsp; }&nbsp; &nbsp; return result;}还有第三种选择:public static int solution3(string S){&nbsp; &nbsp; return S.ToCharArray().Concat(new [] { '0' }).Aggregate(&nbsp; &nbsp; &nbsp; &nbsp; new { result = 0, tally = 0, foundUpper = false },&nbsp; &nbsp; &nbsp; &nbsp; (a, x) => char.IsDigit(x)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? new { result = (a.foundUpper && a.tally > a.result) ? a.tally : a.result, tally = 0, foundUpper = false }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : new { result = a.result, tally = a.tally + 1, foundUpper = a.foundUpper || char.IsUpper(x) })&nbsp; &nbsp; &nbsp; &nbsp; .result;}

LEATH

代码和你写的完全不同。您正在寻找非数字的子字符串,然后在这些子字符串的末尾,您必须检查子字符串的至少一个字符是否为大写。如果是,那么这个子串可能是一个候选,然后必须考虑它的长度。检查是否存在大写字符是通过upperCaseFound布尔变量完成的。public static int LongestSubstring(string s){&nbsp; &nbsp; int maxLength = 0;&nbsp; &nbsp; bool upperCaseFound = false;&nbsp; &nbsp; int length = 0;&nbsp; &nbsp; foreach (char ch in s)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (char.IsDigit(ch))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (upperCaseFound && length > maxLength)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxLength = length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; upperCaseFound = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; length = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (char.IsUpper(ch))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; upperCaseFound = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; length++;&nbsp; &nbsp; }&nbsp; &nbsp; if (upperCaseFound && length > maxLength)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; maxLength = length;&nbsp; &nbsp; }&nbsp; &nbsp; return maxLength;}

慕码人8056858

&nbsp; &nbsp; public static int solution(string s)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int result = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < s.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bool containsUpper = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Char.IsLetter(s[i]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int len = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Char.IsUpper(s[i])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; containsUpper = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; len++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } while (i<s.Length&&Char.IsLetter(s[i])) ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( (len > result )&&containsUpper)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = len;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答