我在面试中遇到了这个问题:
我得到了一个字符串数组 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"
}
}
UYOU
BIG阳
相关分类