登录后可回复主题
您需要 登录 才可以下载或查看,没有帐号?注册账号
x
【 NO.1 统计字符串中的元音子字符串】
4 m+ J3 z: m8 {8 Z. |$ v解题思路
- k+ j+ [: }$ v7 N' \+ {$ Z签到题。& M2 b0 u6 Y! T8 p! Y3 g
- @) C: u' n' b9 W代码展示
; ~5 H+ i+ i7 i2 ]4 O; N% R5 N( M6 t a
class Solution {# K. J" d3 h0 h! ~7 Z- g) h& u
public int countVowelSubstrings(String word) {+ n& j$ B9 y, Y8 a0 d; y. o
int count = 0;
: c5 i0 H9 p/ v, A for (int i = 0; i < word.length(); i++) {
! s( ~$ t) B# g! X5 p9 B8 V for (int j = i + 1; j <= word.length(); j++) {
s1 u% X+ Z( [2 x& A count += containsAll(word.substring(i, j));6 v2 F; ], T9 X: A$ {5 f
}* s0 B0 H# X' M7 P7 x
}
3 W' o! X7 S, Z return count;/ k+ A0 v8 S. X3 E
}2 A5 @( p" h5 k" y+ H9 q
, {) P# r, N3 P3 }
private int containsAll(String s) {
; H9 P( A, f+ v1 t if (s.contains("a") && s.contains("e") && s.contains("i") && s.contains("o") && s.contains("u")) { h; z0 N# c2 j( Z% m6 P
for (var c : s.toCharArray()) {; g7 E0 A! S' _6 w! k: [2 q
if (!"aeiou".contains(String.valueOf(c))) {8 G v( U3 X# z
return 0;' Y. K1 i3 S2 X$ H3 z
}
) o* j" @+ @4 a( O$ k! r/ t( O }
2 ~& S( q+ X1 D2 N1 _- L5 { return 1;
( d$ L0 a, }, m C: O( y i& e }
- P* L9 ^4 r2 o7 F1 s return 0;0 |; n7 @+ a" X5 F& ^; q
}
- x; h @+ v" q}4 `% |; q6 R6 [, L1 S Y w
+ f; |7 b4 B, t【 NO.2 所有子字符串中的元音】
7 d- l9 n. C1 z9 A/ K9 M: s解题思路: M" [4 X# T- G! ?
依次计算每个位置的元音字符会被多少个子串计数即可。" {9 W' A+ ?) e) Z) k
. D8 o, L* n8 [3 V/ B' `& B, Q6 I代码展示
: a* |; @8 k: t
* q# }- L9 [ i# z' q- K v% Nclass Solution {! h6 Q7 j! b7 Y& D" u
public long countVowels(String word) {
( X1 r" x: k" M0 h" m3 ^ long result = 0;
# U# f6 S; i% J0 s9 Z0 m for (int i = 0; i < word.length(); i++) {- S% {0 P1 v5 ~/ y0 ^
if (!"aeiou".contains(String.valueOf(word.charAt(i)))) {8 S$ z* Q8 p& K) o/ u
continue;
! S1 p# @% l4 T }
K7 x6 i6 X% N- n0 {* k! L3 I2 ` long left = i;- U) C/ O, O/ N" e4 g
long right = word.length() - i - 1;2 |- X- L* ~+ p9 c* G
result += left * right + left + right + 1;; z6 L. @- s U3 l/ X- H& l
}
! ]' J' E y8 ^9 K- ~ return result;
- e& C. V9 h% b" J+ N% r0 U }
# ?3 J/ w' W9 O! ^( i$ S}! w+ q- L& n% ]" l+ ^4 @* u
# e& N* q- K- F( k: ~+ F# `" k0 g
【 NO.3 分配给商店的最多商品的最小值】
4 ~: G2 }4 r* b; _% u解题思路
9 U) p% T0 V$ M: D8 E二分答案,假定一个商店最多能分配 x 个商品,那么我们可以轻易计算出需要多少个商店,即可得到 n 个商店能否分配完这 m 种商品。: ?5 C( I1 m3 w4 k& _
2 I1 E( H& |. @6 @* y5 G代码展示 o+ E+ ]' Z& s" _
* \7 ]( O5 f$ n9 \0 E- Kclass Solution {9 ^/ n/ O, T( C/ v; V
public int minimizedMaximum(int n, int[] quantities) {" c- a" t; b) c! `
int left = 1;
3 y# X) A7 N! E0 \ int right = Arrays.stream(quantities).max().getAsInt();$ q+ [' N7 E" `7 l. D& P7 G
while (left + 1 < right) {
6 A8 n; W' R% z. z0 E3 G0 f q* h4 E int mid = (left + right) / 2;
9 L# S$ d- L% c0 v3 F6 A0 N if (check(n, quantities, mid)) {
. h# [) P( }" }" ]- [7 ]" }* g right = mid;
; C- k0 C4 s8 h7 C3 k2 [ } else {% x) q: X; s4 ]1 D5 x+ Z
left = mid;) F6 ^3 S; j: }2 f; G8 {2 o: x7 H
}! g: ?$ z( ]: e, k3 a$ A
}
5 z& ?/ M( a9 [- ~5 T! y3 d4 v return check(n, quantities, left) ? left : right;; w0 U8 Q1 L2 E4 Z# ^2 O3 d- ~* a
}
1 F0 M4 m/ M& Y; `& [) l: B) K# V
private boolean check(int n, int[] quantities, int x) {
" Y5 U5 v9 S' ?/ E, j5 T _4 k int cnt = 0;$ T( D$ z: l5 ?7 d% d1 Z; d+ v
for (int q : quantities) {8 I6 w6 v+ J( l+ T# h1 z9 F+ }
cnt += (q + x - 1) / x;4 Y! x+ I; Z1 C3 j& f0 V ~" n
}$ o# `& ?5 K' z v# C! z3 h
return cnt <= n;4 C7 w" y9 e$ ~$ ~8 L- Z1 ] |
}
/ G$ S. E+ [6 i8 w L- p}
" |9 l" Z- S& G6 t+ k6 L3 @: e9 R0 N2 |4 M
【 NO.4 最大化一张图中的路径价值】
* R, r7 c. p3 O* t, `解题思路
. j. k$ u( [& h看似复杂,但是观察数据范围,发现直接回溯即可。
. L" u! o; f8 y3 p: y0 s1 V6 \6 E ~) E) l ~) R \$ @
代码展示* U3 M1 p0 u p2 i+ x V3 T# A+ D
4 K7 D' p6 q" R
class Solution {; R; k6 s6 v# d
int result;8 S" g1 r1 P0 f
List<Integer> empty = new ArrayList<>();- w/ J/ T* L6 H* `7 I0 y
) e) }8 N) K5 [2 k4 U2 R
public int maximalPathQuality(int[] values, int[][] edges, int maxTime) {
4 m% D0 w" w, Y0 F+ E+ ^ Map<Integer, List<Integer>> children = new HashMap<>();
! [* v1 P. A4 n" } Map<Integer, Map<Integer, Integer>> times = new HashMap<>();
, N! j0 N2 }9 l3 f for (int[] e : edges) {* Y- ^! ^1 D$ p- I
if (!children.containsKey(e[0])) {
% ~& h5 @* O' w$ j: V G) o children.put(e[0], new ArrayList<>());
1 z5 L& u0 N; e7 C }
8 d% e, s8 b: s if (!children.containsKey(e[1])) {3 d' [3 l" ]' S$ A+ |: S0 \
children.put(e[1], new ArrayList<>());/ _6 M/ J. l0 l% [( Y1 [
}& j3 q2 O" V( H! H4 @+ M
if (!times.containsKey(e[0])) {$ r! D6 m W6 ~ j$ f
times.put(e[0], new HashMap<>());
" H6 f) }7 N. N8 v6 w }
2 K G. \$ M, V+ \( f! S# b if (!times.containsKey(e[1])) {7 \: C3 h. ^ a1 I
times.put(e[1], new HashMap<>()); Q0 u: _6 k. D9 b
}
) N' N6 ]7 N% G3 h# N8 K) L) i: S; B children.get(e[0]).add(e[1]);
9 f2 D8 i: @0 E8 U children.get(e[1]).add(e[0]);. K5 M( s$ N9 n' l2 T
times.get(e[0]).put(e[1], e[2]);" a9 g8 ~6 W* p/ | ?& U
times.get(e[1]).put(e[0], e[2]);0 |# H: d: r$ t9 k. N c' s% k
}- p5 F9 D( f- X% d# c7 u, K+ r
int[] vis = new int[values.length]; v }& d# q; L+ A2 r) a" |7 f
result = 0;
" f b& s8 Q3 f m2 ]" L" g' e9 N dfs(0, 0, 0, maxTime, vis, values, children, times);% q2 N- }1 q5 l: s6 @- L
return result;1 r0 ~, F' H# d- A' ~
}
& T+ D0 A) h4 Q
4 q6 m8 r u- E2 Z8 n$ V private void dfs(int pos, int sum, int time, int maxTime, int[] vis, int[] values, Map<Integer, List<Integer>> children, Map<Integer, Map<Integer, Integer>> times) {$ s' E0 W! r- p2 O s
if (vis[pos] == 0) {$ m6 k/ r: S7 o" S: H3 ^
sum += values[pos];+ s+ X6 }- R X9 T; ] \( f
}. _5 Q F3 p- n
vis[pos]++;
( }: N1 _: w3 k: m if (pos == 0) {
# g1 S8 n* b( f result = Math.max(result, sum);8 h3 F6 m' g: q
}
1 B3 j, H! C1 J) G" ` for (int nxt : children.getOrDefault(pos, empty)) {
, l4 j) q1 p4 T6 V; e0 L/ u1 x" o9 i3 g/ ^ if (time + times.get(pos).get(nxt) <= maxTime) {
4 |% i1 V4 \& w9 [, x0 s) L, e9 F dfs(nxt, sum, time + times.get(pos).get(nxt), maxTime, vis, values, children, times);
9 R( {# D% T5 O( F3 K8 O; e1 R }/ ^' _* t. f7 J) ?0 D2 f8 x
}
/ x( o9 F' V/ F: j vis[pos]--;% ^) p5 C- O, y; O
}
! w0 \! N, {8 t; N' O. P+ R) q}
7 h4 d$ ~6 S. ?# H( l. h% K |