No.1
判断字符串的两半是否类似
解题思路
统计元音字母数量即可。
代码展现
class Solution { public boolean halvesAreAlike(String s) { int n = s.length(); int a = 0, b = 0; for (int i = 0, j = n - 1; i < j; i++, j--) { a += "AEIOUaeiou".indexOf(s.charAt(i)) >= 0 ? 1 : 0; b += "AEIOUaeiou".indexOf(s.charAt(j)) >= 0 ? 1 : 0; } return a == b; }}No.2
吃苹果的最大数目
解题思路
贪婪的思路,咱们总是吃掉剩下的苹果中最先烂掉的。应用优先队列能够保护剩下的苹果哪些先烂掉。
代码展现
class Solution { static class Apple { int num; int day; public Apple(int num, int day) { this.num = num; this.day = day; } } public int eatenApples(int[] apples, int[] days) { PriorityQueue<Apple> bucket = new PriorityQueue<>(Comparator.comparingInt(a -> a.day)); int res = 0; for (int i = 0; i < apples.length; i++) { if (apples[i] > 0) { bucket.add(new Apple(apples[i], i + days[i])); } res += eat(bucket, i); } // 不再减少苹果,将剩下的吃完 for (int i = apples.length; !bucket.isEmpty(); i++) { res += eat(bucket, i); } return res; } // 在第 day 天吃苹果,返回能吃到的数量 (0 或 1) private int eat(PriorityQueue<Apple> bucket, int day) { while (!bucket.isEmpty()) { Apple a = bucket.poll(); if (a.day <= day) { continue; } if ((--a.num) > 0) { bucket.add(a); } return 1; } return 0; }}No.3
...