LeetCode Weekly Contest 237解题报告 (上)
想上岸 找上岸No.1 Check if the Sentence Is Pangram解题思路简单遍历字符串判断非英文字母字符直接返回false否则记录不同字符的个数是否满足26个代码展示public boolean checkIfPangram(String sentence) {if (sentence == null || sentence.length() == 0) {
return false;
}
Set<Character> set = new HashSet<>();
for (char c : sentence.toCharArray()) {
if (c - 'a' < 0 || c - 'a' >= 26) {
return false;
}
set.add(c);
}
return set.size() == 26;
}No.2 Maximum Ice Cream Bars解题思路贪心的去买雪糕无需看成背包问题,TLE or MLE代码展示public int maxIceCream(int[] costs, int coins) {
if (costs == null || costs.length == 0) {
return 0;
}
int n = costs.length;
int res = 0;
// 排序从小到大买即可
Arrays.sort(costs);
for (int cost : costs) {
if (coins > cost) {
res += 1;
coins -= cost;
}
else {
return res;
}
}
return res;
}
页:
[1]