Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Spiral Matrix

IList<int> PrintSpiralOrder(int[][] matrix)
{
  int hStart = 0;
  int hEnd = matrix[0].Length;
  int vEnd = matrix.Length;

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

  while (hStart < hEnd && hStart < vEnd)
  {
    for (int i = hStart; i < hEnd; i++)
      ans.Add(matrix[hStart][i]);

    for (int j = hStart + 1; j < vEnd; j++)
      ans.Add(matrix[j][hEnd - 1]);

    if (hStart < vEnd - 1)
      for (int k = hEnd - 2; k >= hStart; k--)
        ans.Add(matrix[vEnd - 1][k]);

    if (hStart < hEnd - 1)
      for (int l = vEnd - 2; l >= hStart + 1; l--)//1>
        ans.Add(matrix[l][hStart]);

    hStart++;
    hEnd--;
    vEnd--;
  }

  return ans;
}
Comment

spiral matrix

public class Solution 
{
    public IList<int> SpiralOrder(int[][] arr) 
    {
        var ls = new List<int>();
        var count = 0;
        var bol = new bool[arr.Length, arr[0].Length];

        int x = 0;
        int y = 0;
        var r = 0;
        while (count < arr.Length * arr[0].Length)
        {

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

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

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

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

spiral matrix

4
1 2 3 4 
5 6 7 8 
9 10 11 12
13 14 15 16
Comment

PREVIOUS NEXT
Code Example
Csharp :: asp net c# browser cursor wait 
Csharp :: unity rollaball 
Csharp :: unity gun clipping through walls 
Csharp :: how to parse mongo db json in c# 
Csharp :: c# how to delete all files in directory 
Csharp :: c# split multiple options 
Csharp :: windows forms webbrowser refresh 
Csharp :: c# Sum of all the factors of a number 
Csharp :: structure in c sharp with example 
Csharp :: c# xunit theory classdata model 
Csharp :: c# asp.net hover tooltip 
Csharp :: Get location in Xamarin - NAYCode.com 
Csharp :: how to stream video from vlc in c# 
Csharp :: c# ip address to string 
Csharp :: cread 2-dimensional array in c# 
Csharp :: use c#9 
Csharp :: c# in equivalent 
Csharp :: datagridview column index 
Csharp :: c# move directory 
Csharp :: dotnet core encryption and decryption 
Csharp :: Get replace normal text from word document in C# 
Csharp :: c# catch two exceptions in one block 
Csharp :: save binary data to file c# 
Csharp :: c# object add property 
Csharp :: how to remove from list from index c# 
Csharp :: send mail c# 
Csharp :: c sharp type in word and calculate how much a letter is inside that word 
Csharp :: c# button click gets assigned the last value 
Csharp :: ef save changes 
Csharp :: pass viewbag selectlistitem to razor 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =