[size=1em]想上岸 找上岸 No.1 Check if the Sentence Is Pangram 解题思路 简单遍历字符串判断 非英文字母字符直接返回false 否则记录不同字符的个数是否满足26个 代码展示 - public boolean checkIfPangram(String sentence) {
7 _$ u$ ~& t- S* ] - if (sentence == null || sentence.length() == 0) { 5 E- T" p/ w2 \ n, L2 o* t: \
- return false;
" z% R8 O7 I8 W, ~ - }
9 H& W# C( l8 i* v& E p2 ? - Set<Character> set = new HashSet<>();
1 [7 }8 h& ?( e' q& w - for (char c : sentence.toCharArray()) {
/ [6 R3 f* H5 h- k - if (c - 'a' < 0 || c - 'a' >= 26) { : b! k/ B/ f8 Z! E# _* z9 R( h, U0 N% G
- return false; 1 }) \0 W" U! T# U& l
- }
, ]1 r! H0 @7 @+ w1 a - set.add(c); " z) f$ s( }8 w/ ]! m4 ]( { ]' k
- }
5 i% Y3 X+ S% ]6 K" { - return set.size() == 26;
; l* K$ _+ s; o6 Q9 U! ^ - }
复制代码No.2 Maximum Ice Cream Bars 解题思路 贪心的去买雪糕 无需看成背包问题,TLE or MLE 代码展示 - public int maxIceCream(int[] costs, int coins) { & O, j \2 f1 s1 C/ q7 h5 i9 y
- if (costs == null || costs.length == 0) {
; S9 x* [* Y- B4 R/ g2 M) `% J - return 0; - l, P/ _7 Z' |; `
- }
. T( c- G' N, g' {4 v8 G - int n = costs.length;
+ J7 L( L8 V+ H3 D. d+ U - int res = 0; 4 V) {2 o# I7 P$ M. i
- // 排序从小到大买即可
$ e; V# W- a/ z$ v$ |3 ~7 w - Arrays.sort(costs);
: l( d: f" c& c6 J - for (int cost : costs) {
+ z9 h0 x4 M* k' e1 Z0 u% D - if (coins > cost) {
: J$ j" x* \( j4 q, a, F - res += 1;
7 W+ c7 T( C1 Z# r - coins -= cost; 3 D; }; z/ u5 V; |" H4 @" _
- }
( z o, I! y' a, I$ |& c - else { 3 |- L' ]" j- J: l' |- m" d
- return res;
. D+ G4 J/ C; _8 v - }
& S8 T; i1 G: ]' Z) r t - } 8 h9 c: e+ @4 {' |+ M
- return res; ' P4 _6 t- U8 b4 Z! M
- }
复制代码
* t& i- ?0 f+ x% G8 g
$ u5 {# Y4 T N
5 _# z( c. a5 w; [, V4 H! K. k f* Z9 l/ d
! `* O6 O: z0 v) D
|