首页 > 其他分享 >【模拟】LeetCode 54. 螺旋矩阵

【模拟】LeetCode 54. 螺旋矩阵

时间:2023-01-04 10:23:18浏览次数:64  
标签:matrix firstColumn int 54 矩阵 firstRow res lastRow LeetCode

题目链接

54. 螺旋矩阵

思路

通过维护上下左右四个边界变量来控制循环。

代码

class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        int firstRow = 0;
        int lastColumn = matrix[0].length - 1;
        int lastRow = matrix.length - 1;
        int firstColumn = 0;
        ArrayList<Integer> res = new ArrayList<>();

        while(true){
            for(int i = firstColumn; i <= lastColumn; ++i){
                res.add(matrix[firstRow][i]);
            }
            if(firstRow + 1 > lastRow){
                break;
            }
            firstRow++;

            for(int i = firstRow; i <= lastRow; ++i){
                res.add(matrix[i][lastColumn]);
            }
            if(lastColumn - 1 < firstColumn){
                break;
            }
            lastColumn--;

            for(int i = lastColumn; i >= firstColumn; --i){
                res.add(matrix[lastRow][i]);
            }
            if(lastRow - 1 < firstRow){
                break;
            }
            lastRow--;

            for(int i = lastRow; i >= firstRow; --i){
                res.add(matrix[i][firstColumn]);
            }
            if(firstColumn + 1 > lastColumn){
                break;
            }
            firstColumn++;
        }

        return res;
    }
}

标签:matrix,firstColumn,int,54,矩阵,firstRow,res,lastRow,LeetCode
From: https://www.cnblogs.com/shixuanliu/p/17024109.html

相关文章

  • leetcode-645. 错误的集合
    645.错误的集合-力扣(Leetcode)又用了哈希表,又用了数学计算,看题解有个位运算看不太懂funcfindErrorNums(nums[]int)[]int{m:=make(map[int]struct{},len(nu......
  • leetcode-643. 子数组最大平均数 I
    643.子数组最大平均数I-力扣(Leetcode)滑动窗口,判断好边界条件即可funcfindMaxAverage(nums[]int,kint)float64{begin,end:=0,k-1ifend>=len(n......
  • 用Python批量绘制二维矩阵
    importnumpyasnpfrommatplotlibimportpyplotaspltimportmatplotlibasmplimportglobdefcreate_4_colorMap():#colors=['blue','cyan','green','p......
  • 算法刷题 Day 7 | 454.四数相加II 383. 赎金信 15. 三数之和 18. 四数之和
    从今天开始,下定决心正式把语言转为C++,之后也会用C++重新把前几天的题解再写一遍。加油454.四数相加II建议:本题是使用map巧妙解决的问题,好好体会一下哈希法如何提......
  • leetcode-168-easy
    ExcelSheetColumnTitleGivenanintegercolumnNumber,returnitscorrespondingcolumntitleasitappearsinanExcelsheet.Forexample:A->1B->2C-......
  • leetcode-206-easy
    ReverseLinkedListGiventheheadofasinglylinkedlist,reversethelist,andreturnthereversedlist.Example1:Input:head=[1,2,3,4,5]Output:[5,......
  • leetcode-557-easy
    ReverseWordsinaStringIIIGivenastrings,reversetheorderofcharactersineachwordwithinasentencewhilestillpreservingwhitespaceandinitialwo......
  • leetcode-627-easy
    IslandPerimeterYouaregivenrowxcolgridrepresentingamapwheregrid[i][j]=1representslandandgrid[i][j]=0representswater.Gridcellsareconn......
  • leetcode-121-easy
    BestTimetoBuyandSellStockYouaregivenanarraypriceswhereprices[i]isthepriceofagivenstockontheithday.Youwanttomaximizeyourprofitb......
  • leetcode-441-easy
    ArrangingCoinsYouhavencoinsandyouwanttobuildastaircasewiththesecoins.Thestaircaseconsistsofkrowswheretheithrowhasexactlyicoins.Th......