2014普及组-螺旋矩阵_如何遍历螺旋矩阵中的数组值

2014普及组-螺旋矩阵

Imagine you are walking through a dungeon and you want to find the destination point in the middle.

想象一下,您正在穿过地牢,并且想要在中间找到目的地。

That’s right…printing values in a spiral matrix feels like that.

没错……在螺旋矩阵中打印值就像那样。

To give you some context, let’s say we have the following double array arr:

为了给您一些背景信息,假设我们有以下双重数组arr:

const arr = [[1,2,3,4,5]
             [16,17,18,19,6]
             [15,24,25,20,7]
             [14,23,22,21,8]
             [13,12,11,10,9]];

As you can see, the numbers spirals 1 to 25 from the outer layers, left to right, top to bottom, right to left, and bottom to up as it dives across other inner layers in a similar passion.

如您所见,当数字以相似的热情跨越其他内层时,数字从外层从左到右,从上到下,从右到左以及从下到上螺旋上升1到25。

The question is, how do we traverse the whole array in a spiral manner and print them in the right format?

问题是,我们如何以螺旋方式遍历整个数组并以正确的格式打印它们?

Here is my suggested solution:

这是我建议的解决方案:

  • Step 1: have a row begin and row-end count, column begin, and column end count. These counts will be used to track boundaries and conditions to end looping through the matrix.

    步骤1:行开始和行结束计数,列开始和列结束计数。 这些计数将用于跟踪边界和条件,以结束循环遍历矩阵。
  • Step 2: add items going from top row, left to right. Afterward, increment row begin count.

    第2步:添加从顶部到左侧的项目。 之后,递增行开始计数。
  • Step 3: add items on the rightmost column, from top to bottom. Afterward, decrement column end count.

    步骤3:在最右边的列中,从上至下添加项目。 之后,递减列结束计数。
  • Step 4: Check if row begin is less than or equal to row end count. This is to prevent adding/printing existing elements that have already been traversed. If the condition matches, then we traverse on the current column end count(on the first iteration, this will be iterating on the last row, going from right column to the leftmost column) until it hits columnBegin count.

    步骤4:检查行开头是否小于或等于行结束计数。 这是为了防止添加/打印已经遍历的现有元素。 如果条件匹配,则遍历当前列的结束计数(在第一次迭代中,将在最后一行上进行迭代,从右列到最左列),直到达到columnBegin计数为止。
  • Step 5:check if the column begins is less than or equal to columned count. This is to prevent adding/printing existing elements that have already been traversed. If the condition matches, then we traverse on the current row end count(on the first iteration, this will be iterating on left-most column, going from bottom to up on the same column) until it hits rowBegin count.

    第5步:检查列开头是否小于或等于列数。 这是为了防止添加/打印已经遍历的现有元素。 如果条件匹配,则遍历当前行的结束计数(在第一次迭代中,将在最左边的列上进行迭代,在同一列上从下到上进行迭代),直到达到rowBegin计数为止。

Here is the implementation for the algorithm:

这是算法的实现:

const spiralOrder = (matrix) => {
   const arr = [];


   if(matrix.length === 0) return arr;
   let rowBegin = 0;
   let rowEnd = matrix.length - 1;
   let columnBegin = 0;
   let columnEnd = matrix[0].length -1;
    
   while(rowBegin <= rowEnd && columnBegin <= columnEnd) {
      for(let i = columnBegin; i <= columnEnd; i++){
         arr.push(matrix[rowBegin][i]);
      }
      rowBegin++;
       
      for(let i = rowBegin; i <= rowEnd; i++){
          arr.push(matrix[i][columnEnd]);
      }
      columnEnd--;
       
      if(rowBegin <= rowEnd){
         for(let i = columnEnd; i >= columnBegin; i--){
            arr.push(matrix[rowEnd][i]);
         }      
      }
      rowEnd--;
       
      if(columnBegin <= columnEnd){
          for(let i = rowEnd; i>= rowBegin; i--){
             arr.push(matrix[i][columnBegin]);
          }          
      }
      columnBegin++;
   }
    return arr;
};

That’s it! You just learned how to solve the spiral matrix problem.

而已! 您刚刚学习了如何解决螺旋矩阵问题。

翻译自: https://medium.com/weekly-webtips/how-to-traverse-through-array-values-in-a-spiral-matrix-cd9d3b1a80c8

2014普及组-螺旋矩阵

你可能感兴趣的:(算法,leetcode,python,matlab,数据结构)