C#二进制搜索出错-数组的偏移量和长度超出范围

时间:2020-06-20 19:57:41

标签: c# binary-search

我正在使用二进制搜索,如下面的代码所示

int[] cost=[1,2,3,5,6]
money=5
    static void whatFlavors(int[] cost, int money)
    {
        int[] costSort = (int[])cost.Clone();
        Array.Sort(costSort);

        for (int i = 0; i < costSort.Length; i++)
        {
            int complement = money - costSort[i];
            int location=  Array.BinarySearch(costSort, i+1, costSort.Length,complement);
            if(location >=0 && location <cost.Length && costSort[location]== complement)
            {
                int[] indicies = getindicies(cost, cost[i], complement);
                break;
            }
        }



    }

但是我在Array.BinarySearch遇到了异常

数组的偏移量和长度超出范围,或者计数大于从索引到源集合末尾的元素数量

0 个答案:

没有答案