- 博客(21)
- 收藏
- 关注
原创 【并查集】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
189
原创 【贪心】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
130
原创 【贪心】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
134
原创 【贪心】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
1444
原创 【贪心】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
93
原创 【贪心】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
1813
原创 【贪心】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
87
原创 【贪心】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
88
原创 【字典序】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
1257
原创 【字典序】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
199
原创 【二叉树】124. 二叉树中的最大路径和
/** * 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 right) {..
2021-12-13 12:15:29
67
原创 【二叉树】最大的二叉搜索子树的大小
给定一棵二叉树的头节点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
286
原创 【二叉树】验证满二叉树(国内定义)
给定一棵二叉树的头节点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
611
原创 【二叉树】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
329
原创 【二叉树】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
76
原创 【二叉树】层序遍历判断二叉树是否是完全二叉树
完全二叉树(complete binary tree):定义 :若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。判断流程 :1.如果二叉树中某个节点有右孩子, 没有左孩子, 那么不是完全二叉树。2.如果遇到不双全的节点(有左孩子, 没有右孩子 || 没有左孩子,没有右孩子), 那么后面遍历的节点必须都是叶子节点(没有左右孩子), 否则不是完全二叉树。public class 判断是否完全二叉树 {
2021-12-11 17:06:53
1177
原创 【二叉树】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
346
原创 线程池之异常处理
关键接口UncaughtExceptionHandler : 自定义异常处理 //自定义异常处理器类,并实现接口 UncaughtExceptionHandler public static class ExceptionHandler implements Thread.UncaughtExceptionHandler { @Override public void uncaughtException(Thread t, Throwable e)
2020-07-31 08:37:54
406
原创 深入理解JDK1.7, 1.8 HashMap,ConcurrentHashMap源码
JDK1.7HashMap&ConcurrentHashMapHashMap类:HashMap继承了AbstaractMapAbstractMap实现了Map接口(AbstarctMap中实现了Map中常用/常见方法)1.成员变量DEFAULT_INITIAL_CAPACITY /** * 空参构造默认初始化容量16(1 << 4是16),必须是2的幂 */ static final int DEFAULT_INITIAL_CAPA
2020-07-24 16:03:38
320
原创 redis实现分布式锁,单机-集群-红锁
Redis实现分布式锁目录什么是分布式锁单机Redis实现分布式锁Redis分布式锁方案一Redis分布式锁方案二Redis分布式锁方案三Redis分布式锁方案四最近学习了Redis实现分布式锁,归纳整理一下目录什么是分布式锁我们知道锁是多线程中的基础概念,是单进程中访问共享资源的一种同步机制,即所有线程都在同一个JVM进程里的时候,使用Java语言提供的锁机制可以起到对共享资源进行同步...
2020-04-15 21:08:28
1594
3
原创 单例模式
单例模式引言优点:缺点:适应环境:代码演示:1.饥汉模式(饿汉模式)2.懒汉模式3.静态内部类方式4.枚举引言说到单例模式,肯定觉得简单啊,不就只有一个实例嘛,自私的家伙。确实很简单,但是现在我有几个问题1.单例模式有什么好处?2.单例模式有什么缺点?3.什么情况下适合用单例模式?4.如果我想只有两个实例,怎么办?优点:1.由于单例模式在内存中只有一个实例,减少内存开支,特别是一...
2020-04-06 10:50:01
219
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人