【问题标题】:Code inside for loop doesnt work. Index was outside the bounds of the arrayfor 循环内的代码不起作用。指数数组的边界之外
【发布时间】:2020-04-20 19:59:47
【问题描述】:

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

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


        }
    }
}

【问题讨论】:

标签: c#


【解决方案1】:

您的问题发生在 n = 2 时,因为当您的 listlength 为 8 时,cases[i, 0] 为 5,cases[i, 1] 为 7 总和为 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;

}

【讨论】:

  • 这是hackerrank问题。简单来说,我必须从一个数组中获取特定的范围,在这种情况下是列表。然后在该范围内搜索一个最小值。然后将其返回到新数组中。抱歉语法错误,英语不是我的母语。
  • 认为 HackerRank 的重点是您不使用 StackOverflow 之类的网站!
【解决方案2】:

问题是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 个!

如果不确切知道您想要实现什么/预期的输出应该是什么,就很难知道它应该是什么!

【讨论】:

  • 这是hackerrank问题。简单来说,我必须从一个数组中获取特定的范围,在这种情况下是列表。然后在该范围内搜索一个最小值。然后将其返回到新数组中。抱歉语法错误,英语不是我的母语。
  • 认为 HackerRank 的重点是您不使用 StackOverflow 之类的网站!
【解决方案3】:

数组和列表太多了。所有这些来回转换都有效地制作了数据的完整副本。坚持使用 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();
         });
}

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

【讨论】:

    猜你喜欢
    • 2011-04-15
    • 2018-10-26
    • 1970-01-01
    相关资源
    最近更新 更多