相关疑难解决方法(0)

C#集合集?

有谁知道Set在C#中是否有与Java 集合相当的好处?我知道你可以使用a Dictionary或者HashTable通过填充而忽略值来模仿一个集合,但这不是一个非常优雅的方式.

.net c# collections set

463
推荐指数
7
解决办法
35万
查看次数

为什么SortedSet <T> .GetViewBetween不是O(log N)?

在.NET 4.0+中,类SortedSet<T>有一个名为的方法GetViewBetween(l, r),它返回树部件上的接口视图,其中包含指定的两个之间的所有值.鉴于它SortedSet<T>是作为红黑树实现的,我自然希望它能够及时运行O(log N).C++中的类似方法是std::set::lower_bound/upper_boundJava TreeSet.headSet/tailSet,它们是对数的.

然而,事实并非如此.以下代码在32秒内运行,而等效O(log N)版本GetViewBetween将使该代码在1-2秒内运行.

var s = new SortedSet<int>();
int n = 100000;
var rand = new Random(1000000007);
int sum = 0;
for (int i = 0; i < n; ++i) {
    s.Add(rand.Next());
    if (rand.Next() % 2 == 0) {
        int l = rand.Next(int.MaxValue / 2 - 10);
        int r = l + rand.Next(int.MaxValue / 2 - 10);
        var t = s.GetViewBetween(l, …
Run Code Online (Sandbox Code Playgroud)

.net c# complexity-theory sortedset

65
推荐指数
2
解决办法
4401
查看次数

推荐排序集合以左右搜索最接近的值

.NET 3.5中是否有现成的数据结构来执行以下操作

存储按十进制键排序的值,允许使用dublicates

得到与给定键左右最接近的下一个值(枚举数)

一个例子:

汽车经销商有汽车,客户要求找到最贵的车但价格低于1000美元

.net c# collections data-structures

0
推荐指数
1
解决办法
568
查看次数