2020-06-05学习随笔

序言

学习随笔是本人对自己一天的自学回顾,由于本人是非科班出身,不懂很多cs的专业术语,所以难免会有些错误,望各位批评指正,本人定当悉心接受并立即改正。希望自己能够慢慢坚持下去,坚持转行的道路,坚持每天学习的输出。

刷题篇

LeetCode

1.题目

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。

2.大致思路

感觉自己挺笨的,连“剥洋葱”这样一层一层的剥都没发现。

我感觉关键在于转向和矩阵的缩小。

转向:其实就是向左,向右,向上,向下

矩阵的缩小:就是最右端-1,最下端-1,最左端+1,最上端-1

3.代码(由于想走后端,所以现在开始适应用Java写)

下列两串代码均来自官方,第一次使用Java还不适应,需慢慢调整

class Solution {
    public int[] spiralOrder(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            //判断是否为空矩阵
            return new int[0];      //注意返回
        }
        int rows = matrix.length, columns = matrix[0].length;
        //rows为行,即matrix.length,columns为列,即matrix[0].length
        boolean[][] visited = new boolean[rows][columns];
        //visited 是判断数组元素是否打印过的矩阵
        int total = rows * columns;         //元素的个数
        int[] order = new int[total];       //用来存储打印的数组元素
        int row = 0, column = 0;            //初始化
        int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        /*
        第一行,是向右走,即行数不动,列数+1
        第二行,是向下走,即列数不动,行数+1
        第三行,是向左走,即行数不动,列数-1
        第四行,是向上走,即列数不动,行数-1
        */
        int directionIndex = 0;
        for (int i = 0; i < total; i++) {
            order[i] = matrix[row][column];
            visited[row][column] = true;
            int nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];
            if (nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || visited[nextRow][nextColumn]) {
            //判断是否走到头了或者是否来过(visited[nextRow][nextColumn]),boolean未赋值时,为false
                directionIndex = (directionIndex + 1) % 4;      //转向
            }
            row += directions[directionIndex][0];
            column += directions[directionIndex][1];
        }
        return order;
    }
}
class Solution {
    public int[] spiralOrder(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return new int[0];
        }
        int rows = matrix.length, columns = matrix[0].length;
        int[] order = new int[rows * columns];
        int index = 0;
        int left = 0, right = columns - 1, top = 0, bottom = rows - 1;
        while (left <= right && top <= bottom) {
            for (int column = left; column <= right; column++) {
                order[index++] = matrix[top][column];
            }
            for (int row = top + 1; row <= bottom; row++) {
                order[index++] = matrix[row][right];
            }
            if (left < right && top < bottom) {
        // 本以为这个限制条件可以不用,但如果是只有数组一列的话,会出现越界的情况
                for (int column = right - 1; column > left; column--) {
                    order[index++] = matrix[bottom][column];
                }
                for (int row = bottom; row > top; row--) {
                    order[index++] = matrix[row][left];
                }
            }
            left++;
            right--;
            top++;
            bottom--;
        }
        return order;
    }
}

更简洁的代码(看懂了上面自然就看得懂这个)

class Solution {
    public int[] spiralOrder(int[][] matrix) {
        if (matrix.length == 0) {
            return new int[0];
        }
        int[] res = new int[matrix.length * matrix[0].length];
        int u = 0, d = matrix.length - 1, l = 0, r = matrix[0].length - 1;
        int idx = 0;
        while (true) {
            for (int i = l; i <= r; i++) {
                res[idx++] = matrix[u][i];
            }
            if (++u > d) {
                break;
            }
            for (int i = u; i <= d; i++) {
                res[idx++] = matrix[i][r];
            }
            if (--r < l) {
                break;
            }
            for (int i = r; i >= l; i--) {
                res[idx++] = matrix[d][i];
            }
            if (--d < u) {
                break;
            }
            for (int i = d; i >= u; i--) {
                res[idx++] = matrix[i][l];
            }
            if (++l > r) {
                break;
            }
        }
        return res;
    }
}

4.多说两句

转行的事因为某些原因好像要和导师产生矛盾。

因为我是第一所以我的一言一语都可能影响到同班同学,要慎言慎行。

具体情况,昨晚已经写好了两千字的作文,算是记录,也是警醒。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。