第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

C#
桃花長相依 2021-11-21 16:09:55
我正在嘗試找出最長有效子字符串的長度。有效的子字符串是包含至少 1 個大寫字母且沒有數(shù)字的子字符串。我的代碼不起作用有人可以幫忙謝謝。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;    }}
查看完整描述

3 回答

?
ibeautiful

TA貢獻1993條經(jīng)驗 獲得超6個贊

這對我有用:


public static int solution(string S)

{

    return

        S

            .Split("1234567890".ToCharArray()) // split on invalid characters

            .Where(x => x.Any(y => char.IsUpper(y))) // keep only those portions containing an uppercase char

            .Select(x => x.Length) // get the length of each string

            .Max(); // find the longest

}

這是基于問題中的代碼的解決方案:


public static int solution(string S)

{

    int result = 0;

    int tally = 0;

    bool foundUpper = false;


    for (int i = 0; i < S.Length; i++)

    {

        char Z = S[i];


        if (char.IsDigit(Z))

        {

            if (foundUpper && tally > result)

            {

                result = tally;

            }

            tally = 0;

            foundUpper = false;

        }

        else

        {

            tally++;

            foundUpper |= char.IsUpper(Z);

        }

    }

    if (foundUpper && tally > result)

    {

        result = tally;

    }

    return result;

}

還有第三種選擇:


public static int solution3(string S)

{

    return S.ToCharArray().Concat(new [] { '0' }).Aggregate(

        new { result = 0, tally = 0, foundUpper = false },

        (a, x) => char.IsDigit(x)

            ? new { result = (a.foundUpper && a.tally > a.result) ? a.tally : a.result, tally = 0, foundUpper = false }

            : new { result = a.result, tally = a.tally + 1, foundUpper = a.foundUpper || char.IsUpper(x) })

        .result;

}


查看完整回答
反對 回復(fù) 2021-11-21
?
LEATH

TA貢獻1936條經(jīng)驗 獲得超7個贊

代碼和你寫的完全不同。您正在尋找非數(shù)字的子字符串,然后在這些子字符串的末尾,您必須檢查子字符串的至少一個字符是否為大寫。如果是,那么這個子串可能是一個候選,然后必須考慮它的長度。檢查是否存在大寫字符是通過upperCaseFound布爾變量完成的。


public static int LongestSubstring(string s)

{

    int maxLength = 0;


    bool upperCaseFound = false;

    int length = 0;


    foreach (char ch in s)

    {

        if (char.IsDigit(ch))

        {

            if (upperCaseFound && length > maxLength)

            {

                maxLength = length;

            }


            upperCaseFound = false;

            length = 0;

            continue;

        }


        if (char.IsUpper(ch))

        {

            upperCaseFound = true;

        }


        length++;

    }


    if (upperCaseFound && length > maxLength)

    {

        maxLength = length;

    }


    return maxLength;

}


查看完整回答
反對 回復(fù) 2021-11-21
?
慕碼人8056858

TA貢獻1803條經(jīng)驗 獲得超6個贊

    public static int solution(string s)

    {

        int result = 0;

        for (int i = 0; i < s.Length; i++)

        {

            bool containsUpper = false;

            if (Char.IsLetter(s[i]))

            {

                int len = 0;

                do 

                {

                    if (Char.IsUpper(s[i])){

                        containsUpper = true;

                    }

                    i++;

                    len++;



                } while (i<s.Length&&Char.IsLetter(s[i])) ;


                if( (len > result )&&containsUpper)

                    result = len;

            }


        }


        return result;

    }


查看完整回答
反對 回復(fù) 2021-11-21
  • 3 回答
  • 0 關(guān)注
  • 228 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號