6180. 最小偶倍数 - 力扣(LeetCode)
sb题目
1 2 3 4 5
| class Solution { public int smallestEvenMultiple(int n) { return n%2==0?n:n*2; } }
|
6181. 最长的字母序连续子字符串的长度 - 力扣(LeetCode)
单指针直接遍历计数就好了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { public int longestContinuousSubstring(String s) { char lastC = 0; int len = 0; int res = 0; for (int i = 0; i < s.length();i++) { char c = s.charAt(i); if (c != lastC + 1) { res = Math.max(len, res); len = 1; } else { len++; } lastC = c; } res = Math.max(len, res);
return res; } }
|
6182. 反转二叉树的奇数层 - 力扣(LeetCode)
先后序dfs记录每个点在哪个层,放进一个list
然后按层遍历哪些list,把他们的值返回来重新存放即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
class Solution { public TreeNode reverseOddLevels(TreeNode root) { Map<Integer, List<TreeNode>> nodes = new HashMap<>(); dfs(root, 0, nodes); for (Map.Entry<Integer, List<TreeNode>> entry : nodes.entrySet()) { if (entry.getKey() % 2 == 0) { continue; } List<TreeNode> list = entry.getValue(); List<Integer> valus = list.stream().map(tn -> tn.val).collect(Collectors.toList()); Collections.reverse(valus); for (int i = 0; i < list.size();i++) { list.get(i).val = valus.get(i); } } return root; } void dfs(TreeNode node, int level, Map<Integer, List<TreeNode>> map) { if( node == null) { return ; } dfs(node.left, level+1, map); dfs(node.right, level+1, map); if (!map.containsKey(level)) { map.put(level, new ArrayList<>()); } map.get(level).add(node); } }
|
6183. 字符串的前缀分数和 - 力扣(LeetCode)
字段树,只有1000的范围,不会超,直接构建完树后直接遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| class Solution { static class Node { int count = 0; Node[] nextNodes = new Node[26]; } public int[] sumPrefixScores(String[] words) { Node root = new Node(); for (String word : words) { Node node = root; for (char c : word.toCharArray()) { if (node.nextNodes[c-'a'] == null) { node.nextNodes[c-'a'] = new Node();
} node.nextNodes[c-'a'].count++; node = node.nextNodes[c-'a']; } } int[] res = new int[words.length]; int i = 0; for (String word : words) { Node node = root; int sum = 0; for (char c : word.toCharArray()) { if (node.nextNodes[c-'a'] != null) { sum += node.nextNodes[c - 'a'].count; } node = node.nextNodes[c-'a']; } res[i++] = sum; } return res; } }
|