|
[size=1em]想上岸 找上岸 No.1 Check if the Sentence Is Pangram 解题思路 简单遍历字符串判断 非英文字母字符直接返回false 否则记录不同字符的个数是否满足26个 代码展示 - public boolean checkIfPangram(String sentence) {
4 K2 m: {4 {0 ^# w6 Q - if (sentence == null || sentence.length() == 0) { 4 y0 M4 |# d7 ]$ i
- return false;
_2 Z! ^1 ?3 f3 y - }
. G) M4 u! P+ X! r1 V6 T% h# i - Set<Character> set = new HashSet<>(); 9 X. U# R8 ?; B0 L1 S6 a$ @
- for (char c : sentence.toCharArray()) {
2 O- X8 g! ]. f/ E: z) \8 I - if (c - 'a' < 0 || c - 'a' >= 26) { 2 L' x u; R7 n4 v" Q
- return false;
, ?) G3 |- V& _ - }
" |- a* Z& Q3 t( ] - set.add(c);
% I1 `/ ]4 f- ?7 j6 C/ u' {8 g - }
1 _ P3 l$ A. n5 M! f& X" I - return set.size() == 26;
; \: m- i; r! N - }
复制代码No.2 Maximum Ice Cream Bars 解题思路 贪心的去买雪糕 无需看成背包问题,TLE or MLE 代码展示 - public int maxIceCream(int[] costs, int coins) {
. ~$ o7 x/ s! N4 g" U9 L - if (costs == null || costs.length == 0) { " b9 \" ] E. x3 q) d0 k5 y2 T
- return 0;
* v0 ]5 v% c4 a - } 5 V! w. S( K: T
- int n = costs.length; 3 ~* t; n. Z f, [0 q
- int res = 0;
9 W+ \+ g ?) g% T6 [ - // 排序从小到大买即可
/ D- R& o4 |" N8 ^3 \5 A - Arrays.sort(costs); ( S! D4 v) s; t' ]5 i# d
- for (int cost : costs) {
* m; m9 W0 V9 ? - if (coins > cost) { ! g% J: _6 t, Y5 D+ n# y
- res += 1;
4 v) w- N! T3 s+ `7 U/ r - coins -= cost; ' n( i/ z/ x5 x. n8 I$ q. V
- }
) I; ]* l4 R" ?/ Q5 ~7 c - else { # s7 @! R, V5 |' j4 y/ ^" m3 t
- return res;
, K( `8 \9 w& \2 G/ ? - } p: J# i4 i8 D: y4 `( I
- } 9 _1 k$ r6 P" \: Z5 K8 ~0 i# o" { @: f
- return res; ! K+ Y2 R" j6 D" s1 D* Z8 n: l
- }
复制代码& A' J$ j: c0 K/ z
* o! e4 I& A5 F
, F6 u! }) e! G
( u! n1 B) i3 F4 v# m. a! a8 _) s2 g" u$ D
1 p& ~6 M* u$ N! }6 Y; l |