在HackerRank上执行this problem,我的O(n)解决方案正在通过除最后三个测试用例之外的所有测试用例,它与运行时错误一起失败。不幸的是,我无法看到运行时错误是什么。当我在Visual Studio中运行测试时,我没有遇到任何错误。任何想法可能导致这个问题?任何想法,为什么我在这里得到一个运行时错误?
using System;
using System.Collections.Generic;
using System.IO;
class Solution
{
public static void Swap(int[] A, int i1, int i2)
{
int temp = A[i1];
A[i1] = A[i2];
A[i2] = temp;
}
static void Main(String[] args)
{
int[] parameters = Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse);
int n = parameters[0];
int k = parameters[1];
int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse);
int[] pos = new int[n + 1]; // pos[m] is the index of the value m in arr
for(int i = 0; i < arr.Length; ++i)
{
pos[arr[i]] = i;
}
for(int i = 0; i < arr.Length && k > 0; ++i, --n)
{
if(arr[i] == n)
continue;
int j = pos[n];
Swap(pos, arr[i], n);
Swap(arr, i, j);
--k;
}
Console.WriteLine(string.Join(" ", arr));
}
}
测试失败后,他们确实允许您从测试用例中“购买”输入和期望的输出,并使用您在网站上赚取的一些积分。我知道这不是你所问的,但是这是一个可以考虑的选择。 –
我想无效的'int'输入? – slawekwin