我目前在循环中使用 SortedList 来按降序对一些值进行排序:
for(...)
{
float rawValue;
float offset;
sortedList.Add(rawValue + offset, index);
}
如果我们按原始值对条目进行排序而不使用偏移量?
显而易见的解决方案是在同一个循环中填充另一个 sortedRawValuesList,但我认为有更快、更高效的内存实现方法吗?
谢谢!
最佳答案
您能否在迭代时简单地跟踪最高的 rawValue?如果偏移量在每次迭代中都发生变化,您可能还想保存偏移量。
float highestRawVal = float.MinVal;
float offset_ForHighestRawVal = float.MinVal;
for(...)
{
float rawValue;
float offset;
sortedList.Add(rawValue + offset, index);
if(highestRawVal < rawVal)
{
highestRawVal = rawValue;
offset_ForHighestRawVal = offset;
}
}
if (highestRawVal + offset_ForHighestRawVal == sortedList[0])
Console.WriteLine("They Match");
然后您可以简单地检查它们是否匹配。
关于c# - 确定最大值的有效方法(有或没有偏移),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13165625/