题解 | #螺旋矩阵#C#

螺旋矩阵

http://www.nowcoder.com/practice/7edf70f2d29c4b599693dc3aaeea1d31

using System.Collections.Generic;


class Solution {
 
    public List<int> spiralOrder (List<List<int>> matrix) {
        // write code here
        List<int> res = new List<int>();
        if (matrix != null && matrix.Count == 0) return res;
        int upBorder = 0,downBorder = matrix.Count-1,
        leftBorder = 0,rightBorder = matrix[0].Count-1;
        while (upBorder <= downBorder && leftBorder <= rightBorder)
        {
            for (int i = leftBorder; i <= rightBorder; i++)
                res.Add(matrix[upBorder][i]);
            upBorder++;
            if (upBorder > downBorder) break;
            for (int i = upBorder; i <= downBorder; i++)
                res.Add(matrix[i][rightBorder]);
            rightBorder--;
            if (leftBorder > rightBorder) break;
            for (int i = rightBorder; i >= leftBorder; i--)
                res.Add(matrix[downBorder][i]);
            downBorder--;
            if (upBorder > downBorder) break;
            for (int i = downBorder; i >= upBorder; i--)
                res.Add(matrix[i][leftBorder]);
            leftBorder++;
            if (leftBorder > rightBorder) break;
        }
        return res;
        
    }
}
全部评论

相关推荐

dongsheng66:如果想进大厂的话,在校经历没必要占这么大篇幅,可以把专业技能单独放一个专栏写,可以加个项目经历
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务