LeetCode73(2021.3.21)

LC73. 矩阵置零

class Solution {
    public void setZeroes(int[][] matrix) {
        boolean fstColumn = false;
        int row = matrix.length;
        int col = matrix[0].length;
        for (int i = 0; i < row; ++i) {
            if (matrix[i][0] == 0) {
                fstColumn = true;
            }
            for (int j = 1; j < col; ++j) {
                if (matrix[i][j] == 0) {
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }
        for (int i = 1; i < col; ++i) {
            if (matrix[0][i] == 0) {
                for (int j = 1; j < row; ++j) {
                    matrix[j][i] = 0;
                }
            }
        }
        for (int i = 0; i < row; ++i) {
            if (matrix[i][0] == 0) {
                for (int j = 1; j < col; ++j) {
                    matrix[i][j] = 0;
                }
            }
        }
        if (fstColumn) {
            for (int i = 0; i < row; ++i) {
                matrix[i][0] = 0;
            }
        }
    }
}

LeetCode150(2021.3.20)

LC150. 逆波兰表达式求值

class Solution {
    public int evalRPN(String[] tokens) {
        Deque<Integer> stack = new LinkedList<Integer>();
        for (int i = 0; i < tokens.length; ++i) {
            String item = tokens[i];
            if ("+".equals(item)) {
                int num2 = stack.pop();
                int num1 = stack.pop();
                stack.push(num1 + num2);
            }
            else if ("-".equals(item)) {
                int num2 = stack.pop();
                int num1 = stack.pop();
                stack.push(num1 - num2);
            }
            else if ("*".equals(item)) {
                int num2 = stack.pop();
                int num1 = stack.pop();
                stack.push(num1 * num2);
            }
            else if ("/".equals(item)) {
                int num2 = stack.pop();
                int num1 = stack.pop();
                stack.push(num1 / num2);
            }
            else {
                stack.push(Integer.parseInt(item));
            }
        }
        return stack.pop();
    }
}

LeetCode1603(2021.3.19)

LC1603. 设计停车系统

class ParkingSystem {

    private int big;
    private int medium;
    private int small;


    public ParkingSystem(int big, int medium, int small) {
        this.big = big;
        this.medium = medium;
        this.small = small;
    }

    public boolean addCar(int carType) {
        if (carType == 1) {
            if (big > 0) {
                big--;
                return true;
            }
            else {
                return false;
            }
        }
        else if (carType == 2) {
            if (medium > 0) {
                medium--;
                return true;
            }
            else {
                return false;
            }
        }
        else {
            if (small > 0) {
                small--;
                return true;
            }
            else {
                return false;
            }
        }
    }
}

LeetCode92(2021.3.18)

LC92. 反转链表 II

class Solution {
    private void reverseLinkedList(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
    }

    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode dummyNode = new ListNode(-1);
        dummyNode.next = head;
        ListNode pre = dummyNode;
        for (int i = 0; i < left - 1; i++) {
            pre = pre.next;
        }
        ListNode rightNode = pre;
        for (int i = 0; i < right - left + 1; i++) {
            rightNode = rightNode.next;
        }

        //截取链表
        ListNode leftNode = pre.next;
        ListNode curr = rightNode.next;
        pre.next = null;
        rightNode.next = null;

        //反转链表
        reverseLinkedList(leftNode);

        pre.next = rightNode;
        leftNode.next = curr;
        return dummyNode.next;
    }
}

LeetCode115(2021.3.17)

LC115. 不同的子序列

class Solution {
    public int numDistinct(String s, String t) {
        int ls = s.length(), lt = t.length();
        if (ls < lt) {
            return 0;
        }
        int[][] dp = new int[ls + 1][lt + 1];
        for (int i = 0; i <= ls; ++i) {
            dp[i][lt] = 1;
        }
        for (int i = ls - 1; i >= 0; --i) {
            for (int j = lt - 1; j >= 0; --j) {
                if (s.charAt(i) == t.charAt(j)) {
                    dp[i][j] = dp[i + 1][j] + dp[i + 1][j + 1];
                }
                else {
                    dp[i][j] = dp[i + 1][j];
                }
            }
        }
        return dp[0][0];
    }
}

LeetCode59(2021.3.16)

LC59. 螺旋矩阵 II

class Solution {
    public int[][] generateMatrix(int n) {
        int maxNum = n * n, curNum = 1;
        int[][] matrix = new int[n][n];
        int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        int row = 0, column = 0, directionIndex = 0;
        while (curNum <= maxNum) {
            matrix[row][column] = curNum;
            curNum++;
            int nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];
            if (nextRow < 0 || nextRow >= n || nextColumn < 0 || nextColumn >= n || matrix[nextRow][nextColumn] != 0) {
                directionIndex = (directionIndex + 1) % 4; //顺时针旋转
            }
            row += directions[directionIndex][0];
            column += directions[directionIndex][1];
        }
        return matrix;
    }
}

LeetCode54(2021.3.15)

LC54. 螺旋矩阵

class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> order = new ArrayList<Integer>();
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return order;
        }
        int rows = matrix.length, columns = matrix[0].length;
        boolean[][] visited = new boolean[rows][columns];
        int total = rows * columns;
        int row = 0, column = 0;
        int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        int directionIndex = 0;
        for (int i = 0; i < total; ++i) {
            order.add(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]) {
                directionIndex = (directionIndex + 1) % 4;
            }
            row += directions[directionIndex][0];
            column += directions[directionIndex][1];
        }
        return order;
    }
}

LeetCode706(2021.3.14)

LC706. 设计哈希映射

class MyHashMap {

    /**
     * Initialize your data structure here.
     */
    ArrayList<Integer> data;

    public MyHashMap() {
        data = new ArrayList<Integer>();
        for (int i = 0; i < 1000010; ++i) {
            data.add(-1);
        }
    }

    /**
     * value will always be non-negative.
     */
    public void put(int key, int value) {
        data.set(key, value);
    }

    /**
     * Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key
     */
    public int get(int key) {
        return data.get(key);
    }

    /**
     * Removes the mapping of the specified value key if this map contains a mapping for the key
     */
    public void remove(int key) {
        data.set(key, -1);
    }
}