登录后可回复主题
您需要 登录 才可以下载或查看,没有帐号?注册账号
x
【 NO.1 检查是否每一行每一列都包含全部整数】$ T+ Y- Y5 ~! P2 ] v& n
" |7 f4 P; I% l9 }% v
解题思路
. T! H- O. Y& M( T1 P签到题,可以用排序做,也可以用 Set 做。( k; _' S: F% X2 `& p/ j
1 |! f! q/ ~" V- o代码展示
) _# ?" r' v$ r3 I1 e1 u3 U* `! F; z
5 G+ l, P5 [1 T! B w. Yclass Solution {
# k' F# }, y4 c5 } public boolean checkValid(int[][] matrix) {0 Q& [6 b- }$ @6 P; G* x
for (int[] mat : matrix) {
7 T: E5 r) x1 Z) |- B. B8 D6 W& p. A int[] m = Arrays.copyOf(mat, mat.length);
/ `7 M8 C K g3 F Arrays.sort(m);8 f$ `: ?& S8 F. ~/ t5 B$ }/ P
for (int i = 0; i < m.length; i++) {
) c1 r0 \! K- P6 s+ `( X9 t E if (m[i] != i + 1) {
( Y8 j5 u9 F2 b return false;* h% O1 N3 ?1 Q+ l1 T* @
}% W" T/ [& a! x2 _$ t
}
7 g8 I5 r8 v4 v" m } p( Z E- M. w6 S* N6 c7 J% b
for (int i = 0; i < matrix.length; i++) {( L% _6 c' w% @! C! g
int[] m = new int[matrix.length];2 r6 X" }4 N5 k- R
for (int j = 0; j < matrix.length; j++) {
z- S8 W# [/ D" t6 w. V% s m[j] = matrix[j][i];8 k- A9 c! {$ G0 O$ @6 ?
}: F/ ]4 u" s0 w9 Z' J. B
Arrays.sort(m);3 o4 N! w5 c+ u- w$ z
for (int j = 0; j < m.length; j++) { j7 F& s: _% \- \$ H" J$ E
if (m[j] != j + 1) {( f \" I# ?& _+ q7 x7 ?1 J
return false;
2 L& N3 I" f) W" } }
) h% B' y4 a9 _& F- T% } }. y- a+ i( r. ]
}2 Q) u+ | ]# |
return true;
7 c2 F2 f) X% c$ Y# Q }$ d5 h, g% ^! Y, r- B
}' a. c9 b( ~3 ]0 Z) o: V
& T% ~2 K/ o5 O( j4 H
S7 R: |9 I+ {/ q' Z3 M
【 NO.2 最少交换次数来组合所有的 1 II】: S/ ]) \3 Y3 {* a
& Z4 u& Y; e4 d+ M( x' E解题思路
3 m' k8 \' I" X! a# X# Z6 y首先化环为链(将 nums 复制一次拼接到尾部)。& T, Y; }7 ^4 z+ k
. ?$ @3 i8 L, A: E' D+ w) Y; r然后枚举最终结果的位置 —— 连续 tot 个 1 分布于 [i, i + tot) 下标时,需要转换的次数就是这段下标中 0 的数量。
/ w3 D9 E5 T# r, h+ u4 T) v9 L: }
4 L. t$ C( r9 j" p2 b% g7 B每一段下标中 0 的数量可以用前缀和求出来。( x/ }# f T- \- ?- Z
, E9 [3 N9 O' M- W" _& ?代码展示
. ]8 N! z1 Z, g" g3 Q8 ]7 d! h" F7 M1 |# R
class Solution {
4 k- y! D$ y% u+ X" G, x4 Z# u public int minSwaps(int[] nums) {+ _. `! |4 j/ `
int[] link = new int[nums.length * 2];; D# l2 [' m$ F1 |7 f! n Y7 S$ ^' i
for (int i = 0; i < nums.length; i++) {! n, @, Q# ]7 p B
link[i] = link[i + nums.length] = nums[i];6 n5 K0 I# b: i+ n0 z- l( y. t
}
4 U6 N- p1 }6 ]* R int[] preSum = new int[link.length];7 F& G! d- V E; z% W. e+ A' z/ ?
preSum[0] = link[0];
' _4 M3 L; F! S for (int i = 1; i < link.length; i++) {
1 U4 D! F5 {2 w5 o, T: L- y preSum[i] = preSum[i - 1] + link[i];% D( U. ?4 }& c! R
}4 |4 ]9 t2 _4 A* h! F' m5 s
int tot = preSum[preSum.length - 1] / 2;
' X q) Q% F& w0 u i if (tot == nums.length) {& k7 {# F( m/ f4 d% R" j" j
return 0;
- c3 x! Q' c! c9 K8 Y% t9 p }
8 o* D2 L) |. p; b1 W1 O7 a% v- h int max = 0;1 R0 D" o0 }0 W& q7 ~. e/ c
for (int i = tot + 1; i < preSum.length; i++) {' I+ v& g, C' g" W) j$ o$ m
max = Math.max(max, preSum[i] - preSum[i - tot]);- y8 {- H+ `# i2 _" B9 o0 F
}$ c0 h; Y3 P* F% n/ l, G6 x1 v, k
return tot - max;1 Y- v% Y* |$ j2 G8 c& f5 Y. o7 T' I
}
& h% y4 B* p1 {% p. M) F}
+ ? l$ M3 p& ?1 x& f
, M# m& i' `. b1 n; o( |1 I& ~; O3 Y/ |
【 NO.3 统计追加字母可以获得的单词数】0 G" j; b+ M8 f% j1 A* X
+ y" C0 e& H2 }" u; R解题思路
, `9 Q, M; {, m" a5 x注意审题,只能追加一个字母。2 s T. n" i" N# u0 ^' u5 X9 p
7 `* o5 X% T: e; \+ x2 l因为题目不要求顺序,所以我们可以使用一个 26 位的二进制数来表示一个单词。
% C8 |( e- y2 Q- x# K* D: ~) s$ `( b7 f4 I' B) Z0 U
代码展示" v% f. k7 S9 y9 K" Z6 F
; L. Q `2 N# l. X8 C; T) b9 K+ P
class Solution {; `, V" k: ?2 }3 Y+ w/ v* c$ H. n
public int wordCount(String[] startWords, String[] targetWords) {
% f3 I% D" q! @* X" a Set<Integer> startSet = new HashSet<>();" z: \7 u8 |; e1 t2 q g
for (var s : startWords) {! e! b# Y4 k! P) l: [
startSet.add(toBinary(s));
. s: s6 [! i8 E; f$ E3 i8 y4 E% I }
7 ~* p! T- e* u5 I2 | int result = 0;( t4 `8 B9 o* X& _
for (var t : targetWords) {
8 Q) K' ~8 n8 p5 B C int target = toBinary(t);
0 K+ `- n# J) ?3 g // 枚举追加的是哪个字母( V2 M, [7 ]8 K; Y; b, d: E* s& z# B
for (char c : t.toCharArray()) {
% W# b& B- |4 s2 t+ b if (startSet.contains(target - (1 << (c - 'a')))) {
% n/ A5 t# O. _) D8 M' @, j# T result++;
/ L6 g) X& f9 l( t- z' W break;
4 V8 g8 V$ [; ]% A- |; h }2 k" i' j# m# `+ _! \" Y
}
9 Y4 M$ p$ i8 e" | }
9 |7 p1 A3 ]5 G Z- G return result;
, p. i* B) V" ?9 h }: Y B1 j- E8 E9 q
7 M7 x& A v4 s& A9 z* l
private int toBinary(String s) {; D0 g' m( |6 E% X# r& J
int res = 0;
0 Z2 ?( x+ A) _: _+ F9 Y for (char c : s.toCharArray()) {9 B$ r$ P: v; D, F. Z: Y L8 y
res += 1 << (c - 'a');7 K3 `. D3 P! W' R- V# f" n
}# o! f' D% b- \3 a3 A. \/ e
return res;
0 r# y: T0 z4 p" O+ H: x }. A: b% l% y% |- B/ v' [
} f# O4 L! Z, s' ?
. Y+ j' t2 @; V% O9 n
! s+ \( I6 n" c( Y0 y【 NO.4 全部开花的最早一天】
9 d- T8 X3 F2 P( T: j2 b8 C8 s9 g5 D2 x8 n9 g8 r. T. k4 F
解题思路/ W' X5 b) B% S
贪心,先种开花时间长的即可。0 K* [' }" K, K( w5 J3 |
! y y: U \+ v3 \. d代码展示1 c- t: t% G! F0 n
; W: k5 V% ]2 W; M7 A# k
class Solution {
! L- [% T: c- e' ~ static class Flower implements Comparable<Flower> {
b4 c3 }4 v9 P r) o public Flower(int plant, int grow) {) l" _1 O+ ?) m5 T4 j
this.plant = plant;
4 n/ H( ?/ i9 Y3 l this.grow = grow;
) W, e: c3 J; i& J- |, m this.tot = grow + plant;
7 p. ]0 D% l8 | I }. c7 t( w/ J. g, H0 X
7 G/ g. p( |! K) x! F5 f) N
int plant;
- d, R& o( B! H8 a3 `* Q int grow;4 K& K6 ]0 P/ c4 d+ |1 F1 K
int tot;
0 f: Z# {. Q6 W5 [) R
3 P" h8 ], h) x( t- Q9 d- J @Override
2 J/ {4 B* [; ] public int compareTo(Flower o) {/ v- Y; H) t% `/ p& ?
return o.grow - grow;
+ R1 ^" y6 N, }6 C+ J }
) S: u0 {& j: F/ x, g6 h }% C1 F5 C6 u# h$ T
; W( L0 W: C- i7 F
public int earliestFullBloom(int[] plantTime, int[] growTime) {
0 S: U3 `; _- J. B* ^ Flower[] flowers = new Flower[plantTime.length];5 h/ C; E9 _" i" [" n8 s
for (int i = 0; i < plantTime.length; i++) {
* h: I6 B% I+ k) j$ }9 s flowers[i] = new Flower(plantTime[i], growTime[i]);0 [; |( ~# ~' ~' Z+ T o
}2 x9 _5 b* K9 e9 t1 u
Arrays.sort(flowers);
( Y1 m' _9 w( S: _# x1 B: Q( Q int result = 0;5 d1 l) t( K; p# W
int current = 0;
: t3 ]/ X0 p( q; Z+ R! O: ^" U; ~! W for (var f : flowers) {/ E# B7 A9 p- G: @* `
result = Math.max(result, f.tot + current);2 a7 l# ?3 |4 u5 H* k
current += f.plant;1 Q0 K7 N! d/ t0 ~
}/ E4 j0 }* d. F( c8 b9 K/ o
return result;( D b+ }, J1 z
}0 K6 M2 s1 \# ~
}" `1 e+ u/ G2 Q, C0 l* h
|