|
[size=1em]想上岸 找上岸 No.1 Check if the Sentence Is Pangram 解题思路 简单遍历字符串判断 非英文字母字符直接返回false 否则记录不同字符的个数是否满足26个 代码展示 - public boolean checkIfPangram(String sentence) {
' B; U" m7 g, F- h2 A9 z - if (sentence == null || sentence.length() == 0) { 6 p8 w1 X& b' A4 s- W2 O6 n# {, F
- return false;
5 p# o) ?1 e3 m+ F5 G - }
' o2 ?( I1 s, L2 f0 C$ F9 ] - Set<Character> set = new HashSet<>();
7 t# F1 x N( G - for (char c : sentence.toCharArray()) { * Q' \$ O8 P6 f) s2 E) n2 r$ u
- if (c - 'a' < 0 || c - 'a' >= 26) { & [ p* g- {9 O: [4 U% D; A
- return false;
' p9 Z I1 ?+ t5 i# }$ n - } " Z2 p& R2 D+ N' }$ f* J+ v
- set.add(c);
9 e1 @' k* G( ? e7 n# K& ~. d5 K - }
! w: O7 z1 I7 Y% } - return set.size() == 26; 0 A( \6 ~; S Z5 g q4 v0 z2 M
- }
复制代码No.2 Maximum Ice Cream Bars 解题思路 贪心的去买雪糕 无需看成背包问题,TLE or MLE 代码展示 - public int maxIceCream(int[] costs, int coins) { 1 n1 h3 {! _; i7 f
- if (costs == null || costs.length == 0) {
' u7 |) O8 S7 y( D9 i9 m - return 0;
$ o+ f- Z+ w/ U& h1 g: | - } 7 J6 i; ?, i2 k; _$ G+ p+ U
- int n = costs.length; / |3 G }9 k# g- Q; `9 ?, X
- int res = 0; 4 ^% A+ `# T2 P8 Q4 X
- // 排序从小到大买即可
; ?! ]/ z2 K% F; V - Arrays.sort(costs); : x# ~: O+ f% {4 @& d' I: T
- for (int cost : costs) { $ n' f8 L5 l" H. f' |
- if (coins > cost) { 6 k! s" \* {( r0 [# y
- res += 1; b' C9 U3 @9 S& R, w
- coins -= cost;
( F- O. O# L' p% n - }
) t- k* M; \) X8 w7 _+ R - else {
, r k& C7 W0 l. z+ ` - return res; 2 j2 z; {9 |8 ]1 s9 r
- }
+ q3 Y0 v( J& v) I5 D$ g. S - } ; }/ g2 @/ T, |2 t9 e
- return res; 1 I; ~6 @0 s, X4 x w0 v
- }
复制代码6 {) _0 O/ y' Q6 k3 z$ [, ~* s a( G
% O6 O+ q4 x3 F1 C" m4 Q {% {& x1 u3 M4 l3 t
$ a1 d P4 |' K, e" N
2 {& ?5 Y& H. z8 O
|