![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
败给你的黑色幽默丶
if i say you're the one would you believe me;
if i ask you to stay would you show me the way
展开
-
【回溯算法】
全排列import java.util.ArrayList;import java.util.LinkedList;import java.util.List;public class Main { List<List<Integer>> res = new ArrayList<>(); List<List<Integer>> permute(int[] nums){ List<Integer>原创 2022-01-07 18:49:29 · 47 阅读 · 0 评论 -
【动态规划】
Easy53. 最大子数组和https://leetcode-cn.com/problems/maximum-subarray/(1)题解1 效率较低class Solution { public int maxSubArray(int[] nums) { //1.定义dp[i] 为前i个元素中 连续子数组的最大和 int[] dp = new int[nums.length + 1]; //2.初始化 dp[1] =原创 2022-01-07 14:49:04 · 265 阅读 · 0 评论 -
【二分查找】
704. 二分查找https://leetcode-cn.com/problems/binary-search/class Solution { public int search(int[] nums, int target) { return search(nums,target,0,nums.length - 1); } private int search(int[] arr,int target,int low,int high){ if原创 2022-01-06 01:50:48 · 48 阅读 · 0 评论 -
编程小技巧
【或运算替代加法运算】使用说明:两个数做加法,如果这两个数的二进制之间没有1在相同位置上,可以用按位或运算;举例说明: 1 + 2 = 3; 0001 0010 ---- 0011 1000 0010 ---- 1010原创 2021-12-13 14:26:40 · 135 阅读 · 0 评论 -
【回溯】40. 组合总和 II
https://leetcode-cn.com/problems/combination-sum-ii/这题让我对回溯算法有了更深刻的理解;回溯算法的代码都是一个套路,但是要明白这个套路中每一部分到底在做什么事情class Solution { List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> combinationSum2(int原创 2021-12-07 14:04:47 · 48 阅读 · 0 评论 -
【快速幂问题】 372_超级次方
https://leetcode-cn.com/problems/super-pow/submissions/class Solution { int mod = 1337; public int superPow(int a, int[] b) { return dfs(a,b,b.length-1); } private int dfs(int a,int[] b,int len){ if(len == -1) return 1原创 2021-12-05 15:45:33 · 63 阅读 · 0 评论 -
【回溯】17. 电话号码的字母组合
https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/class Solution { List<String> res = new ArrayList<>(); private static HashMap<Character,String> phone = new HashMap<>(); static{ phone.pu原创 2021-12-02 21:59:26 · 47 阅读 · 0 评论 -
【回溯】全排列
import java.util.ArrayList;import java.util.LinkedList;import java.util.List;public class Main { List<List<Integer>> res = new ArrayList<>(); List<List<Integer>> permute(int[] nums){ List<Integer> op原创 2021-12-01 15:01:33 · 145 阅读 · 0 评论 -
【二叉树合集】
二叉树递归+全局变量563. 二叉树的坡度题解原创 2021-11-18 14:50:41 · 315 阅读 · 0 评论 -
563. 二叉树的坡度
DFS-后序遍历/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode ri原创 2021-11-18 14:49:30 · 272 阅读 · 0 评论 -
二叉树的深度
剑指 Offer 55 - I. 二叉树的深度https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof/BFS-层序/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x;原创 2021-11-17 18:03:01 · 185 阅读 · 0 评论 -
235. 二叉搜索树的最近公共祖先
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { //最近公共祖先 k //满足:p在k的左子树 q在k的右子树 //如果p和q在k的同一个子树下,k原创 2021-09-30 11:14:00 · 42 阅读 · 0 评论 -
102. 二叉树的层序遍历
层序遍历框架固定,先写出固定的框架;然后做些修改/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode l原创 2021-09-30 01:39:14 · 52 阅读 · 0 评论 -
翻转二叉树
https://leetcode-cn.com/problems/invert-binary-tree/submissions/递归法:class Solution { public TreeNode invertTree(TreeNode root) { if(root == null) return root; TreeNode left = invertTree(root.left); TreeNode right = invertTree(原创 2021-08-11 21:19:48 · 52 阅读 · 0 评论