【问题标题】:Inserting elements in a matrix spirally在矩阵中螺旋插入元素
【发布时间】:2016-07-16 10:36:09
【问题描述】:

给定一个数字 x,将元素 1 到 x^2 螺旋插入矩阵中。 例如对于 x = 3,矩阵看起来像 [[1,2,3],[8,9,4],[7,6,5]]。 为此,我写了以下 sn-p。但是,我得到的 o/p 为 [[7,9,5],[7,9,5],[7,9,5]]

while(t<=b && l<=r){
               System.out.print(t+" "+b+" "+l+" "+r+"\n");
        if(dir==0){
            for(int i = l;i<=r;i++){
                arr.get(t).set(i,x);
                x++;
            }

            t++;
        }else if(dir==1){
            for(int i = t;i<=b;i++){
                arr.get(i).set(r,x);
                x++;
            }
            r--;
        }else if(dir==2){
            for(int i = r;i>=l;i--){
                arr.get(b).set(i,x);
                x++;
            }
            b--;
        }else if(dir==3){
            for(int i = b;i>=t;i--){
                arr.get(l).set(i,x);
                x++;
            }
            l++;
        }
        dir = (dir+1)%4;

    }

【问题讨论】:

    标签: matrix spiral


    【解决方案1】:

    您可以使用下一个代码(我为一些处理巨大矩阵大小的实现而开发的代码)。它将使用任何矩阵大小的宽度(列)和高度(行)并产生您需要的输出

        List<rec> BuildSpiralIndexList(long w, long h)
        {
            List<rec> result = new List<rec>();
            long count = 0,dir = 1,phase = 0,pos = 0;
            long length = 0,totallength = 0;
            bool isVertical = false;
    
            if ((w * h)<1) return null;
            do
            {
                isVertical = (count % 2) != 0;
                length = (isVertical ? h : w) - count / 2 - count % 2;
                phase = (count / 4);
                pos = (count % 4);
                dir = pos > 1 ? -1 : 1;
                for (int t = 0; t < length; t++)
                    // you can replace the next code with printing or any other action you need
                    result.Add(new rec()
                    {
                        X = ((pos == 2 || pos == 1) ? (w - 1 - phase - (pos == 2 ? 1 : 0)) : phase) + dir * (isVertical ? 0 : t),
                        Y = ((pos <= 1 ? phase + pos : (h - 1) - phase - pos / 3)) + dir * (isVertical ? t : 0),
                        Index = totallength + t
                    });
                totallength += length;
                count++;
            } while (totallength < (w*h));
            return result;
        }
    

    【讨论】:

      【解决方案2】:

      此解决方案从左上到右上,从右上到右下,从右下到左下,从左下到左上。 这是一个棘手的问题,希望我下面的cmets能帮助解释。

      下面是一个 codepen 链接,可以看到它添加到表格中。 https://codepen.io/mitchell-boland/pen/rqdWPO

      const n = 3; // Set this to a number
      
      matrixSpiral(n);
      
      function matrixSpiral(number){
      
          // Will populate the outer array with n-times inner arrays
          var outerArray = [];
      
          for(var i = 0; i < number; i++){
            outerArray.push([]);
          }
      
      
          var leftColumn = 0;
          var rightColumn = number - 1;
          var topRow = 0;
          var bottomRow = number-1;
          var counter = 1; // Used to track the number we are up to.
      
          while(leftColumn <= rightColumn && topRow  <=bottomRow){
      
              // populate the top row
              for(var i = leftColumn; i <= rightColumn; i++){
                outerArray[leftColumn][i] = counter;
                counter++;
              }
              // Top row is now populated
              topRow ++;
      
              // Populate the right column
              for(var i = topRow ; i <= bottomRow; i++){
                outerArray[i][rightColumn] = counter;
                counter++;
              }
              // Right column now populated.
              rightColumn--;
      
              // Populate the bottom row
              // We are going from the bottom right, to the bottom left
              for(var i = rightColumn; i >= leftColumn; i--){
                outerArray[bottomRow][i] = counter;
                counter++;
              }
              // Bottom Row now populated
              bottomRow--;
      
              // Populate the left column
              // We are going from bottom left, to top left
              for(var i = bottomRow; i >= topRow ; i--){
                outerArray[i][leftColumn] = counter;
                counter++;
              }
              // Left column now populated.
              leftColumn++;
      
              // While loop will now repeat the above process, but a step in.
          }
      
          // Console log the results.
          for(var i = 0; i < number; i++){
              console.log(outerArray[i]);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-18
        • 2016-11-27
        • 1970-01-01
        相关资源
        最近更新 更多