【问题标题】:Find max value in each of the index of a collection of integers in c# [closed]在c#中的整数集合的每个索引中查找最大值[关闭]
【发布时间】:2021-10-05 17:08:02
【问题描述】:

我有一个List<List<int>>。 所有列表的大小都相同。我需要在列表的每个索引中找到最大值。

即,如果我有 100 个大小为 3 的列表。我需要在 100 个项目中的索引 0,1 和 2 处找到最大值。

Max value out of list1[0], list2[0], list3[0],......list100[0]
Max value out of list1[1], list2[1], list3[1],......list100[1]
Max value out of list1[2], list2[2], list3[2],......list100[2]

需要一个接受List<List<int>> 并返回每个索引最大值的列表的函数。如下所示

public List<int> FindMaxValueByIndex(List<List<int>> items)
{
}

性能是一个需要考虑的关键因素。

【问题讨论】:

    标签: c# performance loops


    【解决方案1】:

    忽略数据验证并保证内部列表将始终具有相同数量的元素:

    public List<int> FindMaxValueByIndex(List<List<int>> items)
    {
        var maxListSize = items[0].Count;
    
        var maxValues = new List<int>(items.Count);
    
        for (var index = 0; index < maxListSize; index++)
            maxValues.Add(items.Select(x => x[index]).Max());
    
        return maxValues;
    }
    

    输入:

    var data = new List<List<int>>
    {
        new List<int> { 5, 10, 300, 1 },
        new List<int> { 3, 24, 2, 56 },
    };
    

    输出:

    {5, 24, 300, 56}
    

    【讨论】:

      【解决方案2】:

      我会推荐使用 LINQ 方法的这种方法 :)

      private static int[] FindMaxByIndex(List<List<int>> lists)
      {
          var listLength = lists
              .Select(x => x.Count)
              .Distinct()
              // If all lists are of equal length
              // this should contain single element.
              // Otherwise this would throw exception - 
              // this is validation.
              .Single();
      
          var maxesByIndex = new int[listLength];
      
          for (int i = 0; i < listLength; i++)
          {
              maxesByIndex[i] = lists
                  .Select(x => x[i])
                  .Max();
          }
      
          return maxesByIndex;
      }
      

      【讨论】:

      • 答案要求返回值List&lt;int&gt; :)
      【解决方案3】:

      我了解问题指出列表的大小始终相同,但是,由于使用了通用列表的通用列表并且通用列表的通用列表不强制执行该要求,因此此代码采用了安全的方法确保不访问越界索引。

      public List<int> FindMaxValueByIndex(List<List<int>> items)
              {
                  List<int> ret = new List<int>();
                  var MaxListSize = items.Select(l => l.Count).Max();
                  for(int i = 0; i < MaxListSize; i ++)
                  {
                      ret.Add(items.Where(l => l.Count > i).Select(l => l[i]).Max());
                  }
                  
                  return ret;
              }
      

      Fiddle

      【讨论】:

      • 除非我遗漏了什么,否则您的代码会在循环的每次迭代中计算表达式 items.Select(l =&gt; l.Count).Max()
      • @TheodorZoulias,我想这可能是一个小的性能损失,可以通过变量来纠正。
      • 诸如“列表本身性能不佳”之类的粗体语句。并且“建议使用数组而不是列表”需要一些澄清并链接支持这些语句。 (并且“所有列表的大小都相同。”意味着无需检查每个元素的大小是否正确)
      • @AlexeiLevenkov,这个答案没有(也从来没有)提到列表是/不是高性能的。你指的是一个不同的答案,当我意识到我完全不在基地时,我删除了这个答案。我对数组的建议是基于一个错误的假设,即我最初认为列表总是有 3 个整数。我忘了删除它 - 我现在已经这样做了。我现在还解释了为什么我检查列表的大小,即使问题表明它们总是相同的大小。
      猜你喜欢
      • 2022-06-15
      • 1970-01-01
      • 2022-07-05
      • 2016-03-11
      • 2020-02-27
      • 1970-01-01
      • 2015-01-22
      • 1970-01-01
      • 2020-10-14
      相关资源
      最近更新 更多