【发布时间】:2022-01-03 10:05:40
【问题描述】:
我正在做一个 codewars kata,它正在工作,但我正在超时。 我在网上搜索了解决方案,以获取某种参考,但它们都是针对 java 脚本的。
这是卡塔:https://i.stack.imgur.com/yGLmw.png
这是我的代码:
public static int DblLinear(int n)
{
if(n > 0)
{
var list = new List<int>();
int[] next_two = new int[2];
list.Add(1);
for (int i = 0; i < n; i++)
{
for (int m = 0; m < next_two.Length; m++)
{
next_two[m] = ((m + 2) * list[i]) + 1;
}
if(list.Contains(next_two[0]))
{
list.Add(next_two[1]);
}
else if(list.Contains(next_two[1]))
{
list.Add(next_two[0]);
}
else
list.AddRange(next_two);
list.Sort();
}
return list[n];
}
return 1;
}
这是一个非常缓慢的解决方案,但这似乎对我有用。
【问题讨论】:
-
首先阅读您的代码并确定复杂性,例如O(1), O(logn), O(n!), ...,然后尝试在分析器下运行以查看“热点”(搜索 .NET 代码分析器。