100294. 统计特殊字母的数量 I - 力扣(LeetCode)
这个简单题被我拿下了:blush:但是耗费了我好长好长的时间,到底是API使用的不够熟练
1 2 3 4 5 6 7 8 9 10 11 12 13
| class Solution { public int numberOfSpecialChars(String word) { HashSet<Character> set = new HashSet<>(); for (char c : word.toCharArray()) set.add(c); int ans = 0; for (Character c : set){ if (Character.isLowerCase(c) && set.contains(Character.toUpperCase(c))){ ans++; } } return ans; } }
|
100291. 统计特殊字母的数量 II - 力扣(LeetCode)
这个题目花了我也是费劲了老长的时间,但是没有拿的下来,可能水平就到这里了:sob:
HashMap如果加入了相同的KEY,后面加入的<KEY,VALUE>会覆盖掉前面的
putIfAbsent() 方法会先判断指定的键(key)是否存在,不存在则将键/值对插入到 HashMap 中。
但是这个方法进行判断不会覆盖掉前面的键值对
注意:如果指定 key 之前已经和一个 null 值相关联了 ,则该方法也返回 null。
我的大致的思路和这个基本一致:sob::sob::sob:
只需要记住大写字母第一次出现的位置以及小写字母最后一次出现的位置就行了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public int numberOfSpecialChars(String word) { Map<Character, Integer> upperCaseStart = new HashMap<>(); Map<Character, Integer> lowerCaseEnd = new HashMap<>(); int n = word.length(); for(int i = 0; i < n; i++){ char c = word.charAt(i); if(Character.isUpperCase(c)){ upperCaseStart.putIfAbsent(c, i); }else{ lowerCaseEnd.put(c, i); } } int count = 0; for(char upper = 'A', lower = 'a'; upper <='Z'; upper++, lower++){ if(upperCaseStart.containsKey(upper) && lowerCaseEnd.containsKey(lower) && upperCaseStart.get(upper) > lowerCaseEnd.get(lower)){ count++; } } return count;
} }
|
100290. 使矩阵满足条件的最少操作次数 - 力扣(LeetCode)
这个题目我都没有时间去看:sob:真他妈难
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
| class Solution { public int minimumOperations(int[][] grid) { int m = grid.length, n = grid[0].length; int[][] count = new int[n][10]; int[][] dp = new int[n][10]; int ans = Integer.MAX_VALUE;
for (int j = 0; j < n; ++j) { Arrays.fill(count[j], 0); for (int i = 0; i < m; ++i) { ++count[j][grid[i][j]]; } }
for (int j = 0; j < 10; j++) { dp[0][j] = m - count[0][j]; }
for (int i = 1; i < n; ++i) { for (int j = 0; j < 10; ++j) { int minOps = Integer.MAX_VALUE; for (int k = 0; k < 10; ++k) { if (i - 1 >= 0 && j != k) { minOps = Math.min(minOps, dp[i - 1][k]); } } dp[i][j] = minOps + (m - count[i][j]); } }
for (int j = 0; j < 10; ++j) { ans = Math.min(ans, dp[n - 1][j]); }
return ans; } }
|
最后的大题是一个图的一个题目,做不来