C
C#2y ago
Cloudy

❔ Error for no reason?

I'm trying a leetcode question and I think I've solved it but whenever I run it, I get an error:
Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
Yet I can see with my logs that the whole code runs through and works perfectly, nowhere for this error to occur other than when returning the value. But this error wasn't happening earlier in testing with the same return value
3 Replies
Cloudy
CloudyOP2y ago
My Code:
public class Solution {
public IList<string> LetterCombinations(string digits) {
int size = ParseToChar(digits[0]).Length;
for (int digit = 1; digit < digits.Length; digit++)
{
size *= ParseToChar(digits[digit]).Length;
}
Console.WriteLine(size);

string[] result = new string[size];

if (size == 1)
return new string[0];

for (int digit = 0; digit < digits.Length; digit++)
{
string abcValues = ParseToChar(digits[digit]);

float v = 0;

int num = (int)Math.Pow(size / abcValues.Length, digit);

for (int i = 0; i < result.Length; i++)
{
int value = (int)Math.Floor(v / num);

Console.WriteLine("{0}, {1}, {2}, {3}, {4}", digit, num, i, v, value);
Console.WriteLine(abcValues[value]);

result[i] += abcValues[value];

Console.WriteLine(result[i]);

v++;

if (v == abcValues.Length * num)
v = 0;
}
}

Console.WriteLine("Result:");
for (int i = 0; i < result.Length; i++)
{
Console.WriteLine(result[i]);
}

return result;
}

public string[] chars = new string[]{
"",
"",
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"
};

public string ParseToChar(char c)
{
int num = c - '0';
return chars[num];
}
}
public class Solution {
public IList<string> LetterCombinations(string digits) {
int size = ParseToChar(digits[0]).Length;
for (int digit = 1; digit < digits.Length; digit++)
{
size *= ParseToChar(digits[digit]).Length;
}
Console.WriteLine(size);

string[] result = new string[size];

if (size == 1)
return new string[0];

for (int digit = 0; digit < digits.Length; digit++)
{
string abcValues = ParseToChar(digits[digit]);

float v = 0;

int num = (int)Math.Pow(size / abcValues.Length, digit);

for (int i = 0; i < result.Length; i++)
{
int value = (int)Math.Floor(v / num);

Console.WriteLine("{0}, {1}, {2}, {3}, {4}", digit, num, i, v, value);
Console.WriteLine(abcValues[value]);

result[i] += abcValues[value];

Console.WriteLine(result[i]);

v++;

if (v == abcValues.Length * num)
v = 0;
}
}

Console.WriteLine("Result:");
for (int i = 0; i < result.Length; i++)
{
Console.WriteLine(result[i]);
}

return result;
}

public string[] chars = new string[]{
"",
"",
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"
};

public string ParseToChar(char c)
{
int num = c - '0';
return chars[num];
}
}
Log:
3
0, 1, 0, 0, 0
d
d
0, 1, 1, 1, 1
e
e
0, 1, 2, 2, 2
f
f
Result:
d
e
f
3
0, 1, 0, 0, 0
d
d
0, 1, 1, 1, 1
e
e
0, 1, 2, 2, 2
f
f
Result:
d
e
f
But the important part is just this:
public IList<string> LetterCombinations(string digits) {
string[] result = new string[3];

// Code

Console.WriteLine("aaaa"); // aaaa

return result; // Error
}
public IList<string> LetterCombinations(string digits) {
string[] result = new string[3];

// Code

Console.WriteLine("aaaa"); // aaaa

return result; // Error
}
The code works perfectly in VS but not in leetcode, any ideas why? Ah ok I figured it out, silly mistake Turns out the error was in fact at the top of the code but it still continued running onto the second testcase and then had an error, I assumed the error would mention what testcase it was on for some reason
blinkbat
blinkbat2y ago
you should learn to use the debugger in lieu of logs much more efficient and powerful for debugging
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server