- 博客(434)
- 收藏
- 关注
原创 每日一题:648. 单词替换
遍历即可,关键在于用Map集合记录当前单词的最短词根,再遇到这个单词后就不需要进行查询了,直接从Map中取出,降低时间复杂度
2022-07-07 10:48:58
161
原创 每日一题:241. 为运算表达式设计优先级
分治,也可以理解为记忆化的dfs,难点在于思路保持清晰例如表达式2-1-1遇到第一个运算符-,开始递归分为左侧表达式2,右侧表达式1-1,符号为-左侧表达式2递归后,为纯数字,list为{2}右侧表达式1-1递归后,遇到运算符-,开始递归分为左侧表达式1,右侧表达式1,符号为-左侧表达式递归后为纯数字,list为{1}右侧表达式递归后为纯数字,list为{1}进行计算后结果为0,最终表达式1-1结果list为{0}{2}与{0}开始计算,运算符为-结果为2...
2022-07-01 10:56:34
139
原创 每日一题:1305. 两棵二叉搜索树中的所有元素
解题思路搜索树中序遍历即为升序排列,对两个根进行dfs在把序列组合代码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int va
2022-05-01 18:47:10
468
原创 每日一题:427. 建立四叉树
解题思路实际上就是递归构建树,但是理解题意会比较困难,可以看一下题解,就会明白四叉树的定义当前区域内值均相同,那么就是一个非叶子节点,反之则需要再次进行划分,从正方形的中心点开始分割代码/*// Definition for a QuadTree node.class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; pu
2022-04-29 10:38:27
298
原创 每日一题:417. 太平洋大西洋水流问题
解题思路题目需要找到既能流入太平洋同时也能流向大西洋的区域初始想法是对每个区域进行dfs判断其能否到达两个大洋,但时间复杂度过高因此更换一种思路,沿着边界进行dfs找到有多少区域能够流到边界位置,且需要两种边界的访问图,Pacific以及Atlantic,最后进行遍历,如果两个访问图都为true的位置,便是答案,即可以流向两个大洋代码class Solution { List<List<Integer>> ans; int[][] dir = {{-1,0}
2022-04-27 11:53:41
153
原创 每日一题:380. O(1) 时间插入、删除和获取随机元素
解题思路利用List组合Map实现代码class RandomizedSet { //对应关系为值和其在List中的下标 Map<Integer,Integer> map; List<Integer> list; public RandomizedSet() { map = new HashMap<>(); list = new ArrayList<>(); } publi
2022-04-26 17:44:19
175
原创 每日一题:386. 字典序排数
解题思路重写排序方式代码class Solution { public List<Integer> lexicalOrder(int n) { List<Integer> list = new ArrayList<>(); for (int i = 1; i <= n; i++) { list.add(i); } Collections.sort(list, new
2022-04-26 13:34:55
230
原创 每日一题:396. 旋转函数
解题思路根据题目案例可以推出在1<=k<n时,具有公式F(k)=F(k−1)+numSum−n×nums[n−k],numSum为nums数组和代码class Solution { //F(k)=F(k−1)+numSum−n×nums[n−k] public int maxRotateFunction(int[] nums) { final int n = nums.length; if (n == 1) { ret
2022-04-26 13:14:18
251
原创 每日一题:398. 随机数索引
解题思路利用Map映射值与下标List,pick时取0到list的大小的随机数代码class Solution { Map<Integer, List<Integer>> map; public Solution(int[] nums) { map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (!map.co
2022-04-26 12:45:42
138
原创 每日一题:2038. 如果相邻两个颜色均相同则删除当前颜色
解题思路假设一个串为"AAAAA",长度为5,Alice实际的操作次数只能有5-2次,Bob同理因此找出Alice和Bob的可操作次数,谁的可操作次数大谁就能获胜代码class Solution { public boolean winnerOfGame(String colors) { final char[] chars = colors.toCharArray(); int n = colors.length(),low = 0,high = 0;
2022-03-22 18:50:07
182
原创 每日一题:2016. 增量元素之间的最大差值
解题思路双指针代码class Solution { public int maximumDifference(int[] nums) { int low = 0,high = 0,res = Integer.MIN_VALUE; while (high<nums.length){ if (nums[high] >= nums[low]){ res = Math.max(res,nums[high
2022-02-26 12:15:38
156
原创 每日一题:537. 复数乘法
解题思路模拟计算过程代码class Solution { public String complexNumberMultiply(String num1, String num2) { final String[] split1 = num1.split("\\+"); final String[] split2 = num2.split("\\+"); int s1 = Integer.parseInt(split1[0]);
2022-02-25 13:52:26
199
原创 每日一题:1189. “气球” 的最大数量
解题思路此处撰写解题思路代码class Solution { public int maxNumberOfBalloons(String text) { char[] chars = text.toCharArray(); int res = Integer.MAX_VALUE; Map<Character,Integer> map = new HashMap<>(); map.put('b',0);
2022-02-13 10:53:37
147
原创 每日一题:1020. 飞地的数量
解题思路遍历周围一圈的1并将与之相连的1全部变为0,这样grid中剩下的1就是无法到达边界的代码class Solution { public int numEnclaves(int[][] grid) { int res = 0; for (int i=0;i<grid.length;i++){ for (int j=0;j<grid[0].length;j++){ if (grid[i][j
2022-02-12 13:58:49
197
原创 每日一题:1629. 按键持续时间最长的键
解题思路模拟代码class Solution { public char slowestKey(int[] releaseTimes, String keysPressed) { char res = keysPressed.charAt(0); int max = releaseTimes[0]; for (int i=1;i<releaseTimes.length;i++){ if (releaseTimes[i
2022-01-10 11:42:54
235
原创 每日一题:1614. 括号的最大嵌套深度
解题思路利用栈代码class Solution { public int maxDepth(String s) { char[] chars = s.toCharArray(); int res = Integer.MIN_VALUE; LinkedList<Character> stack = new LinkedList<>(); for (int i=0;i<chars.length;i++){
2022-01-07 12:33:02
3348
原创 每日一题:71. 简化路径
解题思路利用栈的思想,去除掉字符串中的".","",如果遇到"…"就将栈顶弹出即可代码class Solution { public String simplifyPath(String path) { String[] mem = path.split("/"); LinkedList<String> stack = new LinkedList<>(); StringBuilder res = new StringBu
2022-01-06 13:32:18
73
原创 每日一题:1185. 一周中的第几天
解题思路基姆拉尔森计算公式代码class Solution { public String dayOfTheWeek(int d, int m, int y) { String[] weeks = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; if(m < 3) { m += 12; --y;
2022-01-03 20:45:57
198
原创 计算日期是星期几(基姆拉尔森计算公式)
W= (d+2m+3(m+1)/5+y+y/4-y/100+y/400+1)%7在公式中d表示日期中的日数,m表示月份数,y表示年数。W表示星期 星期一到星期六。
2022-01-03 20:45:16
482
原创 2022-01-03
Hello everyone, please allow me to introduce myself.my name is Zhou Tianle(周天乐) it means happy every day~ from Yichang, Hubei.I am a very outgoing person. If you want to make friends with me, please a...
2022-01-03 13:45:29
48
原创 每日一题:507. 完美数
解题思路枚举代码class Solution { public boolean checkPerfectNumber(int num) { if (num == 1){ return false; } int res = 1; int n = (int)Math.sqrt(num); System.out.println(n); for (int i=2;i
2021-12-31 13:03:28
186
原创 每日一题:846. 一手顺子
解题思路利用TreeMap的特性代码class Solution { public boolean isNStraightHand(int[] hand, int groupSize) { if(hand.length % groupSize != 0){ return false; } TreeMap<Integer,Integer> map = new TreeMap<>();
2021-12-30 12:55:11
264
原创 每日一题:1078. Bigram 分词
解题思路模拟代码class Solution { public String[] findOcurrences(String text, String first, String second) { String[] strings = text.split(" "); List<String> res = new ArrayList<>(); for (int i=0;i<strings.length-2;i++)
2021-12-27 13:21:32
164
原创 5947. 从给定原材料中找到所有可以做出的菜
解题思路DFS,并进行剪枝代码class Solution { public List<String> findAllRecipes(String[] recipes, List<List<String>> ingredients, String[] supplies) { List<String> res = new ArrayList<>(); Set<String> have
2021-12-27 13:16:07
213
原创 每日一题:1609. 奇偶树
解题思路广度优先搜索BFS代码/** * 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, Tr
2021-12-25 15:47:58
319
原创 每日一题:419. 甲板上的战舰
解题思路类似于岛屿数量问题,只需要在图中找出练成一片的X即可代码class Solution { int[][] dir = {{-1,0},{1,0},{0,-1},{0,1}}; public int countBattleships(char[][] board) { int res = 0; for (int i=0;i<board.length;i++){ for (int j=0;j<board[i].le
2021-12-18 09:51:07
280
原创 关于结构设计类题目涉及到最大最小值的解决方案
方案1.优先队列2.TreeMap3.TreeSet代码/*优先队列取最大值即取出队头的值q.peek()即可但是无法修改其中元素数值*/PriorityQueue<Integer> q = new PriorityQueue<Integer>((a, b) -> b - a);/*TreeMap其键值key按照升序排列,可以用于有重复值且有修改问题的结构firstKey为最小值lastKey为最大值*/TreeMap<Integer,
2021-12-17 11:13:36
260
原创 第 262 场周赛 美团T3.2034. 股票价格波动
解题思路map记录关系为 时间戳->价格priceMap记录关系为 价格->价格出现次数因为priceMap是TreeMap,所以其lastKey就是最高价格,firstKey就是最低价格需要注意的细节就是当更新已有时间戳时,也要更新priceMap当中记录的价格出现次数,从而保证价格是最新的代码class StockPrice { int curTime; HashMap<Integer,Integer> map; TreeMap<Int
2021-12-17 11:05:46
230
原创 第 262 场周赛 美团T2.2033. 获取单值网格的最小操作数
解题思路贪心思想1.找到所有数的中位数,将所有数字都操作向其逼近,所耗费操作数一定是最小的2.计算当前数字能否通过x,转换为中位数,如果不能转换,说明矩阵无法统一变成同一数字代码class Solution { public int minOperations(int[][] grid, int x) { List<Integer> list = new ArrayList<>(); for (int i=0;i<grid.le
2021-12-17 10:59:39
112
原创 第 262 场周赛 美团T1.2032. 至少在两个数组中出现的值
解题思路利用Map<Integer, Set>统计数字和出现次数即可,如果set.size()>=2说明符合条件代码class Solution { public List<Integer> twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) { Map<Integer, Set<Integer>> map = new HashMap<>();
2021-12-17 10:56:31
288
原创 第 263 场周赛 字节跳动T3.2044. 统计按位或能得到最大值的子集数目
解题思路先找到按位或可以的到的最大值再进行dfs深搜,分为选择当前数和不选择当前数代码class Solution { int count = 0,max = 0; public int countMaxOrSubsets(int[] nums) { for (int i : nums) { max |= i; } dfs(nums,0,0); return count; } pu
2021-12-16 13:57:26
568
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人