[size=1em]想上岸 找上岸 No.1 Check if the Sentence Is Pangram 解题思路 简单遍历字符串判断 非英文字母字符直接返回false 否则记录不同字符的个数是否满足26个 代码展示 - public boolean checkIfPangram(String sentence) {
9 [: D0 q' x+ X; K! R8 ^ - if (sentence == null || sentence.length() == 0) {
$ K' p9 G4 e* S9 V2 ? - return false; 1 ^- I1 ~' Q) }6 M8 y, P
- }
+ v% V8 M# H; K9 v - Set<Character> set = new HashSet<>(); ) ~6 M- E/ j6 \/ ~. ]0 c
- for (char c : sentence.toCharArray()) {
: h3 h3 _: R7 R6 `; e& ? - if (c - 'a' < 0 || c - 'a' >= 26) { ' u. W4 x0 C" h, f& P% J2 z. {# c
- return false;
' D( t! A2 }3 J - }
: z# V3 b* a- c+ ^ - set.add(c);
- r- k+ @: D$ p5 n3 Y3 z* j - } 2 F& O! F; {5 Z2 A& k
- return set.size() == 26; 3 d4 ~% ~' V: n( u, ]" t0 `
- }
复制代码No.2 Maximum Ice Cream Bars 解题思路 贪心的去买雪糕 无需看成背包问题,TLE or MLE 代码展示 - public int maxIceCream(int[] costs, int coins) { / [( C) Z% }0 R
- if (costs == null || costs.length == 0) { + Z; B6 H" g! Z6 \7 {% @
- return 0;
! @& |0 h0 R0 C& h - } ' b" i1 U/ {1 L' k. H8 L
- int n = costs.length;
: }. x5 Z5 m/ O! ~( O - int res = 0;
( a& C# P+ H7 Q% H0 e - // 排序从小到大买即可
* X9 a6 G. t- c; o2 V9 l1 p" m - Arrays.sort(costs); 1 D$ X9 T$ Y& w& Q: X: a( U% T: ]+ N! R
- for (int cost : costs) { 3 m4 H' `0 ?$ Q T
- if (coins > cost) {
# u, d5 ]6 B9 w - res += 1;
H) y9 i, v- A9 m. M - coins -= cost; 1 O& k6 d: d) u2 {) y+ Q
- }
; y4 f) K: I! S6 @$ _ - else { 3 {( F4 Y4 x9 X; _+ F
- return res;
# A/ a! E: N& H+ ^4 c- M0 ]& z - } 0 q8 Y$ C8 f+ n/ C5 o* I( a% n
- } 5 }2 Y3 c6 F2 J1 ^4 s
- return res;
3 ?6 h+ c6 E- K; I# u. U6 L# e - }
复制代码
! P& R1 y$ \! }) _! L/ P4 L. U6 P
: k( f5 A* v7 N5 j% [* P% }3 _$ x. p2 }0 l
& Y2 n/ r0 T% G7 ?
/ c5 j9 N. |2 u. i. Y! k, Q' v, W- w# G |