< Summary

Information
Class: LeetCode.Algorithms.SpiralMatrix4.SpiralMatrix4Simulation
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SpiralMatrix4\SpiralMatrix4Simulation.cs
Line coverage
100%
Covered lines: 64
Uncovered lines: 0
Coverable lines: 64
Total lines: 120
Line coverage: 100%
Branch coverage
100%
Covered branches: 21
Total branches: 21
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SpiralMatrix(...)100%2121100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SpiralMatrix4\SpiralMatrix4Simulation.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12using LeetCode.Core.Models;
 13
 14namespace LeetCode.Algorithms.SpiralMatrix4;
 15
 16/// <inheritdoc />
 17public class SpiralMatrix4Simulation : ISpiralMatrix4
 18{
 19    /// <summary>
 20    ///     Time complexity - O(m * n)
 21    ///     Space complexity - O(1)
 22    /// </summary>
 23    /// <param name="m"></param>
 24    /// <param name="n"></param>
 25    /// <param name="head"></param>
 26    /// <returns></returns>
 27    public int[][] SpiralMatrix(int m, int n, ListNode head)
 328    {
 329        var matrix = new int[m][];
 30
 2231        for (var i = 0; i < m; i++)
 832        {
 833            matrix[i] = new int[n];
 834        }
 35
 336        var x = 0;
 337        var y = 0;
 338        var round = 0;
 339        var direction = 0;
 340        var count = 0;
 41
 342        var listNode = head;
 43
 3844        while (count < m * n)
 3545        {
 3546            if (listNode == null)
 347            {
 348                matrix[x][y] = -1;
 349            }
 50            else
 3251            {
 3252                matrix[x][y] = listNode.val;
 3253            }
 54
 3555            switch (direction)
 56            {
 57                case 0:
 58
 1859                    if (y + 1 < n - round)
 1360                    {
 1361                        y++;
 1362                    }
 63                    else
 564                    {
 565                        direction = 1;
 66
 567                        x++;
 568                    }
 69
 1870                    break;
 71                case 1:
 672                    if (x + 1 < m - round)
 373                    {
 374                        x++;
 375                    }
 76                    else
 377                    {
 378                        direction = 2;
 79
 380                        y--;
 381                    }
 82
 683                    break;
 84                case 2:
 885                    if (y - 1 < round)
 386                    {
 387                        direction = 3;
 88
 389                        round++;
 90
 391                        x--;
 392                    }
 93                    else
 594                    {
 595                        y--;
 596                    }
 97
 898                    break;
 99                case 3:
 3100                    if (x - 1 < round)
 2101                    {
 2102                        direction = 0;
 103
 2104                        y++;
 2105                    }
 106                    else
 1107                    {
 1108                        x--;
 1109                    }
 110
 3111                    break;
 112            }
 113
 35114            listNode = listNode?.next;
 35115            count++;
 35116        }
 117
 3118        return matrix;
 3119    }
 120}