首页 > 其他分享 >【LeetCode】59. 螺旋矩阵 II

【LeetCode】59. 螺旋矩阵 II

时间:2024-05-21 21:51:29浏览次数:23  
标签:count 59 int res II start LeetCode

题目:59. 螺旋矩阵 II

解题思路

  1. 手动模拟螺旋矩阵,分别实现四个方向的代码,将数组依次填入数组中即可
  2. 需要注意的是,如果n为奇数,说明最后只剩下中间的一个位置,将最后一个数直接填入即可;若n为偶数,则正好能够遍历n/2遍
class Solution {
    public int[][] generateMatrix(int n) {
        int[][] res = new int[n][n];
        int count = 1;
        int start = 0;
        int loop = 0;
        int i, j;

        while(loop++ < n/2) {
            for(j=start; j<n-loop; j++) {
                res[start][j] = count++;
            }

            for(i=start; i<n-loop; i++) {
                res[i][j] = count++;
            }

            for(; j>=loop; j--) {
                res[i][j] = count++;
            }

            for(; i>=loop; i--) {
                res[i][j] = count++;
            }
            start++;
        }

        if(n % 2 == 1) {
            res[start][start] = count;
        }

        return res;
    }
}

参考资料:59. 螺旋矩阵 II

标签:count,59,int,res,II,start,LeetCode
From: https://www.cnblogs.com/syr463/p/18205024

相关文章

  • Reflective Journal III
    Theprocessofmakingadigitalstory:Whenichoosethestory,thefirstideainmymindisanantienttaleihavereadwithmynephew.Soidecidedtochangethepointofviewofthistale.Atfirst,isimplifiedthestoryandfoundtheprotagonist,putting......
  • Reflective Journal III
    ItriedtousethefirstpersonpointofviewtonarrateSleepingBeauty,whichismyfavoritestoryinmychildhood.Soinordertocreateacompletedigitalstory,thefirststepItookwastosearchforthedetailsofthestory.Afterthis,Ifoundsom......
  • web259(超详细)
    SoapClient看完之后还不是很懂的话就可以去下面的两个网址去补充一下SoapClient是PHP中用于与SOAP(SimpleObjectAccessProtocol)服务进行通信的内置类。SOAP是一种基于XML的协议,用于在分布式环境中交换结构化信息。以下是关于SoapClient类的详细解释:创建SoapClien......
  • Reflective Journal III
    Whenmakingadigitalstory,Iidentifymyownmaterialfirst,andIchooseafilmthatimpressesmethemost.IcollectedvideomaterialsfromvarioussourcesandusedJianyingtoassemblethem.Addingtransitionsandadjustingsubtitlesaresimpleoperati......
  • Reflective Journal III
    Whenitcomestomyfavoritestory,I'dliketocomeupwithBarbiewiththreeswordswomen.Thestorytellstheprocessabouthowthefourbravegirlsachievedtheirdreams,whichseemtobeimpossibletoachieve.Theinsistentspiritofthemmadea......
  • Reflective Journal III
    1.Intheprocessofmakingthisdigitalstory,firstly,IlistedaoutlinetoclearwhatIneed.Secondly,IquestedforsomepicturesandbackgroundmusicontheInternet.Thirdly,Ireadthenarrativewritingandrecordedit.Finally,Imadeavideobyinsertin......
  • Reflective Journal II
    1.Firstly,Igotaideaaboutthechoiceofstory.Next,ascriptwaswrittentooutlineit.Then,Idownloadedsomerelatedpictures,addedsubtitlesandvoiceovers.Furthermore,Iaddedbackgroundmusic,soundeffects,andvisualeffectstoenhancethenarrativ......
  • Reflective Journal III
    TheinitialphaseincraftingadigitalnarrativeentailedformulatingapreciseconceptionofthetaleIintendedtounfold.Followingthedelineationofthestorylineandpivotaloccurrences,Iembarkedonaquesttoamassvisualcomponents,encompassin......
  • [LeetCode] 1863. Sum of All Subset XOR Totals
    TheXORtotalofanarrayisdefinedasthebitwiseXORofallitselements,or0ifthearrayisempty.Forexample,theXORtotalofthearray[2,5,6]is2XOR5XOR6=1.Givenanarraynums,returnthesumofallXORtotalsforeverysubsetofnums.......
  • Reflective Journal III
    ......