摘要: 模拟 class Solution { public int[] spiralOrder(int[][] matrix) { if (matrix.length == 0){ return new int[0]; } int m = matrix.length; int n = matrix[0]. 阅读全文
posted @ 2022-02-17 16:29 振袖秋枫问红叶 阅读(22) 评论(0) 推荐(0)
摘要: 模拟 import java.util.ArrayList; import java.util.List; class Solution { public List<Integer> spiralOrder(int[][] matrix) { /** * 定义上下左右边界,每次循环一圈后都要缩小边界 阅读全文
posted @ 2022-02-17 16:14 振袖秋枫问红叶 阅读(24) 评论(0) 推荐(0)
摘要: 模拟 class Solution { public int[][] generateMatrix(int n) { /** * 定义上下左右边界,每次循环一圈后都要缩小边界 * 从1开始赋值 */ int[][] arr = new int[n][n]; int top = 0; int bott 阅读全文
posted @ 2022-02-17 15:50 振袖秋枫问红叶 阅读(24) 评论(0) 推荐(0)
摘要: 滑动窗口法 class Solution { public int totalFruit(int[] fruits) { /** * 滑动窗口 * 最多只能存在两种数字,right从1开始 * i和j记录最先遇到的两种数字,一旦发现第三种数字,就记录当前的长度 */ int left = 0; in 阅读全文
posted @ 2022-02-17 13:46 振袖秋枫问红叶 阅读(30) 评论(0) 推荐(0)
摘要: 双指针法 class Solution { public int[] sortedSquares(int[] nums) { /** * 先求出所有元素的平方,然后从两端双指针遍历 * 两端的元素总是最大的,逆序插入新的数组 */ for (int i = 0; i < nums.length; i 阅读全文
posted @ 2022-02-17 10:57 振袖秋枫问红叶 阅读(32) 评论(0) 推荐(0)
摘要: 栈 import java.util.Stack; class Solution { public boolean backspaceCompare(String s, String t) { /** * 将字符串压入栈,遇到#则弹出 */ Stack<Character> stack1 = new 阅读全文
posted @ 2022-02-17 09:57 振袖秋枫问红叶 阅读(31) 评论(0) 推荐(0)