我对在线代码挑战比较陌生,对HackerRank的工作原理有些困惑。
以下实现应该可以很好地用于对角差异质询:
// Test case 0
List<List<int>> arr = new List<List<int>>();
arr.Add(new List<int> { 3 });
arr.Add(new List<int> { 11, 2, 4 });
arr.Add(new List<int> { 4, 5, 6 });
arr.Add(new List<int> { 10, 8, -12 });
// Sum across the primary diagonal: 11 + 5 - 12 = 4
// Sum across the secondary diagonal: 4 + 5 + 10 = 19
// Absolute Difference: |4 - 19| = 15
// Test case 1
/*
List<List<int>> arr = new List<List<int>>();
arr.Add(new List<int> { 4 });
arr.Add(new List<int> { -1, 1, -7, -8 });
arr.Add(new List<int> { -10, -8, -5, -2 });
arr.Add(new List<int> { 0, 9, 7, -1 });
arr.Add(new List<int> { 4, 4, -2, 1 });
*/
// By definition, the data structure already provides the size.
arr.RemoveAt(0);
List<List<int>> matrix = arr;
List<int> principalDiagonal = new List<int>();
for (int i = 0; i < matrix.Count; i++)
{
principalDiagonal.Add(matrix[i][i]);
}
List<int> secondaryDiagonal = new List<int>();
for (int i = 0; i < matrix.Count; i++)
{
secondaryDiagonal.Add(matrix[i][matrix.Count - 1 - i]);
}
int absoluteDifference = Math.Abs(principalDiagonal.Sum() - secondaryDiagonal.Sum());
字符串
我甚至在一个.NET Fiddle上用他们自己的几个测试用例试过它,它像预期的那样工作:https://dotnetfiddle.net/6s4aU2
然而,当我提交代码时,它被认为是一个错误的答案,奇怪的是没有任何错误,只是基于测试用例的不同结果。
// HackerRank's "Console" message
/*
Compiler Message
Wrong Answer
Input (stdin)
3
11 2 4
4 5 6
10 8 -12
Your Output (stdout)
3
*/
型
有没有人在这个平台上有类似的经历?
1条答案
按热度按时间qkf9rpyu1#
问题是您更改了读取数据的方式。(所以现在您是从创建的静态表中阅读数据)
下面的代码是工作的(是你的解决方案内提供的方法)
字符串