[size=1em]想上岸 找上岸 No.1 Check if the Sentence Is Pangram 解题思路 简单遍历字符串判断 非英文字母字符直接返回false 否则记录不同字符的个数是否满足26个 代码展示 - public boolean checkIfPangram(String sentence) {
# ?( p: _: O( ]! _ r# L - if (sentence == null || sentence.length() == 0) { & Y4 g& M( K) E( i, v2 r
- return false; ( A1 P3 @" ^5 V6 R* S- E" Z7 Y
- } ; O" E8 r* [4 t! A( G
- Set<Character> set = new HashSet<>(); 3 x" m* i% z- R! G8 N
- for (char c : sentence.toCharArray()) { 2 T1 I+ g4 ^: R! f
- if (c - 'a' < 0 || c - 'a' >= 26) {
( ]5 n! X3 X: X& ^2 e- g4 f0 h6 b - return false; " ]8 r& I2 _, Q. K& e
- } 5 i/ l9 T7 j8 ^$ `4 r5 y& R! L
- set.add(c);
2 F* E3 m5 {* J$ F9 B+ x - }
8 g& }) ]+ i8 ]; D - return set.size() == 26;
6 u0 c. `7 x) J4 h! W - }
复制代码No.2 Maximum Ice Cream Bars 解题思路 贪心的去买雪糕 无需看成背包问题,TLE or MLE 代码展示 - public int maxIceCream(int[] costs, int coins) { ; q, C9 G' U% q6 {
- if (costs == null || costs.length == 0) { 2 l/ O& ~( F: e |& U" e! D x8 F; u
- return 0;
% x+ O- I7 U" f - }
: C" _) S* j! g" l4 E2 k - int n = costs.length; " Z* O) Q8 U* l- P
- int res = 0;
% `1 j1 i& D7 g8 d - // 排序从小到大买即可 $ n& b0 e6 K6 Q$ O {# f7 D
- Arrays.sort(costs); ( V1 V" G& e* ^& r* W
- for (int cost : costs) {
8 _& ~5 A- j L) n! X/ q6 M) x - if (coins > cost) {
, e$ ]7 \; v7 { - res += 1; ! w8 H+ e( p ]% J- T7 C" {4 ?- l6 K
- coins -= cost;
: i9 f4 I5 R$ w - } / T# f0 ~( V7 M& ~. I1 n# u
- else { / x3 p1 c6 [9 w) S% `( [4 _
- return res; 1 O3 Y/ E5 ?+ J( `7 v2 D
- } . x8 E: A( U* R$ [5 H
- }
; [6 Q* N& {+ D - return res; 1 \/ N. U0 k/ h$ H) v3 g
- }
复制代码
. a2 M1 R7 ]& I; r( E
, e1 I/ J; A$ h) g8 j
) k1 g1 ?: M: r/ d6 i6 o9 O9 y; Z$ S/ }% F* @6 j9 Q
/ Y; C3 w: i6 `# }/ x' c: }! b |