随笔分类 - leetCode
摘要:class Solution { public String longestDupSubstring(String S) { int n = S.length(); int[] nums = new int[n]; // 字符串转为数组 for (int i = 0; i < n; i++) { n
阅读全文
摘要:二分查找 class Solution { public int superEggDrop(int K, int N) { return dp(K, N); } Map<Integer, Integer> memo = new HashMap<>(); private int dp(int K, i
阅读全文
摘要:均分最大深度 遍历seq计算最大深度maxDepth遍历seq,遇到"(",当A子序列的最大深度小于ceil(maxDepth/2)时分配给A,反之分配给B遇到")", 当A的最大深度不为0时说明A子序列还有未被匹配的"(",所以分配给A,否则分配给B class Solution { public
阅读全文
摘要:回溯法 每使用一个字符就往栈中添加一个字符,如果该字符已经使用过则跳过该字符。回溯时从栈中删除字符。 class Solution { public String[] permutation(String S) { if (S.length() == 0) return new String[]{}
阅读全文
摘要:回溯 class Solution { private List<List<Integer>> ans = new LinkedList<>(); public List<List<Integer>> permute(int[] nums) { backTrace(nums, 0); return
阅读全文
摘要:回溯法 class Solution { // 一共9行,一行9个数字 private boolean[][] line = new boolean[9][9]; // 一共9列,一列9个数字 private boolean[][] column = new boolean[9][9]; // 一共
阅读全文
摘要:class Solution { public List<String> generateParenthesis(int n) { List<String> ans = new LinkedList<>(); backtrack(ans, n, 0, ""); return ans; } // 回溯
阅读全文
摘要:回溯法 class Solution { static final int SEC_COUNT = 4; List<String> ans = new ArrayList<>(); int[] segments = new int[SEC_COUNT]; public List<String> re
阅读全文
摘要:排序 + 双指针 思路: 先将数组排序,再从数组中取出一个元素。使用两个指针从该元素之后取出一小一大两个元素,如果当前三个元素相加不为0,那么调整两个指针的位置继续求和。如果为0则将三个元素加入结果中。 class Solution { public List<List<Integer>> thre
阅读全文
摘要:暴力法 class Solution { public int[] corpFlightBookings(int[][] bookings, int n) { int[] ans = new int[n]; for (int i = 0; i < bookings.length; i++) { fo
阅读全文
摘要:动态规划 使用一个二维数组记录每个位置的最大宽度,这样就能在线性时间内计算得到以该点为右下角的最大矩形。 class Solution { public int maximalRectangle(char[][] matrix) { if (matrix.length == 0) return 0;
阅读全文
摘要:动态规化 使用两个数组保存左边数字的最大值与右边数字的最大值遍历数组中每个元素,该元素所在位置能接的雨水量为该元素左边界和右边界的最小值减去该位置柱子的高度 class Solution { public int trap(int[] height) { if (height == null ||
阅读全文