[size=1em]想上岸 找上岸 No.1 Check if the Sentence Is Pangram 解题思路 简单遍历字符串判断 非英文字母字符直接返回false 否则记录不同字符的个数是否满足26个 代码展示 - public boolean checkIfPangram(String sentence) {
* m6 f0 D5 o/ y9 L! c - if (sentence == null || sentence.length() == 0) { , ^- C L, S+ m& f( h
- return false;
. g4 w1 K4 v6 d$ W - }
+ U% q# c' q9 p+ z - Set<Character> set = new HashSet<>();
) R2 ?7 N4 F7 R, Q - for (char c : sentence.toCharArray()) {
4 w' i4 `* ]4 u8 ?: w5 S - if (c - 'a' < 0 || c - 'a' >= 26) {
& [& I' L: f) k) e8 ]2 E - return false;
6 F2 k4 J# t. L. @2 U0 r: Q - }
- d3 R$ F, Z: S. k* m) t - set.add(c);
1 X, U8 X# k+ |: {+ j - }
; x6 |/ m2 s D* u& y1 A; | - return set.size() == 26; - `3 ~1 E! N% O* h) f
- }
复制代码No.2 Maximum Ice Cream Bars 解题思路 贪心的去买雪糕 无需看成背包问题,TLE or MLE 代码展示 - public int maxIceCream(int[] costs, int coins) { & `, v- |( U, n" a( M2 g9 ~& ~
- if (costs == null || costs.length == 0) { & \8 k- a, E, [, a D
- return 0; $ W1 b( G! l, v% a7 e2 E
- } . x6 b& r7 s, T& ~; _
- int n = costs.length; & W4 M' e" }* V- r' i
- int res = 0; * @1 _; v* O# h$ c9 K" A; L( y
- // 排序从小到大买即可 . u& e5 ^9 x ~' z7 {& E+ t U. p; s
- Arrays.sort(costs);
% }( E; S1 Y3 x! B - for (int cost : costs) { ) v. l, Y& {: D/ b& G2 r
- if (coins > cost) { 8 S/ Y6 X1 {; p1 |+ J
- res += 1;
& a( v+ E0 j0 l) Z' I5 z: ?0 S - coins -= cost; 3 ~* K O# ]0 c" {' G/ R& F2 V
- } ) `8 \- K, D9 j
- else { 1 s. ~2 [4 X: a/ n& d6 _% I
- return res;
/ J1 I; x3 p! ]8 e' S! W - } 1 C3 J, m7 L) {1 }
- } ( I6 e2 H. z3 G" {
- return res;
5 K% `% e/ i! y& S6 d - }
复制代码
% H1 B! d _: v% t
% l6 T' g1 t- k( i
! `- S- z' z" r2 q! w* O" _ P! G! R& `0 y' g* t
! [3 t- i; ~1 B0 |7 @- x2 G
|