国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

LeetCode 54. Spiral Matrix

2019-11-08 02:23:25
字體:
供稿:網(wǎng)友

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

For example,Given the following matrix:

[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]

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

answer:

class Solution {public:    vector<int> spiralOrder(vector<vector<int>>& matrix) {        vector<int> result;        if(matrix.empty()) return result;        int nLength = matrix[0].size(),mLength = matrix.size();        int n = nLength - 1;        int m = 0;//        cout << nLength << " " << mLength << endl;                while(nLength > 0 && mLength > 0){            for(int i = 0; i < nLength; i ++)                result.push_back(matrix[m][i + m]);                        for(int i = 1; i < mLength; i ++)                result.push_back(matrix[i + m][n]);            if(nLength == 1){                return result;            }            for(int i = nLength - 2; i >= 0 && mLength > 1; i --)                result.push_back(matrix[m + mLength - 1][i + m]);            for(int i = mLength - 2 ; i > 0; i --)                result.push_back(matrix[i + m][m]);            mLength -= 2;            nLength -= 2;            m ++;            n --;        }        return result;    }    };


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 堆龙德庆县| 临猗县| 马尔康县| 郸城县| 彰武县| 基隆市| 德令哈市| 马尔康县| 二手房| 广东省| 黎平县| 黑水县| 乌什县| 志丹县| 华阴市| 台江县| 榕江县| 西充县| 阳谷县| 大安市| 阳曲县| 会同县| 北宁市| 和平县| 江永县| 上林县| 许昌县| 罗江县| 长兴县| 益阳市| 郸城县| 德州市| 顺义区| 兴城市| 彭阳县| 河池市| 花莲市| 周宁县| 石棉县| 瑞金市| 沈丘县|