随笔分类 - leetcode
leetcode刷题记录
摘要:import static java.lang.Integer.min; class Solution { public int minDistance(String word1, String word2) { int m = word1.length(); int n = word2.lengt
阅读全文
摘要:动态规划,还是学不会,,这道题好难啊!!!! 但是思路对了之后又是如此清晰。 public boolean isScramble(String s1, String s2) { if(s1.length() != s2.length()){ return false; } int len = s1.
阅读全文
摘要:public int maxProfit(int[] prices) { int n = prices.length; if(n<2){ return 0; } int[] f = new int[n]; int[] g = new int[n]; for(int i = 1,valley = pr
阅读全文
摘要:public int minCut(String s) { int n = s.length(); int[] f = new int[n+1]; boolean[][] p = new boolean[n][n]; for (int i = 0;i<= n;i++){ f[i] = n-1-i;
阅读全文
摘要:动态规划好巧妙啊啊啊啊啊啊啊 啊 int max =Integer.MIN_VALUE; public int maxPathSum(TreeNode root) { //用动态规划来求解 //DFS //根节点的值、左子树的值、右子树的值、 dfs(root); return max; } pri
阅读全文
摘要:class Solution { //定义一个数组,用于存放每一列的皇后放置的位置 List<List<String>> result = new ArrayList<>(); public List<List<String>> solveNQueens(int n) { //N皇后 int[] c
阅读全文
摘要:思路错一点点,努力都是白费。 public String reorganizeString(String S) { int N = S.length(); int[] counts = new int[26]; for (char c: S.toCharArray()) counts[c-'a']
阅读全文
摘要:由中序遍历得到数组 用递归构造平衡二叉树 List<Integer> list = new ArrayList<>(); public TreeNode balanceBST(TreeNode root) { //根据数组进行平衡二叉树的创建 inOrder(root); return buildB
阅读全文
摘要:虽然思路是差不多的,但是我自己写的话就越写越繁琐,不能抽丝剥茧言简意赅,最后成了一团乱麻。 那种精巧感还是没办法掌握,一道题解的思想是多么巧妙,但是我自己抽不出来那种主干。 虽菜犹学。 虽菜犹学…… List<Integer> flipped; int index; int[] voyage; pu
阅读全文
摘要:这道题做得比较顺利,但是效果并不是很好 public TreeNode buildTree(int[] preorder, int[] inorder) { if(preorder.length == 0){ return null; } if(preorder.length == 1){ retu
阅读全文
摘要:像这种DFS相关的题型,重点还是要知道如何进行标记。我就是卡在这里顺不下去,难过死了…… public int closedIsland(int[][] grid) { int sum = 0; for(int i = 1;i<grid.length;i++){ for(int j = 1;j<gr
阅读全文
摘要:public int[] pondSizes(int[][] land) { List<Integer> list = new ArrayList<>(); int temp; for (int i = 0; i < land.length; i++) { for (int j = 0; j < l
阅读全文
摘要:public TreeNode recoverFromPreorder(String S) { Stack<TreeNode> path = new Stack<>(); //构建好栈 //定义一个变量来遍历S int i = 0; //定义一个int变量来确定节点的层数 //先将S转换成char[
阅读全文
摘要:public void flatten(TreeNode root) { if(root == null){ return; }else{ if(root.left == null){ flatten(root.right); }else{ TreeNode node = root; TreeNod
阅读全文
摘要:private final static int MAXVALUE = 1000; public int networkDelayTime(int[][] times, int N, int K) { boolean[] visited = new boolean[N+1]; int[][] tim
阅读全文
摘要:class Solution { public boolean isBalanced(TreeNode root) { //判断一个树是不是平衡二叉树 //应该是用递归了吧,判断左右子树的高度相差是不是超过1 //但是如何记录树的高度呢,左右子树的高度分别记录的话,应该如何进行呢? if(root
阅读全文
摘要:public boolean leafSimilar(TreeNode root1, TreeNode root2) { //要想找到两棵树的叶子节点的值是不是一样,就得通过遍历判断来得到,同时存储在一个数组中 ArrayList<Integer> leaf1 = new ArrayList<>()
阅读全文
摘要:import java.util.LinkedList; import java.util.Queue; class Solution { public boolean hasValidPath(int[][] grid) { int[][] visited = new int[grid.lengt
阅读全文
摘要:public void nextPermutation(int[] nums) { int i = nums.length -2; while(i>=0 && nums[i+1] <= nums[i]){ i--; } if(i>=0){ int j = nums.length -1; while(
阅读全文
摘要:class Solution { public ListNode deleteNode(ListNode head, int val) { ListNode ln = head; ListNode p = new ListNode(); p.next = ln; head = p; while(ln
阅读全文

浙公网安备 33010602011771号