LC.P239[滑动窗口最大值]
LC.P239[滑动窗口最大值]
方法一:滑动窗口1234567891011121314151617181920212223class Solution { public int[] maxSlidingWindow(int[] nums, int k) { if (nums == null || nums.length < 2) return nums; Deque<Integer> queue = new ArrayDeque<>(); int n = nums.length; int[] ans = new int[n - k + 1]; for (int i = 0; i < n; ++i) { // 队列内从大到小 while (!queue.isEmpty() && nums[queue.peekLast()] <= nums[i]) { ...
LC.P726[原子的数量]
LC.P726[原子的数量]
方法一:数据结构模拟12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788class Solution { public String countOfAtoms(String formula) { int n = formula.length(); char[] s = formula.toCharArray(); Map<String, Integer> map = new HashMap<>(); Deque<String> stack = new ArrayDeque<>(); int i = 0, index = 0; ...
LC.P1079[活字印刷]
LC.P1079[活字印刷]
方法一:计数+回溯1234567891011121314151617181920212223class Solution { int[] cnt = new int[26]; public int numTilePossibilities(String tiles) { for (char c : tiles.toCharArray()) { ++cnt[c - 'A']; } return dfs(); } private int dfs() { int ans = 0; for (int i = 0; i < cnt.length; ++i) { if (cnt[i] > 0) { ++ans; --cnt[i]; a ...
LC.P1190[反转每对括号间的子串]
LC.P1190[反转每对括号间的子串]
方法一:栈123456789101112131415161718192021class Solution { public String reverseParentheses(String s) { Deque<Character> stack = new ArrayDeque<>(); for (int i = 0; i < s.length(); ++i) { char c = s.charAt(i); if (c == ')') { List<Character> list = new ArrayList<>(); while (stack.peek() != '(') list.add(stack.pop()); stack.pop(); ...
LC.P1073[负二进制数相加]
LC.P1073[负二进制数相加]
方法一:模拟12345678910111213141516171819202122232425262728293031class Solution { public int[] addNegabinary(int[] arr1, int[] arr2) { List<Integer> list = new ArrayList<>(); for (int i = arr1.length - 1, j = arr2.length - 1, c = 0; i >= 0 || j >= 0 || c != 0; --i, --j) { int x = c; if (i >= 0) x += arr1[i]; if (j >= 0) x += arr2[j]; if (x >= 2) { // 逢二进负一 x -= 2 ...
快速查看SpringBoot项目中的所有自启动执行方法
在SpringBoot项目本地调试过程中,若有不需要的自启动方法,则会捣乱输出大堆日志、修改数据等。以下是总结的SpringBoot项目的常见自定动方式:
@Scheduled(fixedRate = 5 * 60 * 100)定时的cron时间特别短
继承CommandLineRunner或ApplicationRunner的类的run方法
被@EventListener注解的方法参数是ApplicationReadyEvent
implements ApplicationListener<ContextRefreshedEvent>的类的onApplicationEvent(ContextRefreshedEvent event)
@PostConstruct 类初始化后执行操作,但是不能保证Spring容器已完全初始化,ApplicationContextAware不保证能取到值,最好用ApplicationRunner或者@EventListener
while(true) 空构造器里的持续执行
搜索 Quartz,JobDetailFactoryBean,Cro ...
LC.P622[设计循环队列]
LC.P622[设计循环队列]
方法一:双指针123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354class MyCircularQueue { int[] data; int size, head, tail; public MyCircularQueue(int k) { data = new int[k]; size = 0; head = 0; tail = k - 1; } public boolean enQueue(int value) { if (isFull()) return false; tail = tail == data.length - 1 ? 0 : tail + 1; data[tail] = value; ++size; ...
LC.P2446[判断两个事件是否存在冲突]
LC.P2446[判断两个事件是否存在冲突]
方法一:模拟123456class Solution { public boolean haveConflict(String[] event1, String[] event2) { // event1的开始时间大于event2的结束时间或者event1的结束时间小于event2的开始时间则没有冲突 return !(event1[0].compareTo(event2[1]) > 0 || event1[1].compareTo(event2[0]) < 0); }}
时间复杂度:$O(1)$
空间复杂度:$O(1)$
LC.P1335[工作计划的最低难度]
LC.P1335[工作计划的最低难度]
方法一:动态规划+记忆化搜索123456789101112131415161718192021222324252627282930313233343536class Solution { int[] jobDifficulty; int[][] cache; public int minDifficulty(int[] jobDifficulty, int d) { int n = jobDifficulty.length; if (n < d) return -1; this.jobDifficulty = jobDifficulty; this.cache = new int[d][n]; for (int i = 0; i < d; ++i) { Arrays.fill(cache[i], -1); } return dfs(d - 1, n - 1); ...
LC.P1044[最长重复子串]
LC.P1044[最长重复子串]
方法一:字符串哈希+二分12345678910111213141516171819202122232425262728293031323334353637class Solution { static final int hash = 1313131; long[] h, p; int n; public String longestDupSubstring(String s) { n = s.length(); h = new long[n + 1]; // 哈希数组 p = new long[n + 1]; // 次方数组 p[0] = 1; for (int i = 1; i <= n; ++i) { p[i] = p[i - 1] * hash; h[i] = h[i - 1] * hash + s.charAt(i - 1); } S ...