diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode283.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode283.java new file mode 100644 index 0000000..454cbd4 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode283.java @@ -0,0 +1,71 @@ +package cn.whaifree.leetCode; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/11/1 14:24 + * @注释 + */ +public class LeetCode283 { + + @Test + public void test() { + int[] nums = {0, 4, 0, 3, 12, 0}; + new Solution().moveZeroes(nums); + for (int num : nums) { + System.out.print(num + " "); + } + } + + class Solution { + public void moveZeroes(int[] nums) { + + + for (int i = 1; i < nums.length; i++) { + // 插入排序 + for (int j = i; j > 0; j--) { + if (nums[j - 1] == 0) { + swap(nums, j, j - 1); + } else if (nums[j] != 0 && nums[j - 1] == 0) { // 遇到非0就往前插入 + swap(nums, j, j - 1); + } else { + break; + } + } + } + } + + public void swap(int[] nums, int i, int j) { + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } + + } + + @Test + public void test1() { + int[] nums = {0, 4, 0, 3, 12, 0}; + new Solution1().moveZeroes(nums); + for (int num : nums) { + System.out.print(num + " "); + } + } + + class Solution1 { + public void moveZeroes(int[] nums) { + int left = 0; + for (int i = 0; i < nums.length; i++) { + if (nums[i] != 0) { + nums[left] = nums[i]; + left++; + } + } + for (int i = left; i < nums.length; i++) { + nums[i] = 0; + } + } + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode560.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode560.java new file mode 100644 index 0000000..5677a1f --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode560.java @@ -0,0 +1,96 @@ +package cn.whaifree.leetCode; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/11/1 15:17 + * @注释 + */ +public class LeetCode560 { + + @Test + public void test() { + int[] nums = {-1, -1, 1}; + int k = 0; + + int res = new Solution().subarraySum(nums, k); + System.out.println(res); + } + + class Solution { + /** + * -2 -5 1 2 + * + * -2 -7 -6 -4 + * 子数组(连续)的个数 。 + * + * 前缀和,前缀和的区间总面积为k的个数 + * + * @param nums + * @param k + * @return + */ + public int subarraySum(int[] nums, int k) { + int len = nums.length; + + int[] preSum = new int[len + 1]; + for (int i = 0; i < len; i++) { + preSum[i + 1] = preSum[i] + nums[i]; + } + + + int count = 0; + for (int left = 0; left < len; left++) { + for (int right = left; right < len; right++) { + // 区间和 [left..right],注意下标偏移 + if (preSum[right + 1] - preSum[left] == k) { + count++; + } + } + } + return count; + + } + + /** + * 子数组(连续)的个数 。 + * + * 前缀和,前缀和的区间总面积为k的个数 + * + * + * 1. 每次计算的元素n,在前面计算好前缀和,只要找到k-n的前缀和的个数 + * --- 计算每个前缀和的数量, + * + * + * @param nums + * @param k + * @return + */ + public int subarraySum1(int[] nums, int k) { + Map map = new HashMap<>(); + map.put(0, 1); + // map为各种前缀和的数量 + int preSum = 0; + int res = 0; + for (int num : nums) { + preSum += num; + // 画图,就是一个大的面积,找到小的面积,取其中的差值区域;即找到一个范围==k + // | preSum-k | k | + // 整个是preSum + // PreSum当前的前缀和-某个之前的前缀和=k,而map存储之前前缀和k的个数为v个 + if (map.containsKey(preSum - k)) { + res += map.get(preSum - k); + } + map.put(preSum, map.getOrDefault(preSum, 0) + 1); + } + + return res; + + } + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode1005.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode1005.java new file mode 100644 index 0000000..769226b --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode1005.java @@ -0,0 +1,45 @@ +package cn.whaifree.redo.redo_all_241016; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/11/1 14:08 + * @注释 + */ +public class LeetCode1005 { + + @Test + public void test() { + int[] nums = {4,-5,4,-5,9,4,5}; + int k = 1; + int result = new Solution().largestSumAfterKNegations(nums, k); + System.out.println(result); + } + + class Solution { + public int largestSumAfterKNegations(int[] nums, int k) { + Arrays.sort(nums); + + int res = 0; + int minAbs = Integer.MAX_VALUE; + for (int i = 0; i < nums.length; i++) { + if (nums[i] < 0 && k > 0) { + nums[i] = -nums[i]; + k--; + } + minAbs = Math.min(minAbs, nums[i]); + res += nums[i]; + } + if (k % 2 == 0) { + // 偶数 + return res; + }else { + return res - minAbs * 2; + } + } + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode123.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode123.java new file mode 100644 index 0000000..47b39c3 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode123.java @@ -0,0 +1,48 @@ +package cn.whaifree.redo.redo_all_241016; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/31 12:03 + * @注释 + */ +public class LeetCode123 { + + class Solution { + /** + * + * dp[1] 表示手里没有第一支股票的最大利润 + * - 前一天就没 dp[i-1][1] + * - 前一天有第一支,今天刚刚卖了 dp[i-1][2] + prices[i] + * dp[2] 表示手里有第一支股票的最大利润 + * - 前一天就有 dp[i-1][2] + * - 前一天没有,刚刚买入第一支 dp[i-1][1] - prices[i] + * + * dp[3] 表示手里没有第二支股票的最大利润 + * + * dp[4] 表示手里有第二支股票的最大利润 + * + * @param prices + * @return + */ + public int maxProfit(int[] prices) { + + int[][] dp = new int[prices.length][5]; + dp[0][1] = 0; + dp[0][2] = -prices[0]; + dp[0][3] = 0; + dp[0][4] = -prices[0]; + + for (int i = 1; i < prices.length; i++) { + dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][0] - prices[i]); + dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][2] + prices[i]); + // 手里有第二支股票的最大利润 前一天就有第二支,【前一天没有第二支,也没有第一支但今天买入】 + dp[i][4] = Math.max(dp[i - 1][4], dp[i - 1][1] - prices[i]); + // 手里没有第二支股票的最大利润 前一天就没有第二支,前一天有第二支但今天卖出 + dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][4] + prices[i]); + } + return dp[prices.length - 1][3]; + } + } + +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode289.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode289.java new file mode 100644 index 0000000..daea32a --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode289.java @@ -0,0 +1,87 @@ +package cn.whaifree.redo.redo_all_241016; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/31 11:28 + * @注释 + */ +public class LeetCode289 { + + @Test + public void test() { + int[][] board = new int[][]{{0,1,0},{0,0,1},{1,1,1},{0,0,0}}; + new Solution().gameOfLife(board); + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[0].length; j++) { + System.out.print(board[i][j] + " "); + } + } + } + + class Solution { + /** + * 如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡;1 2 + * 如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活; 1 1 + * 如果活细胞周围八个位置有超过三个活细胞,则该位置活细胞死亡;1 2 + * 如果死细胞周围正好有三个活细胞,则该位置死细胞复活;0 3 + * + * 2 之前是活着,现在死了 + * 3 之前是死的,现在活了 + * + * 最后遍历吧3变成1,把2变成0 + * + * @param board + */ + public void gameOfLife(int[][] board) { + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[0].length; j++) { + int count = aliveCount(board, i, j); + if (board[i][j] == 1) { // 之前是活着的 + if (count < 2) { + board[i][j] = 2; + }else if (count > 3) { + board[i][j] = 2; + } + }else { + if (count == 3) { + board[i][j] = 3; + } + } + + } + } + + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[0].length; j++) { + if (board[i][j] == 3) { + board[i][j] = 1; + } else if (board[i][j] == 2) { + board[i][j] = 0; + } + } + } + } + + + public int aliveCount(int[][] board, int i, int j) { + int res = 0; + for (int ti = i - 1; ti < i + 2; ti++) { + for (int tj = j - 1; tj < j + 2; tj++) { + if (ti == i && tj == j) { + continue; + } + if (ti >= 0 && ti < board.length && tj >= 0 && tj < board[0].length) { + if (board[ti][tj] == 1 || board[ti][tj] == 2) { + res++; + } + } + } + } + return res; + } + } + +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode468.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode468.java new file mode 100644 index 0000000..0d807a0 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode468.java @@ -0,0 +1,10 @@ +package cn.whaifree.redo.redo_all_241016; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/31 12:56 + * @注释 + */ +public class LeetCode468 { +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode49.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode49.java new file mode 100644 index 0000000..91a2eba --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode49.java @@ -0,0 +1,58 @@ +package cn.whaifree.redo.redo_all_241016; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/11/1 13:55 + * @注释 + */ +public class LeetCode49 { + + @Test + public void test() { + String[] strs = {"abbbbbbbbbbb","aaaaaaaaaaab"}; + List> result = new Solution().groupAnagrams(strs); + for (List list : result) { + System.out.println(list); + } + } + + class Solution { + public List> groupAnagrams(String[] strs) { + + Map> map = new HashMap<>(); + for (int i = 0; i < strs.length; i++) { + + int[] counts = new int[26]; + String item = strs[i]; + for (int j = 0; j < item.length(); j++) { + counts[item.charAt(j) - 'a']++; + } + + StringBuilder str = new StringBuilder(); + for (int j = 0; j < counts.length; j++) { + if (counts[j] != 0) { + str.append((char) ('a' + j)).append(counts[j]); + } + } + if (!map.containsKey(str.toString())) { + map.put(str.toString(), new ArrayList<>()); + } + map.get(str.toString()).add(strs[i]); + } + + List> result = new ArrayList<>(); + for (Map.Entry> entry : map.entrySet()) { + result.add(entry.getValue()); + } + return result; + } + } +} diff --git a/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/controller/DynamicThreadPoolController.java b/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/controller/DynamicThreadPoolController.java index 2f8a908..0ffcf89 100644 --- a/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/controller/DynamicThreadPoolController.java +++ b/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/controller/DynamicThreadPoolController.java @@ -33,7 +33,6 @@ public class DynamicThreadPoolController { @PostConstruct public void init() { - executor = new ThreadPoolExecutor( Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().availableProcessors() * 4, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100), @@ -68,6 +67,7 @@ class DynamicThreadPoolConfig implements ApplicationListener InstanceIds) { + System.out.println("当前限流数:" + rateLimiter.getRate()); + List succ = new ArrayList<>(); + for (String instanceId : InstanceIds) { + if (rateLimiter.tryAcquire()) { + // 处理重置请求 + succ.add(instanceId); + }else { + return "请求过于频繁,请稍后重试"; + } + } + return StrUtil.format("重置成功,成功实例:{}", succ); + } + } diff --git a/SpringCloud/ServiceB/src/main/resources/application.yaml b/SpringCloud/ServiceB/src/main/resources/application.yaml index b2aed2f..f344e0b 100644 --- a/SpringCloud/ServiceB/src/main/resources/application.yaml +++ b/SpringCloud/ServiceB/src/main/resources/application.yaml @@ -1,6 +1,5 @@ spring: - application: name: ServiceB cloud: @@ -12,23 +11,21 @@ spring: group: DEFAULT_GROUP server-addr: localhost:8848 config: - # ?????????id????????dev?id??????????id?? + # 配置所属命名空间的id,我们配置名称为dev的id,在命名空间列表查看id的值 namespace: 97ff159f-6177-4aab-b735-bd75458949d4 - # ?????????????? ${spring.application.name} - # prefix: springboot3-nacos - # ?????? + # 文件名,如果没有配置则默认为 ${spring.application.name} + # prefix: springboot3-nacos + # 配置所属分组 group: DEFAULT_GROUP - # ??????? properties ? yaml ?? + # 后缀名,只支持 properties 和 yaml 类型 file-extension: yaml - # nacos????? + # nacos服务器地址 server-addr: localhost:8848 - # ?????? + # 配置自动刷新 refresh-enabled: true - # ???????? - enable-remote-sync-config: true config: import: - - optional:nacos:${spring.application.name}.${spring.cloud.nacos.config.file-extension} + - optional:nacos:springboot3-nacos.${spring.cloud.nacos.config.file-extension} # Logger Config logging: @@ -37,4 +34,3 @@ logging: hexadecimal: name: whai -# --server.port=12139 diff --git a/SpringCloud/pom.xml b/SpringCloud/pom.xml index 6584809..254e478 100644 --- a/SpringCloud/pom.xml +++ b/SpringCloud/pom.xml @@ -31,6 +31,13 @@ 2023.0.3 + + + org.projectlombok + lombok + 1.18.30 + provided + cn.hutool diff --git a/SpringDemo/pom.xml b/SpringDemo/pom.xml index beca947..4418e5c 100644 --- a/SpringDemo/pom.xml +++ b/SpringDemo/pom.xml @@ -38,14 +38,8 @@ com.baomidou - mybatis-plus-boot-starter - 3.5.1 - - - - org.mybatis.spring.boot - mybatis-spring-boot-starter - 3.0.2 + mybatis-plus-spring-boot3-starter + 3.5.9 com.github.xiaoymin diff --git a/SpringDemo/src/main/java/cn/whaifree/springdemo/config/MybatisConfig.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/config/MybatisConfig.java new file mode 100644 index 0000000..11a362b --- /dev/null +++ b/SpringDemo/src/main/java/cn/whaifree/springdemo/config/MybatisConfig.java @@ -0,0 +1,16 @@ +package cn.whaifree.springdemo.config; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.context.annotation.Configuration; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/11/1 12:41 + * @注释 + */ +@Configuration +@MapperScan("cn.whaifree.springdemo.mybatis.mapper") +public class MybatisConfig { + +} diff --git a/SpringDemo/src/main/java/cn/whaifree/springdemo/tech/CircleRelation.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/tech/CircleRelation.java new file mode 100644 index 0000000..352c4f7 --- /dev/null +++ b/SpringDemo/src/main/java/cn/whaifree/springdemo/tech/CircleRelation.java @@ -0,0 +1,138 @@ +//package cn.whaifree.springdemo.tech; +// +//import jakarta.annotation.Resource; +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.context.annotation.AnnotationConfigApplicationContext; +//import org.springframework.context.annotation.Lazy; +//import org.springframework.context.annotation.Scope; +//import org.springframework.stereotype.Component; +// +///** +// * @version 1.0 +// * @Author whai文海 +// * @Date 2024/10/31 13:57 +// * @注释 +// */ +//public class CircleRelation { +// +// +//} +// +// +//// ================================================================ +// +//@Component +////@Scope("prototype") +//class A1{ +//// @Resource +//// public B1 b1; +// +// public static void main(String[] args) { +// AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("cn.whaifree.springdemo.tech"); +//// A1 bean = context.getBean(A1.class); +//// System.out.println(bean.b1.a1); +// +//// LazyA1 lazyA1 = context.getBean(LazyA1.class); +//// System.out.println(lazyA1.b1); +// } +//} +// +///** +// * BeanCurrentlyInCreationException: +// * Error creating bean with name 'a1': +// * Requested bean is currently in creation: Is there an unresolvable circular reference? +// * +// * A 实例创建后,populateBean 时,会触发 B 的加载。 +// * B 实例创建后,populateBean 时,会触发 A 的加载。由于 A 的 scope=prototype,需要的时候都会创建一个全新的 A。 +// * 这样,就会进入一个死循环。Spring 肯定是解决不了这种情况下的循环依赖的。所以,提前进行了 check,并抛出了异常。 +// * +// * A需要一个新的B,B需要一个新的A,不能用为初始化的A活着B +// */ +//@Component +////@Scope("prototype") +//class B1{ +// @Resource +// public A1 a1; +//} +// +// +//@Component +//@Scope("prototype") +//class LazyA1{ +// +// @Autowired +// @Lazy +// public LazyB1 b1; +//} +///** +// * +// */ +//@Component +//@Scope("prototype") +//class LazyB1{ +// @Autowired +// public LazyA1 a1; +//} +//// ================================================================ +// +// +//@Component +//class ConstructA { +// ConstructB b; +// public ConstructA(@Lazy ConstructB b) { +// this.b = b; +// } +//} +//@Component +//class ConstructB { +// ConstructA a; +// public ConstructB(ConstructA a) { +// this.a = a; +// } +// public static void main(String[] args) { +// /** +// * A 实例在创建时(createBeanInstance),由于是构造注入,这时会触发 B 的加载。 +// * B 实例在创建时(createBeanInstance),又会触发 A 的加载,此时,A 还没有添加到三级缓存(工厂)中,所以就会创建一个全新的 A。 +// * 这样,就会进入一个死循环。Spring 是解决不了这种情况下的循环依赖的。所以,提前进行了 check,并抛出了异常。 +// * +// * 解决:@Lazy放在构造器上,这样,A 实例在创建时,不会触发 B 的加载。 +// * +// * 构造函数注入要求在创建bean实例时必须提供所有依赖 +// * +// */ +// AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("cn.whaifree.springdemo.tech"); +// ConstructA bean = context.getBean(ConstructA.class); +// System.out.println(bean.b); +// // Unsatisfied dependency expressed through constructor parameter 0: +// // Error creating bean with name 'constructA': Requested bean is currently in creation: Is there an unresolvable circular reference? +// } +//} +// +// +// +//@Component +//class SetA { +// SetB b; +// +// @Autowired +// public void setB(SetB b) { +// this.b = b; +// } +//} +//@Component +//class SetB { +// +// SetA a; +// +// @Autowired +// public void setA(SetA a) { +// this.a = a; +// } +// +// public static void main(String[] args) { +// +// AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("cn.whaifree.springdemo.tech"); +// SetA bean = context.getBean(SetA.class); +// System.out.println(bean.b); +// } +//} diff --git a/SpringDemo/src/main/resources/application.yaml b/SpringDemo/src/main/resources/application.yaml index e54ae08..82e4781 100644 --- a/SpringDemo/src/main/resources/application.yaml +++ b/SpringDemo/src/main/resources/application.yaml @@ -40,8 +40,6 @@ spring: direct: acknowledge-mode: manual -mybatis: - mapper-locations: '"classpath:/mapper/**.xml"' mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl @@ -49,6 +47,7 @@ mybatis-plus: db-config: logic-delete-field: 1 logic-not-delete-value: 0 + mapper-locations: "classpath:/mapper/**.xml" diff --git a/SpringDemo/src/test/java/cn/whaifree/springdemo/MybatisTest/MybatisTest.java b/SpringDemo/src/test/java/cn/whaifree/springdemo/MybatisTest/MybatisTest.java new file mode 100644 index 0000000..98d221f --- /dev/null +++ b/SpringDemo/src/test/java/cn/whaifree/springdemo/MybatisTest/MybatisTest.java @@ -0,0 +1,34 @@ +package cn.whaifree.springdemo.MybatisTest; + +import cn.whaifree.springdemo.mybatis.domain.Orders; +import cn.whaifree.springdemo.mybatis.mapper.OrdersMapper; +import cn.whaifree.springdemo.mybatis.service.OrdersService; +import jakarta.annotation.Resource; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/11/1 12:43 + * @注释 + */ +@SpringBootTest +public class MybatisTest { + + @Resource + private OrdersService ordersService; + + @Resource + private OrdersMapper ordersMapper; + @Test + public void test() { + + List orders = ordersMapper.selectList(null); + System.out.println(orders); + List orders1 = ordersMapper.selectByExample(); + System.out.println(orders1); + } +} diff --git a/pom.xml b/pom.xml index b9e49f4..575f05c 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ org.example LeetCode 1.0-SNAPSHOT - pom + jar ForJdk8 ForJdk17