![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法学习
胭脂绿
这个作者很懒,什么都没留下…
展开
-
【并查集】547. 省份数量
class Solution { public int findCircleNum(int[][] isConnected) { UnionFind uf = new UnionFind(isConnected.length); for(int i = 0 ; i < isConnected.length ; i++){ for(int j = i + 1 ; j < isConnected[0].length ; j++){..原创 2021-12-20 14:14:02 · 136 阅读 · 0 评论 -
【贪心】121.
class Solution { public int maxProfit(int[] prices) { int min = Integer.MAX_VALUE; int maxProfit = 0; for(int i = 0 ; i < prices.length ; i++){ //找出最小值 if(prices[i] < min){ min = p..原创 2021-12-18 23:06:39 · 81 阅读 · 0 评论 -
【贪心】122.II
class Solution { public int maxProfit(int[] prices) { int sum = 0; for(int i = 0 ; i < prices.length - 1; i++){ if(prices[i] < prices[i + 1]) sum += prices[i + 1] - prices[i]; } return sum; }}..原创 2021-12-18 22:24:32 · 82 阅读 · 0 评论 -
【贪心】45. 跳跃游戏 II
class Solution { public int jump(int[] nums) { int max = 0; int end = 0; int count = 0; //这里要注意一个细节,就是 for 循环中,i < nums.length - 1,少了末尾。 //因为开始的时候边界是第 00 个位置,steps 已经加 1 了。 //如果最后一步刚好跳到了末尾,此时 steps ..原创 2021-12-18 21:58:16 · 1394 阅读 · 0 评论 -
【贪心】135. 分发糖果
class Solution { public int candy(int[] ratings) { int [] left = new int [ratings.length]; Arrays.fill(left,1); for(int i = 1 ; i < ratings.length ; i++){ if(ratings[i] > ratings[i-1]){ left[..原创 2021-12-18 21:04:32 · 66 阅读 · 0 评论 -
【贪心】55. 跳跃游戏
class Solution { public boolean canJump(int[] nums) { boolean ans = true; int max = 0; for(int i = 0 ; i < nums.length ; i++){ if(max < i) ans = false; max = Math.max(max,nums[i] + i); } ..原创 2021-12-18 20:18:23 · 1768 阅读 · 0 评论 -
【贪心】134. 加油站
class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { int min = Integer.MAX_VALUE; int start = 0; int total = 0; for(int i = 0 ; i < gas.length ; i++){ total += gas[i] - cost[i]; ...原创 2021-12-18 17:51:39 · 57 阅读 · 0 评论 -
【贪心】179. 最大数
class Solution { public String largestNumber(int[] nums) { String [] arr = new String[nums.length]; for(int i = 0 ; i < nums.length ; i++) arr[i] = "" + nums[i]; Arrays.sort(arr,(a,b)->{ //字典序降序排列 ..原创 2021-12-18 17:36:48 · 49 阅读 · 0 评论 -
【字典序】440. 字典序的第K小数字
使用字典序排数的暴力解法会提示超时class Solution { public int findKthNumber(int n, int k) { //起始位置cur 为1 , k为0 long cur = 1; // 当前遍历到的数字,从1(根)出发 k = k - 1; // 从1出发开始往后按字典序从小到大的顺序走k-1步到达的就是 字典序的第K小数字 while(k > 0){ int nodes.原创 2021-12-15 17:56:52 · 1207 阅读 · 0 评论 -
【字典序】386. 字典序排数
class Solution { public List<Integer> lexicalOrder(int n) { List<Integer> ans = new ArrayList(); for(int i = 1; i<=9 ;i++){ dfs(i,n,ans); } return ans; } public void dfs(int i , int.原创 2021-12-15 17:45:07 · 172 阅读 · 0 评论 -
【二叉树】最大的二叉搜索子树的大小
给定一棵二叉树的头节点head,返回这颗二叉树中最大的二叉搜索子树的大小方法1: public static class Node { public int value; public Node left; public Node right; public Node(int data) { this.value = data; } } public static int 最原创 2021-12-12 11:48:58 · 249 阅读 · 0 评论 -
【二叉树】验证满二叉树(国内定义)
给定一棵二叉树的头节点head,返回这颗二叉树是不是满二叉树满二叉树(Full Tree):国内定义 : 一个二叉树,如果每一个层的结点数都达到最大值,则这个二叉树就是满二叉树。也就是说,如果一个二叉树的层数为K,且结点总数是(2^k) -1 ,则它就是满二叉树。public class 判断是否满二叉树 { public static class Node { public int value; public Node left; public原创 2021-12-11 22:33:43 · 521 阅读 · 0 评论 -
【二叉树】110. 平衡二叉树
给定一个二叉树,判断它是否是高度平衡的二叉树。本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。tips : 1.平衡树的左右子树都是平衡树 。2.空树是平衡树。下图不是平衡树/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; *原创 2021-12-11 21:09:44 · 297 阅读 · 0 评论 -
【二叉树】98. 验证二叉搜索树
给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。有效 二叉搜索树定义如下:节点的左子树只包含 小于 当前节点的数。节点的右子树只包含 大于 当前节点的数。所有左子树和右子树自身必须也是二叉搜索树。来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/validate-binary-search-tree/** * Definition for a binary tree node. * public class Tr原创 2021-12-11 20:18:27 · 55 阅读 · 0 评论 -
【二叉树】层序遍历判断二叉树是否是完全二叉树
完全二叉树(complete binary tree):定义 :若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。判断流程 :1.如果二叉树中某个节点有右孩子, 没有左孩子, 那么不是完全二叉树。2.如果遇到不双全的节点(有左孩子, 没有右孩子 || 没有左孩子,没有右孩子), 那么后面遍历的节点必须都是叶子节点(没有左右孩子), 否则不是完全二叉树。public class 判断是否完全二叉树 {原创 2021-12-11 17:06:53 · 1080 阅读 · 0 评论 -
【二叉树】515. 在每个树行中找最大值
在每个树行中找最大值给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeN原创 2021-12-09 16:33:16 · 300 阅读 · 0 评论