LeetCode 54 - Spiral Matrix

Brush title errors when school recruit

After 1. AC uploaded to Github on the matter

Very low cost, the solution does not mean that other people understand their true digestion, brushing does not reflect summarized easily forgotten, just remember that look submissions AC before, but the idea is completely not remember. After / biweekly week / month, to be the subject of a recent AC whiteboard program review, Reviewing the Old, the subject of testing standards is done whiteboard bug-free AC.

2. Type of test cases is not fully covered

Stick 防御性编程up corner case to consider which of these categories, so the question is bound to consider than a good row vector, column vector, matrix, general matrix. In addition, dig out potential points of topics, such questions than have symmetry.

Summary and Reflection

Own AC Solution
class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> result = new ArrayList<>();
        if (matrix == null || matrix.length == 0) {
            return result;
        }
        int m = matrix.length, n = matrix[0].length;
        for (int k = 0; k < (Math.min(m, n) + 1) / 2; ++k) {
            int j = 0;
            for (j = k; j < n - k; ++j) {
                result.add(matrix[k][j]);
            }
            for (j = k + 1; j < m - k; ++j) {
                result.add(matrix[j][n - k - 1]);
            }
            if (m - k - 1 > k) {
                for (j = n - k - 2; j >= k; --j) {
                    result.add(matrix[m - k - 1][j]);
                }
            }
            if (n - k - 1 > k){
                for (j = m - k - 2; j > k; --j) {
                    result.add(matrix[j][k]);
                }
            }
        }
        return result;
    }
}
Nine chapters of the Solution
class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> result = new ArrayList<>();
        if (matrix == null || matrix.length == 0) {
            return result;
        }
        int m = matrix.length, n = matrix[0].length;
        int x = 0, y = 0;
        while (m > 0 && n > 0) {
            if (m == 1) {
                for (int i = 0; i < n; ++i) {
                    result.add(matrix[x][y++]);
                }
                break;
            } else if (n == 1) {
                for (int i = 0; i < m; ++i) {
                    result.add(matrix[x++][y]);
                }
                break;
            }
            for (int i = 0; i < n - 1; ++i) {
                result.add(matrix[x][y++]);
            }
            for (int i = 0; i < m - 1; ++i) {
                result.add(matrix[x++][y]);
            }
            for (int i = 0; i < n - 1; ++i) {
                result.add(matrix[x][y--]);
            }
            for (int i = 0; i < m - 1; ++i) {
                result.add(matrix[x--][y]);
            }
            x++;
            y++;
            m = m - 2;
            n = n - 2;
        }
        return result;
    }
}
Summary and Reflection

Although he wrote the AC code shorter, but more like an outsider by observing the law written code. Nine chapters of the algorithm is more in line with computational thinking through ( x , Y ) (X, y) recording start point coordinates, by m m and n n controls the number of steps in the interview easier to write bug-free. Own code in case of a forgotten interview i f if directly gg up.

Also remember some of the devil / unusual ideas in interviews is risky, not all interviewers are like Pony.ai ACM great God sits there, you can quickly understand the idea, most of the company's line of business development interviewers are temporary ready. So play safe, master general idea is to first pick.

Published 11 original articles · won praise 2 · Views 667

Guess you like

Origin blog.csdn.net/liheng301/article/details/104946578