[size=1em]想上岸 找上岸 No.1 Check if the Sentence Is Pangram 解题思路 简单遍历字符串判断 非英文字母字符直接返回false 否则记录不同字符的个数是否满足26个 代码展示 - public boolean checkIfPangram(String sentence) {
1 w( \; ~7 Y' [: k) u' X# N | - if (sentence == null || sentence.length() == 0) { 4 r! }$ E; h/ J- x! W% R$ ]
- return false;
! L# y; E! D1 x T9 z" A+ U - } 4 p6 B3 x# G0 M0 t$ p
- Set<Character> set = new HashSet<>();
) C% `* P8 S- l% c, L - for (char c : sentence.toCharArray()) {
8 p5 _3 U: Z: m3 i% Q+ U* i2 J - if (c - 'a' < 0 || c - 'a' >= 26) { * J5 n3 E4 f/ Y; n; g5 {
- return false;
& |2 s7 u1 j& z7 ?& Y( E - } [; t! P4 \: a! W6 r; X$ [' s
- set.add(c); w. U" k6 V( J }% K
- } M3 g( o/ n7 y* B1 V
- return set.size() == 26;
f0 b: i' `5 x2 U5 n* u5 q - }
复制代码No.2 Maximum Ice Cream Bars 解题思路 贪心的去买雪糕 无需看成背包问题,TLE or MLE 代码展示 - public int maxIceCream(int[] costs, int coins) {
0 R- i/ v8 A8 g6 o' \! H" u - if (costs == null || costs.length == 0) {
\& S5 P$ c" q, Y1 k( I* b - return 0;
3 M$ b% z' R0 n! k1 H - }
6 F4 U6 L. Y+ A& Y7 z+ b - int n = costs.length; . l* S& @, q. P/ C% @
- int res = 0; / J$ F, [( e5 D7 q; o; b# B
- // 排序从小到大买即可
; }: ~$ {/ q2 d - Arrays.sort(costs);
, c7 S' ^$ Q( H& b& h5 X7 M- D - for (int cost : costs) {
" D* U8 s0 X1 @3 Y; L& f - if (coins > cost) { # H1 ]) t; {% W+ |4 l. v" p t3 k
- res += 1; ) a; k. j9 c8 ^- F* @- X
- coins -= cost; " `* T+ q/ O( H( ?- j2 E5 h# e
- } - z! `, h( Y( a! j5 q
- else {
( k, ^) w- G5 ~: R5 i3 M% m - return res;
/ }/ W& ~5 y3 g. e: e - }
8 i" Q' C3 X( w9 T6 M6 A - }
5 c- f+ R& X3 _) Z7 x9 y7 v( S - return res;
( @" a1 p0 k; P5 Z0 J i5 D - }
复制代码
3 R( }! ^9 R) ~. ^0 x " g( W# n, A7 P, G+ q
- L7 T9 u1 e; a5 _1 V# p
; Y; X8 o8 T; J* G) h' I8 O$ M. R2 w, `3 y$ m
|