返回第一个最长的字符串,由c#中数组中取的n个连续字符串组成

我在面试中遇到了这个问题:


我得到了一个字符串数组 strarr 和一个整数 k。我的任务是返回由数组中的 k 个连续字符串组成的第一个最长字符串。


规则:n 为字符串数组的长度,如果 n = 0 或 k > n 或 k <= 0 返回“”。


例子:

LongestConsec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 2) 

输出应该是“abigailtheta”


下面是我试过的代码..它不工作。


public class LongestConsecutives

{

    public static String LongestConsec(string[] strarr, int k)

    {

        string final = String.Join("", strarr.OrderByDescending(s=>s.Length).Take(k)); 

        return final;

    }


    public static void Main()

    {

        string s1 = LongestConsec(new String[] { "wlwsasphmxx", "owiaxujylentrklctozmymu", "wpgozvxxiu" }, 2); //op="wlwsasphmxxowiaxujylentrklctozmymu"

        string s2 = LongestConsec(new string[] { "itvayloxrp", "wkppqsztdkmvcuwvereiupccauycnjutlv", "vweqilsfytihvrzlaodfixoyxvyuyvgpck" }, 2);//op="wkppqsztdkmvcuwvereiupccauycnjutlvvweqilsfytihvrzlaodfixoyxvyuyvgpck"

        string s3 = LongestConsec(new String[] { "zone", "abigail", "theta", "form", "libe", "zas" }, -2);//op=""

        string s4 = LongestConsec(new String[] { "it", "wkppv", "ixoyx", "3452", "zzzzzzzzzzzz" }, 3);//op="ixoyx3452zzzzzzzzzzzz"

        string s5 = LongestConsec(new String[] { "it", "wkppv", "ixoyx", "3452", "zzzzzzzzzzzz" }, 15);//op=""

        string s6 = LongestConsec(new String[] { "it", "wkppv", "ixoyx", "3452", "zzzzzzzzzzzz" }, 0);//op=""

        string s7 = LongestConsec(new String[] { }, 3);//op=""

        string s8 = LongestConsec(new String[] { "zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail" }, 2);//op="abigailtheta"

        string s9 = LongestConsec(new String[] { "ejjjjmmtthh", "zxxuueeg", "aanlljrrrxx", "dqqqaaabbb", "oocccffuucccjjjkkkjyyyeehh" }, 1);//op="oocccffuucccjjjkkkjyyyeehh"

    }

}


红颜莎娜
浏览 164回答 2
2回答

UYOU

您.Distinct()在 LINQ 中缺少删除重复项的调用。public static String LongestConsec(string[] strarr, int k){&nbsp; &nbsp; string final = String.Join("",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; strarr.Distinct()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Remove Duplicates&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .OrderByDescending(s => s.Length) // Order by Length&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Take(k)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Take from List&nbsp; &nbsp; );&nbsp; &nbsp; return final;}&nbsp;在此修改后,您将获得预期的输出:string target = LongestConsec(new string[] { "zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"}, 2);// target = "abigailtheta"但是您仍然缺少IF完成您描述的规则的案例:如果 n = 0 或 k > n 或 k <= 0 返回“”更新正如 Vladimir Pavelka 在评论中指出的那样,您只需要一种方法来存储词序的初始索引。你可以用这样的Dictionary东西来做到这一点:public static String LongestConsec(string[] strarr, int k){&nbsp; &nbsp; var dict = new Dictionary<string, int>();&nbsp; &nbsp; for (int i = 0; i < strarr.Length; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (!dict.ContainsKey(strarr[i]))&nbsp; // Preventing duplicates&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dict.Add(strarr[i], i + 1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; string final = String.Join("",&nbsp; &nbsp; &nbsp; &nbsp; dict.OrderByDescending(s => s.Key.Length)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Take(k)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .OrderBy(s => s.Value)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(s => s.Key)&nbsp; &nbsp; );&nbsp; &nbsp; return final;}

BIG阳

与 Smartis 讨论后:public static String LongestConsec(string[] strarr, int k) =>&nbsp; &nbsp; String.Join("",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; strarr.Distinct() // note: this won't change order&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select((s, idx) => (s, idx)) // store initial index&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .OrderByDescending(x => x.s.Length) // longest words first&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Take(k)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .OrderBy(x => x.idx) // restore original order&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(x => x.s));
打开App,查看更多内容
随时随地看视频慕课网APP