大家!如何在C#中的特定范围内获得INT数组的最小值?例如:int [] array = new int {1,2,3,4,5,6,7,8,76,45};我想获得最小值在第三元到第8个元素之间。也许可以通过LINQ查询获得?

有帮助吗?

解决方案

array.Skip(2).Take(5).Min();

其他提示

我认为我不妨将我的tuppence添加到此。当杰森(Jason)反对这样一个事实,即我们说我们跳过了多少而不是结束索引,我们可以添加一个简单的扩展方法:

public static IEnumerable<T> WithIndexBetween<T>(this IEnumerable<T> source,
    int startInclusive, int endExclusive)
{
    // The two values can be the same, yielding no results... but they must
    // indicate a reasonable range
    if (endExclusive < startInclusive)
    {
        throw new ArgumentOutOfRangeException("endExclusive");
    }
    return source.Skip(startInclusive).Take(endExclusive - startInclusive);
}

然后:

int min = array.WithIndexBetween(2, 7).Min();

调整扩展方法名称的口味。 (命名很难,我不会花很长时间在这里提出一个好人:)

int min = array.Where((value, index) => index >= 2 && index <= 7).Min(); 

编辑

实际上,上面的方法效率很低,因为它列举了整个顺序,即使我们对索引高于7的项目不感兴趣。更好的解决方案是使用 TakeWhile:

int min = array.TakeWhile((value, index) => index <= 7).Skip(2).Min();

不幸的是,它不是很可读性...使其更好的选择是创建一种自定义扩展方法,如乔恩(Jon)的答案所示。

int[] arr = {0,1,2,3,4,5,6,7,8};
int start = 3;
int end = 8;
int min = arr.Skip(start - 1).Take(end - start).Min();

只是添加另一个选项:

int start = 3;
int end = 8;
var min = Enumerable.Range(start - 1,end - start).Select(idx => array[idx]).Min();

阿法克(Afaik),如果您必须将射程接近一个末端,并且您的数组确实很长,那么这在理论上会更快。

那是因为(再次afaik) Skip() 没有考虑到一个数组(即可以在O(1)中随机访问)并列举它。

array.Skip(3).Take(4).Min();

就个人而言,我更喜欢这个:

public static class ArrayExtensions {
    public static bool ArrayAndIndexesAreValid(
        T[] array,
        int startInclusive,
        int endExclusive
    ) {
    return array != null &&
           array.Length > 0 &&
           startInclusive >= 0 && startInclusive < array.Length &&
           endExclusive >= 1 && endExclusive <= array.Length &&
           startInclusive < endExclusive;
    }
    public static IEnumerable<T> Slice<T>(
        this T[] array,
        int startInclusive,
        int endExclusive
    ) {
        Contract.Requires<ArgumentException>(ArrayAndIndexesAreValid(
            array,
            startInclusive,
            endExclusive)
        );
        for (int index = startInclusive; index < endExclusive; index++) {
            yield return array[index];
        }
    }
    public static T MinimumInIndexRange<T>(
        this T[] array,
        int startInclusive,
        int endExclusive
    ) where T : IComparable {
        Contract.Requires<ArgumentException>(ArrayAndIndexesAreValid(
            array,
            startInclusive,
            endExclusive)
        );
        return array.Slice(startInclusive, endExclusive).Min();
    }

    public static T MaximumInIndexRange<T>(
        this T[] array,
        int startInclusive,
        int endExclusive
    ) where T : IComparable {
        Contract.Requires<ArgumentException>(ArrayAndIndexesAreValid(
            array,
            startInclusive,
            endExclusive)
        );
        return array.Slice(startInclusive, endExclusive).Max();
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top