登录后可回复主题
您需要 登录 才可以下载或查看,没有帐号?注册账号
x
【 NO.1 检查是否每一行每一列都包含全部整数】! w# y$ H, y6 @9 _ j) M5 x
" V! z/ |- B' c1 G3 y2 P% Q& ~解题思路" z! ~# n) D* H
签到题,可以用排序做,也可以用 Set 做。
, s- @& R/ [( x' A! [1 `# m# D' J8 V' R
代码展示
% }6 P2 z6 B6 h7 Z6 h- ?9 t% F$ T3 U( g0 ~' d( L, D5 S
class Solution {; }5 n3 t% }, h% o& T
public boolean checkValid(int[][] matrix) {1 f/ U* t& V, Y. g4 ?4 ^- z, b
for (int[] mat : matrix) {8 g0 R2 Y( C! L' P. `0 l# p3 t
int[] m = Arrays.copyOf(mat, mat.length);/ S" e, d! D$ W2 J
Arrays.sort(m);% u6 D' L+ K+ K) \
for (int i = 0; i < m.length; i++) {$ j) ^7 {2 t+ w+ Z) P5 m
if (m[i] != i + 1) {& l e9 o8 N; p
return false;( H* g3 ?3 w: k/ `" Q1 c
}6 G, @: {: P; L, [( X
}
7 C D6 q! l. M }# ~- M2 a( ?6 U
for (int i = 0; i < matrix.length; i++) {
2 v# S' P6 z6 S. w: Z int[] m = new int[matrix.length];5 s. E/ ~) C2 j$ n- k2 Q' T
for (int j = 0; j < matrix.length; j++) {3 y) a7 C7 l- ^% D Q* m0 M. G
m[j] = matrix[j][i];
% \6 r; q" P' a2 d) w. G5 M }
% }& l- J& m1 X+ I6 T! @- K Arrays.sort(m);+ R( q9 m4 |8 w
for (int j = 0; j < m.length; j++) {
R( a& j9 j D( A if (m[j] != j + 1) {, p* H# \+ T- l; B6 c
return false;8 z, V: w4 y; b% u
}
0 X' ?- c1 x" F: r# P } ]: v" p- a% c! x a4 G l
}
$ `+ o& V/ h0 K- K return true;
+ l: B$ l* t0 ^- P+ n* U; W+ U }
' N7 U t' {. W/ U( Z}
' U8 E8 A8 C2 V7 R* c$ Z! ^7 G0 {$ }# b/ X
4 e/ V* [9 X8 ~0 Y8 D: a【 NO.2 最少交换次数来组合所有的 1 II】% P# Y1 J7 l# \) @9 {
. T+ i; _, D0 Z- `/ N2 |! [2 F解题思路
; @) H4 c7 [! t( l3 u首先化环为链(将 nums 复制一次拼接到尾部)。4 y$ j- v4 r) D. g2 }# `5 M
/ r1 {+ k, @$ [2 U; }* B
然后枚举最终结果的位置 —— 连续 tot 个 1 分布于 [i, i + tot) 下标时,需要转换的次数就是这段下标中 0 的数量。
1 |3 l( K- Q2 [' h* y5 {, A5 y" [/ N1 ^3 p/ r
每一段下标中 0 的数量可以用前缀和求出来。
+ y/ A' p: V& [$ b) s* Y0 U8 Q5 P# I" I x h8 i
代码展示
& z- g; y7 o# c
0 Y L$ r3 K, bclass Solution {4 _% f$ t) G) ]. u8 r$ s2 N
public int minSwaps(int[] nums) {
. D% B. X! u* L7 T1 y g int[] link = new int[nums.length * 2];( b$ V X: T1 \
for (int i = 0; i < nums.length; i++) {6 c% i. |" `2 V& |6 T0 V) ~$ r
link[i] = link[i + nums.length] = nums[i];
7 d- r y* x8 R/ B/ B! q }
/ x3 e% X: L6 q% P: \ int[] preSum = new int[link.length];
: s# x/ t6 y, D/ i4 J% q preSum[0] = link[0];8 o; e+ `7 k" A. n% H3 Z" @, _
for (int i = 1; i < link.length; i++) {
/ h. Y$ z& {% w, ]0 a3 X: y3 m preSum[i] = preSum[i - 1] + link[i];8 D" ?# o K: H
}
+ F3 X% W* A# V+ o0 o int tot = preSum[preSum.length - 1] / 2;
5 F" Y5 L+ T& A! M* s/ ^% m8 h! I if (tot == nums.length) {9 a3 G$ I' ` c% {
return 0;, }" o( h! D! _0 L6 _8 C
}
' g# w( m: i6 o+ r4 X% u9 J( r# r% w0 O int max = 0;
+ U& q. I. h- S; s5 l for (int i = tot + 1; i < preSum.length; i++) {( a. V. g- ]+ O8 t, e
max = Math.max(max, preSum[i] - preSum[i - tot]);$ [, w2 u6 L0 `& X# h' e% N
}
* R9 x C- r) b2 S return tot - max;+ m8 _) n& M ?6 ^1 |6 D
}
3 X @* n9 @9 `1 I) g9 Q}
( _4 e h- {# {* |1 q6 Y5 T, {( s
0 D7 |9 U. w- v& E& Z, C
|* Y. B u3 A- i3 f5 @# G【 NO.3 统计追加字母可以获得的单词数】
0 D/ A5 V: `* F0 M$ r6 D7 X* ?; K7 a! J! q% B u
解题思路
1 a1 K5 G( m M7 _3 E- z2 i注意审题,只能追加一个字母。- m/ R$ S5 {4 E% M& s9 G/ D, O# G
, r; ^$ J* z& G/ W
因为题目不要求顺序,所以我们可以使用一个 26 位的二进制数来表示一个单词。
) I' r r9 }1 N, `- @2 @
5 ~0 I2 G0 ^5 o" K6 J4 n+ j代码展示8 c' A5 l3 U) y1 s1 c! r' N
+ _# M9 }) E" |' y( qclass Solution {3 s3 y% E& p% A; O2 m7 m2 J
public int wordCount(String[] startWords, String[] targetWords) {) u! E. d$ p* C' @9 M" N
Set<Integer> startSet = new HashSet<>();
1 ?4 i, y/ u/ r- E6 ~5 I for (var s : startWords) {
9 N* H% Z! v' D: q. j5 U4 A( C startSet.add(toBinary(s));
z2 Z) d. v+ [. p3 a( i1 _ }5 M2 A7 ]. t( @
int result = 0;
% U4 X2 Z% n+ T- ^ N0 h4 D for (var t : targetWords) {
# A( |4 w# F+ C; x5 d' {/ `5 R: ^ int target = toBinary(t);
$ R0 }/ \& m$ K, I- v. u1 O // 枚举追加的是哪个字母
8 n% {6 e9 w/ F& w' J for (char c : t.toCharArray()) {4 [: X# Z% J! D8 M3 D9 a
if (startSet.contains(target - (1 << (c - 'a')))) {4 b- x( i: u! A! M# C
result++;
2 {4 B; e8 Z. S+ k break;6 N) {, `' w3 f7 {- D8 F+ Y
}+ Z; J4 x/ Q6 v% ]8 D8 [
}
+ {4 ?, q# B0 | }8 Q" q% D* W, v4 @* T1 f. ~
return result;
U: s k2 K& p L }
) e9 |9 \. Y! M3 H4 z! k0 t: K" g( E7 G9 k) h: C3 S c
private int toBinary(String s) {
; M$ L" x8 s3 @ int res = 0;4 r4 }" Q% `- C8 @
for (char c : s.toCharArray()) {
- P$ U5 s E3 }; M- j res += 1 << (c - 'a');; _- w4 I* x; x
}
' E- Q$ q2 ]4 b$ ~/ R return res;* w& v8 D& F+ j6 {9 P. x
}$ Z& Z) T9 P" R2 i
}9 [" ?. `' X, s% S
0 ~- w0 a7 w7 P/ G
$ y7 [/ U+ T! { w4 ?* A) p1 L& w
【 NO.4 全部开花的最早一天】) ] S' @9 D: r' J, i- F R
8 M" j* h; C- k3 m% S
解题思路, L1 u( X9 G9 m* O/ K
贪心,先种开花时间长的即可。
: D' u- ~6 |! J# h7 Q0 c! t
5 x: }0 Z0 F& V1 R1 B代码展示
1 @3 k+ v% g9 }2 P3 J! ?4 p, {
4 b8 k) F+ d6 O& _class Solution {
; i# ^) ]# g7 @5 N( Y, j static class Flower implements Comparable<Flower> {; R r* K5 j3 V* S1 Q) _5 u# ^
public Flower(int plant, int grow) {7 D& u' y# W. m- b: r5 I
this.plant = plant;9 v1 e. ?4 [; U# D
this.grow = grow;
. L/ K* O, D, W* H& b# g. m1 h6 y, n this.tot = grow + plant;
1 Z6 ^8 C4 g0 z+ f( n, F }
1 v) A) N' b' R+ Q% y! C% a( n, q2 W/ f
int plant;
! k& E& ^& A/ \; E/ _: k) W int grow;2 B' O6 M8 q$ W; g) L4 ]3 T- b2 Z
int tot;
. C6 N( p' Y9 F
! E0 n/ g! R6 D @Override
7 W9 J1 y+ C6 Y) k public int compareTo(Flower o) {7 g+ ]. }9 G7 D9 r
return o.grow - grow;
8 n! N% x$ ]- e1 R2 q }* t& U g: u& M- T+ t, b+ t
}
; \: q, C/ x( [4 R, n" u
2 z8 a, y, v" a2 o- v& B8 v public int earliestFullBloom(int[] plantTime, int[] growTime) {
( j8 c1 s# b" n- N" c0 @% Q' S Flower[] flowers = new Flower[plantTime.length];/ H; x* o8 ?- O W
for (int i = 0; i < plantTime.length; i++) {; O% E! C& t l- s' h
flowers[i] = new Flower(plantTime[i], growTime[i]);
% A* p- A% o* j# T& m) o }
\6 v" A# r. q/ C Arrays.sort(flowers);* F5 w5 J! q4 P) S2 W5 A
int result = 0;! e, u1 Y+ _- m# h) B! d0 {; `% a
int current = 0;: o! m* g2 J% R6 p4 D
for (var f : flowers) {# O; C6 G# k4 s9 Z* x A# \
result = Math.max(result, f.tot + current);
0 R, u2 l$ x# r1 B2 N9 ]5 r current += f.plant;0 i) R' K+ j" ]
}
2 m( l1 y5 B# q. v+ K w; N return result;
& X8 H* Q4 @( h9 X% s; x U }
7 [ y% l0 ~) ^/ x$ J K' u, G}
8 z. y" T1 @+ |* ~; t0 g, M& h6 H$ k |