for循环内的代码不起作用。指数数组的边界之外

时间:2020-04-20 19:59:47

标签: c#

在for循环中,代码不起作用。错误:索引超出了数组的范围。 当我将代码放入主要方法并手动尝试时,一切正常。你能告诉我怎么了吗?

namespace HackerRankProblems
{
    class Program
    {

        static int[] serviceLane(int n, int[] width, int[,] cases)
        {
            List<int> list = width.OfType<int>().ToList();
            List<int> returnList = new List<int>();
            List<int> tempList;
            for (int i = 0; i < n; i++)
            {
                tempList = list.GetRange(cases[i, 0], cases[i, 1]);
                returnList.Add(tempList.Min());



            }
            int[] returnArray = returnList.ToArray();
            return returnArray;

        }


        static void Main(string[] args)
        {

            int[,] cases = new int[,]
            {
                {1,2},
                {3,4},
                {5,7}
            };

            int[] width = new int[] { 2, 3, 1, 2, 3, 2, 3, 3 };

            List<int> lista = width.OfType<int>().ToList();
            List<int> returnLista = new List<int>();

            List<int> tempList = lista.GetRange(cases[3, 0], cases[1, 1]);
            returnLista.Add(tempList.Min());
            int[] returnArray = returnLista.ToArray();
            string.Join(",", serviceLane(3, width, cases));// Error


        }
    }
}

3 个答案:

答案 0 :(得分:0)

问题是list.GetRange(cases[i, 0], cases[i, 1])。当n为2时,对list.GetRange(cases[i, 0], cases[i, 1])的调用与list.GetRange(5, 7)相同,但是width / list只有8个元素,而不是12个!

没有确切地知道您想要实现什么/期望的输出应该是什么,很难知道它应该是什么!

答案 1 :(得分:0)

您的问题在n = 2时发生,这是因为case [i,0]为5,cases [i,1]为7,当您的listlength为8时总计为12,我认为您正在尝试要做的就是这个

static int[] serviceLane(int n, int[] width, int[,] cases)
{
    List<int> list = width.OfType<int>().ToList();
    List<int> returnList = new List<int>();
    List<int> tempList;
    for (int i = 0; i < n; i++)
    {
        tempList = list.GetRange(cases[i, 0], cases[i, 1] - cases[i, 0]);
        returnList.Add(tempList.Min());
    }
    int[] returnArray = returnList.ToArray();
    return returnArray;

}

答案 2 :(得分:0)

太多数组和列表。所有来回的转换有效地完成了数据的完整副本。坚持使用IEnumerable可以节省很多工作。

class Program
{

    static IEnumerable<int> serviceLane(int[] width, int[,] cases)
    {
        return Enumerable.Range(0, cases.GetUpperBound(0)).
               Select(i => width.Skip(cases[i,0]).Take(cases[i,1]).Min());
    }

    static void Main(string[] args)
    {
        int[,] cases = new int[,]
        {
            {1,2},
            {3,4},
            {5,7}
        };

        int[] width = new int[] { 2, 3, 1, 2, 3, 2, 3, 3 };

        //changed to [1,0] to say within the bounds of the array
        var temp = width.Skip(cases[1,0]).Take(cases[1,1]);
        var resultArray = new int[] {temp.Min()};
        var resultString = string.Join(",", serviceLane(width, cases));
    }
}

但是,该程序将与原始程序存在相同的问题:错误数据。对于所有可能的示例情况,width数组都不大。我们可以调整方法以解决此问题:

static IEnumerable<int> serviceLane(int[] width, int[,] cases)
{
    return Enumerable.Range(0, cases.GetUpperBound(0)).
         Select(i => 
         {
             if (cases[i,0] + cases[i,1]) >= width.Length)
             {
                 //do something here. Width is not long enough.
             }
             else 
                 return width.Skip(cases[i,0]).Take(cases[i,1]).Min();
         });
}

但是可能我们没有正确看到宽度样本数据。