降低这些函数的时间和空间复杂度
在这里,我从列表中生成一个随机数。
/////somecode///////
randNum = new Random();
Dictionary = new Dictionary<int, double>();
List1 = new List<double>();
/////somecode///////
public int func1()
{
double rand = randNum.NextDouble();
int index = 0;
foreach (var item in Dictionary)
{
if (randomNum <= List1[index])
{
return item.Key;
}
index++;
}
return -1 ;
}
在这里,我给出一个pairList作为参数,将其键和值添加到Dictionary1中。
public void func2(List<KeyValuePair<string, int>> pairList)
{
foreach (var item in pairList)
{
if (Dictionary1.TryGetValue(pair.Key, out double value))
{
Dictionary1.Remove(item.Key);
sum -= value;
SumList.Remove(sum);
}
Dictionary1.Add(new KeyValuePair<string, int>(item.Key, item.Value));
Sum += item.Value;
SumList.Add(_cdfSum);
}
}
如何降低这些函数的时间和空间复杂度?
Here I generate a random number from a list.
/////somecode///////
randNum = new Random();
Dictionary = new Dictionary<int, double>();
List1 = new List<double>();
/////somecode///////
public int func1()
{
double rand = randNum.NextDouble();
int index = 0;
foreach (var item in Dictionary)
{
if (randomNum <= List1[index])
{
return item.Key;
}
index++;
}
return -1 ;
}
And here I give a pairList as a parameter to add it's keys and values to the Dictionary1.
public void func2(List<KeyValuePair<string, int>> pairList)
{
foreach (var item in pairList)
{
if (Dictionary1.TryGetValue(pair.Key, out double value))
{
Dictionary1.Remove(item.Key);
sum -= value;
SumList.Remove(sum);
}
Dictionary1.Add(new KeyValuePair<string, int>(item.Key, item.Value));
Sum += item.Value;
SumList.Add(_cdfSum);
}
}
How can I reduce the time and space complexity of these functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论