C
C#4mo ago
morry329#

IndexOutOfRangeException? Why??

I am analysing a solution for Spiral Matrix puzzle on LeetCode: https://leetcode.com/problems/spiral-matrix/ But I keep getting an IndexOutOfRange exception at this line if (x >= r && !bol[y, x]) Could anyone kindly tell me why? Here is the whole solution code
public IList<int> SpiralOrder3(int[][] arr2D)
{
var ls = new List<int>();
var count = 0;

var bol = new bool[arr2D.Length,arr2D[0].Length];

int x = 0, y = 0;
var r = 0;

while (count < arr2D.Length * arr2D[0].Length)
{
for (int i = 0; i < arr2D[0].Length - r; i++)
{
if (x < arr2D[0].Length - r && !bol[y, x])
{
bol[y, x] = true;
ls.Add(arr2D[y][x++]);
count++;
}
}

y++;
x--;

for (int i = 0; i < arr2D.Length - r; i++)
{
if (y < arr2D.Length - r && !bol[y, x])
{
bol[y, x] = true;
ls.Add(arr2D[y++][x]);
count++;
}
}

y--;
x++;

for (int i = 0; i < arr2D[0].Length - r; i++)
{
if (x >= r && !bol[y, x]) //EXCEPTION

{
bol[y, x] = true;
ls.Add(arr2D[y][x--]);
count++;
}
}

for (int i = 0; i < arr2D.Length - r; i++)
{
if (y >= r && !bol[y, x])
{
bol[y, x] = true;
ls.Add(arr2D[y--][x]);
count++;
}
}

x++;
y++;
r++;
}

return ls;
}
public IList<int> SpiralOrder3(int[][] arr2D)
{
var ls = new List<int>();
var count = 0;

var bol = new bool[arr2D.Length,arr2D[0].Length];

int x = 0, y = 0;
var r = 0;

while (count < arr2D.Length * arr2D[0].Length)
{
for (int i = 0; i < arr2D[0].Length - r; i++)
{
if (x < arr2D[0].Length - r && !bol[y, x])
{
bol[y, x] = true;
ls.Add(arr2D[y][x++]);
count++;
}
}

y++;
x--;

for (int i = 0; i < arr2D.Length - r; i++)
{
if (y < arr2D.Length - r && !bol[y, x])
{
bol[y, x] = true;
ls.Add(arr2D[y++][x]);
count++;
}
}

y--;
x++;

for (int i = 0; i < arr2D[0].Length - r; i++)
{
if (x >= r && !bol[y, x]) //EXCEPTION

{
bol[y, x] = true;
ls.Add(arr2D[y][x--]);
count++;
}
}

for (int i = 0; i < arr2D.Length - r; i++)
{
if (y >= r && !bol[y, x])
{
bol[y, x] = true;
ls.Add(arr2D[y--][x]);
count++;
}
}

x++;
y++;
r++;
}

return ls;
}
`
LeetCode
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
2 Replies
FusedQyou
FusedQyou4mo ago
This code is way too complex and unreadable to get a proper answer The best solution here is to improve the readability of the code, and possible placing Debug.Assert statements to check if variables are what they should be Currently you have multiple actions happening in a single method and these should be split up. You should also name your variables better and not reuse everything constantly. Debugging this takes way too much time because you need to find out the behaviour of the whole method before anything can be determined, so you should improve the way you write it
morry329#
morry329#4mo ago
Thank you so much for your assessment of this code. Someone else wrote it so it's good for me to know it's way too complex and needs to be split up. I still don't have a good set of eyes to distinguish the clean code from not so clean ones