![](https://img-blog.csdnimg.cn/20190927151101105.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
算法
学习算法与数据结构
ddl是第一生产力
这个作者很懒,什么都没留下…
展开
-
子集 回溯算法
采用回溯算法,每个数有两种情况,选中或不选中 class Solution{ public: //结果集 vector<vector<int>> ans; //临时数组 vector<int> path; vector<vector<int>> subsets(vector<int> &nums){ dfs(nums,0); return ans; } void dfs(vector<int>.原创 2020-09-20 10:44:24 · 133 阅读 · 0 评论 -
Leetcode 216. 组合总和 III
回溯算法,按照之前的思路我列了一个1-9的数组 class Solution { List<List<Integer>> res=new ArrayList<>(); int [] candidates={1,2,3,4,5,6,7,8,9}; public List<List<Integer>> combinationSum3(int k, int n) { if(n==0) return res; .原创 2020-09-13 15:35:14 · 59 阅读 · 0 评论 -
Leetcode 40.组合总数Ⅱ
相较于组合总和,虽然也是回溯算法,但是不能一个数字使用多次,在看了评论区的答案后需要加上一行判断代码并修改一些小点。 class Solution { List<List<Integer>> res=new ArrayList<>(); public List<List<Integer>> combinationSum2(int[] candidates, int target) { Arrays.sort(can.原创 2020-09-13 15:25:19 · 105 阅读 · 0 评论 -
Leetcode 39 组合总和
此题为经典回溯算法 class Solution { List<List<Integer>> res=new ArrayList<>(); public List<List<Integer>> combinationSum(int[] candidates, int target) { //对数组从小到大进行排序 Arrays.sort(candidates); //设置路径List.原创 2020-09-13 15:14:34 · 56 阅读 · 0 评论 -
Leetcode 79 单词搜索
Leetcode 79 单词搜索 此题运用dfs深搜+回溯算法 class Solution { public boolean exist(char[][] board, String word) { int row=board.length,col=board[0].length; for(int i=0;i<row;i++){ for(int j=0;j<col;j++){ //找到第一个字符相等 if(word.charAt(0)==board[i][原创 2020-09-13 15:00:15 · 62 阅读 · 0 评论 -
946 验证栈的序列
946 验证栈的序列 class Solution { public boolean validateStackSequences(int[] pushed, int[] popped) { //pushed数组的长度 int len=pushed.length; //popped数组指针 int j=0; Stack<Integer> stack=new Stack<>(); for(int i=0;i<len;i++){ //压栈原创 2020-09-06 15:17:15 · 59 阅读 · 0 评论 -
111. 二叉树的最小深度
二叉树的最小深度 此题和最大深度不同,根据题目描述,二叉树的最小深度为叶节点的最短路径上的节点数量,在1,2这个测试用例中 最大深度为2,最大深度不为1,因此有下列三种情况: ●当左右子树均为空时,则返回1。 ●当左右子树为有一个为空时,返回不为空子树的最小深度。 ●当左右子树均不为空时,返回左右子树中的最小深度。 代码如下: /** * Definition for a binary tree node. * public class TreeNode { * int val; *原创 2020-08-22 10:51:28 · 110 阅读 · 0 评论 -
LeetCode 121.买卖股票的最佳时机
LeetCode 121.买卖股票的最佳时机 解题技巧 第i天最大利润=Max(前i-1天最大利润,第i天股价-前i-1天中的最低股价) class Solution{ public int maxProfit(int[] prices){ if(prices.length<=1) return 0; int min=prices[0]; //保存前i-1天的最低利润 int max=0;//保存第i天的最大利润 for(int i=1;i<prices.length;i+原创 2020-07-11 21:10:12 · 84 阅读 · 0 评论 -
LeetCode 26.树的子结构
LeetCode 26.树的子结构 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution{ public boolean isSubStructure(TreeNode A,Tree原创 2020-07-09 20:54:34 · 60 阅读 · 0 评论 -
LeetCode 53.最大子序和
LeetCode 53.最大子序和 两种做法 class Solution{ public int maxSubArray(int[] nums){ int pre=0;//用来记录和,注意要初始化为0 int res=nums[0];//用来记录要返回的值,注意要初始化为nums[0] /** * 当nums数组中只有两个元素时,且nums[0]<nums[1],(例如[1,2]),经过推导可以得知,应从下标为0的元素开始 */ for(int i=0;i<n原创 2020-07-09 15:08:02 · 65 阅读 · 0 评论 -
探索初级算法——删除排序数组中的重复项
探索初级算法——删除排序数组中的重复项 题目要求: 删除排序数组中的重复项 给定一个排序数组,你需要在 原地 删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。 不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。 示例1 给定数组 nums = [1,1,2], 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。 你不需要考虑数组中超出新长度后面的元素。 示例2 给定 nums = [0,0,1,1,1,2,2,原创 2020-07-04 17:09:22 · 102 阅读 · 0 评论 -
LeetCode 206.反转链表
LeetCode 206. 反转链表 定义结点类 public class ListNode{ int val; ListNode next; } 详细图解https://leetcode-cn.com/problems/reverse-linked-list/solution/dong-hua-yan-shi-206-fan-zhuan-lian-biao-by-user74/. 1.前插法 1) 头指针 public ListNodereverse(ListNode head){ ListNod原创 2020-06-14 20:43:41 · 77 阅读 · 0 评论 -
LeetCode 965.单值二叉树
今日算法 LeetCode 965. 单值二叉树 定义树节点 public class ThreeNode{ int val; //值 TreeNode left; //左孩子 TreeNode right; //右孩子 } 具体实现 public static boolean isUnivalTree(TreeNode root){ if(root==null) return true; //结点为空 if(root.left!=null&&root.left.val!=ro原创 2020-06-13 11:43:35 · 136 阅读 · 0 评论