|
[size=1em]想上岸 找上岸 No.1 Check if the Sentence Is Pangram 解题思路 简单遍历字符串判断 非英文字母字符直接返回false 否则记录不同字符的个数是否满足26个 代码展示 - public boolean checkIfPangram(String sentence) {
0 p" @' Q M# C% |5 I0 C - if (sentence == null || sentence.length() == 0) {
0 {8 w) k) u4 B# @$ T+ F" w - return false; % N4 D `: C$ u
- }
/ o- H" P# k5 K3 A, A, m - Set<Character> set = new HashSet<>();
! R; B! i! W; k - for (char c : sentence.toCharArray()) { * ]% l% f, N& u: r; _ P2 p
- if (c - 'a' < 0 || c - 'a' >= 26) {
! v1 _# A2 l$ c* ?" z/ a* ? - return false;
; \0 m( F! X- @% M6 m3 X - }
- q" k0 _. P, N' I9 U- O. {. j, S* C - set.add(c); * m$ F3 c! b! d6 ^6 \
- } * P# i# j5 m- K' a2 Y
- return set.size() == 26; 2 k" {0 h2 s6 w- n1 r- ]" f& l
- }
复制代码No.2 Maximum Ice Cream Bars 解题思路 贪心的去买雪糕 无需看成背包问题,TLE or MLE 代码展示 - public int maxIceCream(int[] costs, int coins) {
+ s* N9 }6 {! A* \" r - if (costs == null || costs.length == 0) { ) G- @" h! K1 p" c$ _9 |
- return 0;
5 I/ w2 a0 q) y - } 3 z& L2 P7 Q* ]% z, b( w
- int n = costs.length; # W6 d; O6 ~/ `* N3 f7 G8 U$ ?
- int res = 0; ! i$ ~7 y& n% S# I& n
- // 排序从小到大买即可
3 J! t( w% _$ h; E, T( I4 r' u/ S - Arrays.sort(costs);
+ A$ I/ t/ z" A: e - for (int cost : costs) {
# O2 f. S/ w0 \8 i5 V - if (coins > cost) {
, u& ?- [& v" { - res += 1;
3 S: C8 X; u" _9 H5 {0 u - coins -= cost; $ I+ H% f$ H0 ~9 b/ R/ I
- }
D& g7 i1 N! J0 ^ - else { $ x1 d. @; ?; U4 R2 O' F/ {3 |1 V
- return res;
( a9 }# l5 f" @! _ - }
& V/ `1 |8 o: B7 f' o# J; a - } ! u$ f ^/ _* e5 ^
- return res;
5 Z C8 v t2 H |! x% D - }
复制代码
9 U% v- I; M9 d/ L: z ! Q; I" y! b2 m, Y
, U+ [# r$ w# }, Y+ B: V$ d
9 j; x4 G* [* n
+ \4 o# j) k2 W. ^7 @
|