相关疑难解决方法(0)

查找字符串的公共前缀

我有4个字符串:

"h:/a/b/c"
"h:/a/b/d"
"h:/a/b/e"
"h:/a/c"
Run Code Online (Sandbox Code Playgroud)

我想找到这些字符串的公共前缀,即"h:/a".怎么找到?

通常我会用分隔符拆分字符串'/'并将其放在另一个列表中,依此类推.
有没有更好的方法呢?

c# string pattern-matching

26
推荐指数
5
解决办法
2万
查看次数

如何"压缩"或"旋转"可变数量的列表?

如果我有一个包含任意数量列表的列表,如下所示:

var myList = new List<List<string>>()
{
    new List<string>() { "a", "b", "c", "d" },
    new List<string>() { "1", "2", "3", "4" },
    new List<string>() { "w", "x", "y", "z" },
    // ...etc...
};
Run Code Online (Sandbox Code Playgroud)

...有没有办法以某种方式将列表"压缩"或"旋转"成这样的东西?

{ 
    { "a", "1", "w", ... },
    { "b", "2", "x", ... },
    { "c", "3", "y", ... },
    { "d", "4", "z", ... }
}
Run Code Online (Sandbox Code Playgroud)

显而易见的解决方案是做这样的事情:

public static IEnumerable<IEnumerable<T>> Rotate<T>(this IEnumerable<IEnumerable<T>> list)
{
    for (int i = 0; i < list.Min(x => x.Count()); i++) …
Run Code Online (Sandbox Code Playgroud)

c# linq algorithm

13
推荐指数
3
解决办法
1145
查看次数

标签 统计

c# ×2

algorithm ×1

linq ×1

pattern-matching ×1

string ×1