国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 申扎县| 会泽县| 通山县| 阿城市| 台南县| 商城县| 余姚市| 九江县| 上饶市| 宜兰市| 孝义市| 米脂县| 沂南县| 且末县| 合江县| 潞城市| 清远市| 八宿县| 屏东市| 沈阳市| 龙井市| 宣化县| 当阳市| 周口市| 冷水江市| 南通市| 娄底市| 东海县| 柘荣县| 天门市| 泰兴市| 翁牛特旗| 东乡县| 朔州市| 鹿泉市| 通海县| 江永县| 岢岚县| 尉氏县| 亳州市| 大姚县|