【问题标题】:how to clear Hackerank Dynamic array Runtime error?如何清除 Hackerrank 动态数组运行时错误?
【发布时间】:2020-07-09 04:07:38
【问题描述】:

我已经完成了编码,我的几个测试用例通过了,但是测试用例 2 和其他几个没有通过。当我在本地运行相同的代码时,我得到了想要的结果,但在 HackerRank 上,它显示运行时错误。

这是问题 - https://www.hackerrank.com/challenges/dynamic-array/problem

这是我的解决方案:-

public static List dynamicArray(int n, List> queries) {

   List<List<int>> seqList = new List<List<int>>();

        for (int j = 0; j < n ; j++)
        {
            seqList.Insert(j, new List<int>(n-1))  ;
        }


        List<int> seqList2 = new List<int>();

        int lastAnswer = 0;
        int index;

        for (int i = 0; i < queries.Count ; i++)
        {

                index = ((queries[i][1] ^ lastAnswer) % n); 

                if (queries[i][0] == 1)
                {
                  seqList[index].Add(Convert.ToInt32(queries[i][2]));
                }

                else

                {

                int value = queries[i][2] % seqList[index].Count ;
                lastAnswer = seqList[index][value];

                    Console.WriteLine(lastAnswer);
                    seqList2.Add(lastAnswer);
                } 

        } 

【问题讨论】:

  • 你确定这是你完整的 dynamicArray 函数,你是在某处返回 seqList 吗?
  • 是的,我要返回 seqList2

标签: c# asp.net arrays .net


【解决方案1】:

Hackerrank 的运行时错误可能是由于未初始化的变量或尝试访问不存在的数组中的元素 (ArgumentOutOfRangeException)

我猜你的情况是因为后者。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多