登录后可回复主题
您需要 登录 才可以下载或查看,没有帐号?注册账号
x
【 NO.1 执行操作后的变量值】
" C& Z Y }' A9 o% k6 c解题思路
( N2 V3 \9 ~2 R& l" \: v4 g签到题。0 N7 Y' x C5 |" o
* B4 z( `6 C' r) z% O代码展示
6 v& O; y7 N6 P' Q
. Y! h7 e+ C. j$ k& }. x3 nclass Solution {' |$ W* B% H5 a, H
public int finalValueAfterOperations(String[] operations) {8 }5 o- `4 t/ m) ~# W1 _) }& O
int v = 0;/ {1 s3 x6 S# \
for (String op : operations) {6 v5 p& d4 x0 q. Z: c0 l% [
if (op.contains("++")) {
$ J# U3 {* g) N0 G0 D5 r; B5 j, g4 \ v++;, w: O& G" S: X; Z' f
} else {
( W5 F' e- U" t @/ |: ]: Y v--;
# V* b. i+ U A* e* g7 [5 T }
' A$ D' T: q) U! m% b1 F1 J" t- c }
7 ?; Y$ J- f" q, J: n) ]0 y. j return v;' ?! }- ]& [( h8 U
}6 i: Z6 {/ C' C! _/ k
}8 |2 Q% ^. k6 f: k w
( |7 j' k8 [5 ?( o
【 NO.2 数组美丽值求和】- N/ I7 c. l4 \9 `8 g1 l7 s. S3 n
# g- E( z$ v% ^5 i! d% Q
解题思路% [% ~3 p- Q+ M* e; x' l
由前缀最大值和后缀最小值即可得到中间元素的美丽值,所以预处理出前缀最大值和后缀最小值数组即可。 I' A/ z4 t) ?3 I3 z- c/ ]7 N% {
+ a: r/ {$ o+ J/ \& r3 d( h+ E
代码展示3 Z6 ]+ K1 q: C+ s# ~
6 K% T7 T2 D! O! N9 b A( Hclass Solution {
' K9 J4 l/ d7 @( G9 ?$ B3 e public int sumOfBeauties(int[] nums) {5 r, i8 o/ C( J
int[] preMax = new int[nums.length];
) Q# q. t9 x% P2 g4 q# r9 Q/ b preMax[0] = nums[0];
M2 n* H+ Q3 f* e! H5 |( _ for (int i = 1; i < nums.length; i++) {$ x Z: H8 W( M" V. f4 m8 s
preMax[i] = Math.max(preMax[i - 1], nums[i]);
8 f- s5 U( o6 e6 _' G3 | }- e' O- S9 N$ K( {
int[] sufMin = new int[nums.length];
1 D1 G+ z# L5 z7 R# }- v sufMin[nums.length - 1] = nums[nums.length - 1]; j& F' Q- C+ B1 {3 t* t5 n+ q( A( T
for (int i = nums.length - 2; i >= 0; i--) {( Y& N) b e* W; E
sufMin[i] = Math.min(sufMin[i + 1], nums[i]);6 t2 b; m, s0 q+ w
}
7 g* } w# E3 Q) c int res = 0;! f2 }: J7 |/ f
for (int i = 1; i < nums.length - 1; ++i) {
' a1 A! a' R5 m& Z9 F0 V if (preMax[i - 1] < nums[i] && nums[i] < sufMin[i + 1]) {# _5 k7 w& M, c
res += 2;
. T2 P! R1 A5 q0 L7 Z } else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1]) {+ I. Q1 V5 o& f& x \, F4 c3 h8 L
res += 1;
6 V& U( e$ q" P) J }$ J/ J" s& ?( j9 d) }% z
}
0 C. g% d9 t1 H G- r return res;% x+ b; s7 F2 q- ]% V* Y
}
8 |1 x3 h& f' ^0 [}' |& i ^1 t; o' B# n" ^9 G4 W
2 D3 }8 B, X1 e: p) d
【 NO.3 检测正方形】
' V7 m e9 P5 o& I7 l [
0 V! P9 ]. \ x' M解题思路
' O; T3 a! R t使用 Map 储存所有的顶点,然后在 count 查询时枚举对角线。. |7 A) `& |" p8 Q
1 [, t8 |1 Y3 N. _2 @代码展示
7 N4 _1 q8 Z8 _& e& p+ c1 f0 F6 k1 v* U( t+ F& a; a
class DetectSquares {
0 n) s1 [2 t' _' t5 i7 i. W* I7 c4 Y6 _% G
Map<Integer, Integer> count;
, x: j6 k3 E: n5 n6 q3 z' r+ z Y" L$ ~$ ~( \5 L; X" }4 G( Z! q
public DetectSquares() {: r% W1 L; d- S$ c: ^, [
count = new HashMap<>();' T3 f& p- i, F J ]( U
}
/ W9 N Y) E. f7 {
. q2 e) {% T! Q: c public void add(int[] point) {
6 r& g: ]! P! }( [+ A int c = comp(point[0], point[1]);5 m: o5 ]. j, [! G v
count.put(c, count.getOrDefault(c, 0) + 1);4 n3 J. s; w: k9 h: x2 N
}
8 n! q8 m: n7 m* b- R6 x6 r: e. P7 F/ W+ ]1 F) |8 n* O
public int count(int[] point) {
f, X# p8 Z, b6 c) t8 t& H" U int res = 0;
+ P+ l. X4 t; P8 }+ a+ E5 ^& H for (var kv : count.entrySet()) {. P& h# M% j( O% p
int x = X(kv.getKey());
: n! G* {% _% L+ c( E( Z int y = Y(kv.getKey());0 h+ j0 e& ~2 I" l/ q
if (Math.abs(x - point[0]) == Math.abs(y - point[1]) && x != point[0]) {
: {) [# `! x' m/ t; i4 w res += kv.getValue() *
: z+ A6 F+ m0 b2 l' e2 Z count.getOrDefault(comp(x, point[1]), 0) *
$ @5 C1 V! b* D! F1 \ count.getOrDefault(comp(point[0], y), 0);
; l- |6 o4 J p, t$ {) O. p }
# r6 Z* {' t% j5 i$ }, | }
3 @: @! I% x+ v return res;* h% u) S' L+ i& j% F+ M
}
/ o; D' B# J8 A$ j2 O. \; u
; _# I) {) P5 }& b private int comp(int x, int y) {, l; W6 u/ o/ Q- p. D9 X L W
return x * 10000 + y;
+ B2 i3 z7 v9 V }
5 R( b, c6 q$ H/ A8 T
9 Q" F) u2 P! L: e7 f private int X(int c) {: H' n- M! E, k% w
return c / 10000;$ H7 B; r& O+ H
}; e( x) D6 |/ C6 `
$ _( J/ d( P; H) k' }, V5 a9 r0 | private int Y(int c) {
' r2 D. K$ N, c6 \* ]& R return c % 10000;, k6 `. l# C$ ~/ d) S9 W; @. o- N, \
}
3 ?# D7 i5 R @) X, a9 C2 L}7 C% s/ Z$ a% K0 `
( m6 v8 |' y( H4 j- O, B
【 NO.4 重复 K 次的最长子序列】0 a( R& D; l E, P! t, z: o
3 O6 d' ~/ [6 g# ]3 `解题思路
* u; b3 e @5 j4 F注意 2 <= n < k * 8,而如果一个子序列想要重复出现 k 次,那么这个子序列中的每个字符都至少要出现 k 次,所以说答案的长度一定小于等于 7。
8 O8 v3 f( ~7 {8 X. |! D& P0 Y7 V4 z/ W6 R& Q5 r! |" G3 x/ `
我们首先找出来所有出现次数不小于 k 次的字符,然后枚举这些字符的排列组合,依次判断每一个排列组合是否出现了 k 次。
1 {4 l( M9 p% H
7 A4 r6 _* J2 ~! v B& K8 x; P, R代码展示
9 t. m' Y: \* d. U+ i$ Q
2 I* z* `" o7 M/ q9 b) ^" I# J* O. Bclass Solution {
/ u' J1 [! A' a" Y, A public String longestSubsequenceRepeatedK(String s, int k) {# T2 }" ~4 B9 s; ^
Map<Character, Integer> count = new HashMap<>(); h2 m! U0 F2 S
for (char c : s.toCharArray()) {) [4 w' R7 F) P( N8 P
count.put(c, count.getOrDefault(c, 0) + 1);
i% x, Q1 h2 f5 P }% `3 F2 F; t! [- m. v7 @
StringBuilder s2 = new StringBuilder();4 i. o& R+ e, }4 q( c2 T; g
for (char c : s.toCharArray()) {& D1 l: x) |. b. m* K: Q M
if (count.get(c) >= k) {
/ M) r+ y$ g* j X s2.append(c);, J! k4 u d4 z9 }. f
}
8 | K, E l% _3 ~& q( q }
3 p; A/ }, f. Z H6 S3 j count.clear();
. H/ P h% L0 q+ Y, U. a& ? for (char c : s2.toString().toCharArray()) {6 L. J* V/ J. e5 p7 ~
count.put(c, count.getOrDefault(c, 0) + 1);; ~4 g& Y6 }9 Q: O1 h7 W4 W1 D
} n O& T4 d3 n# [: n3 U b
return solve(new StringBuilder(), count, s2.toString().toCharArray(), k);+ j8 A! n* m5 E
}
' R) |: d% G3 c& A7 d$ [) Q9 S
' |1 q# O5 O% f7 i0 o# i f s E private String solve(StringBuilder cur, Map<Character, Integer> count, char[] s, int k) {
^( a( f2 [! I1 s) ~ String res = "";% l3 k+ D# g0 ~7 H( S! H
var keys = new HashSet<Character>(count.keySet());4 V# v+ _4 u: X+ ^4 V$ Y! [0 t0 Y
for (var c : keys) {
" g# [: d7 @- F& W' p) @7 N cur.append(c);9 O }! \0 S! a3 Y
if (comp(cur.toString(), res)) {7 G! o/ W# P+ o
int cnt = 0, idx = 0;
1 i; h: l( m1 \- D! f6 ] for (char cc : s) {
& l6 s0 I. W% M5 }. l! M% z if (cc == cur.charAt(idx) && ++idx == cur.length()) {- M+ y5 X: ~* Z& W* R/ ~
idx = 0;
4 P$ E* p8 [, h$ `# R: ` if (++cnt == k) {
0 f+ Y$ F1 t. d& d/ z8 \3 l# k res = cur.toString();( G* h# |" S: \/ N `8 W# {
break;
: A: w, s( X& G( X* b! o }6 d% x4 y! i: H" _/ E: J5 ]6 ~
}
. b. c- }5 X4 ]9 C# \9 g8 s }; G4 b! u* M. f
}
0 o3 l1 {( |3 u5 b/ X( q6 h int bak = count.get(c);
& `8 i( T+ V0 T! [4 A! m if (bak - k < k) {
9 Q1 I' `4 E8 _ count.remove(c);
, q% v# Q) E) g( e' n- v9 f9 o } else {7 F' @7 v/ e1 p" Y, e% x
count.put(c, bak - k);
) B0 H8 R6 V9 v2 Y0 @6 W }; G2 ~+ e# F6 g$ p& Z" U
String r = solve(cur, count, s, k);
( j- T( l" _$ x. J1 c if (comp(r, res)) {! A* P6 _- } F: t. ~% X1 w: h- B2 ?
res = r;
1 v! V/ l8 G+ |. n" k; g }
, R2 G# d" W3 x$ k4 Y; R cur.deleteCharAt(cur.length() - 1);2 f, d3 }6 F& w A) ?2 Z
count.put(c, bak);
) s9 Q7 l% b) h* l/ [ t }
! L1 z% e/ p1 M8 R- j return res;0 y4 R9 l1 U/ s: V2 ~8 e% K k
}2 x) W3 B, J1 A
: h# a: W, L; H7 w private boolean comp(String a, String b) {
) }5 w. v9 ?- r$ E return a.length() > b.length() || (a.length() == b.length() && a.compareTo(b) > 0);1 O3 U. T: }+ A5 u$ x5 @
}
9 [0 @- c/ b, X/ j2 {: t; b} |