[LeetCode] Spiral Matrix,leetcodespiral

來源:互聯網
上載者:User

[LeetCode] Spiral Matrix,leetcodespiral

這個題目的意思比較簡單,就是螺旋式的輸出數組的資料,[
[

 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].

我自己的解法比較直接,就是從最外層向內層逐步遞迴這種方式不需要額外的儲存空間,但是邊界控制比較繁瑣,代碼如下:

    public List<Integer> spiralOrder(int[][] matrix) {        List<Integer> rs = new LinkedList<Integer>();        int m, n;//row and column        if(matrix == null || matrix.length == 0)            return rs;        else {            m = matrix.length;            n = matrix[0].length;            getSpiralData(matrix, 0, 0, m, n, rs);        }        return rs;    }    /**     * 迴圈擷取資料     * @param matrix data matrix     * @param startX 起始位置     * @param startY     * @param maxM max row     * @param maxN max column     * @param rs result list     */    public void getSpiralData(int[][] matrix, int startX, int startY, int maxM, int maxN, List<Integer> rs){        if(maxM <= 0 || maxN <= 0){            return;        } else {            int x= startX, y = startY;            if(maxM ==1){                while (y < startY + maxN){                    rs.add(matrix[x][y]);                    y++;                }                return;            }            if(maxN ==1){                while (x < startX + maxM){                    rs.add(matrix[x][y]);                    x++;                }                return;            }            while (y <= startY + maxN-1){                rs.add(matrix[x][y]);                y++;            }            x = x + 1;            y = startY + maxN -1;            while (x <= startX + maxM-1){                rs.add(matrix[x][y]);                x++;            }            y = y-1;            x = startX + maxM -1;            while (y >= startY){                rs.add(matrix[x][y]);                y--;            }            x = x-1;            y = startY;            while (x > startX){                rs.add(matrix[x][y]);                x--;            }            getSpiralData(matrix,startX+1, startY+1, maxM-2, maxN-2, rs);        }    }
此外看到網上別的一種解法也蠻直觀的,通過控制每一步的方向,然後用額外的數組儲存是否訪問過,代碼如下:

http://www.cnblogs.com/remlostime/archive/2012/11/18/2775708.html

class Solution {private:    int step[4][2];    vector<int> ret;    bool canUse[100][100];public:    void dfs(vector<vector<int> > &matrix, int direct, int x, int y)    {        for(int i = 0; i < 4; i++)        {            int j = (direct + i) % 4;            int tx = x + step[j][0];            int ty = y + step[j][1];            if (0 <= tx && tx < matrix.size() && 0 <= ty && ty < matrix[0].size() && canUse[tx][ty])            {                canUse[tx][ty] = false;                ret.push_back(matrix[tx][ty]);                                dfs(matrix, j, tx, ty);                           }                    }    }        vector<int> spiralOrder(vector<vector<int> > &matrix) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        step[0][0] = 0;        step[0][1] = 1;        step[1][0] = 1;        step[1][1] = 0;        step[2][0] = 0;        step[2][1] = -1;        step[3][0] = -1;        step[3][1] = 0;        ret.clear();        memset(canUse, true, sizeof(canUse));        dfs(matrix, 0, 0, -1);                return ret;    }};



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.