登录后可回复主题
您需要 登录 才可以下载或查看,没有帐号?注册账号
x
【 NO.1 检查是否每一行每一列都包含全部整数】
# q2 {3 b7 }, J `5 c; O& q
: k8 N! x9 I: v1 K4 j+ C解题思路! A9 J9 H5 Z3 T! O+ Y8 m
签到题,可以用排序做,也可以用 Set 做。: k, Q& e1 ]* x9 H7 _7 s
+ ~9 |7 l% Z+ D! i) s6 _( u
代码展示; }$ `( J: d: X/ K% c4 a& j
; P8 Z! Y- _& k6 K0 W
class Solution {
9 U) ~5 B& h* c7 F' J, p( ` public boolean checkValid(int[][] matrix) {/ K) P! c+ b( I. k- o
for (int[] mat : matrix) {7 K1 S1 H% M( J( l! c5 N! o7 |
int[] m = Arrays.copyOf(mat, mat.length);
2 ^9 B: t8 O) ^# H- C2 Y/ W2 n Arrays.sort(m);# B: V) C; ~0 C% S$ D5 a! s; l
for (int i = 0; i < m.length; i++) { O6 j' m+ @! V: B
if (m[i] != i + 1) {
4 r* t& z6 C+ T. H7 F" u return false;6 B B m' }! H5 L1 K3 F9 _
}
! a6 n5 \5 p2 p5 t1 t }
" d5 y5 c) `% J }/ M5 e a) E5 }6 c2 I: q- o8 R/ o1 l
for (int i = 0; i < matrix.length; i++) {
7 M, J. M0 t8 e: s int[] m = new int[matrix.length];
) `5 \7 x3 z7 W; I+ B* F( h" @/ ]8 N for (int j = 0; j < matrix.length; j++) {, \. `3 m( b" v
m[j] = matrix[j][i];
' V- J4 s m' }3 R }7 Q8 y& W& t2 L! K/ O0 N- X& G
Arrays.sort(m);
& G; Q$ z/ z/ y# N" K for (int j = 0; j < m.length; j++) {# U( z& v5 ~! ^( ] a
if (m[j] != j + 1) {
% v4 q+ u Y2 U; O* Y$ G return false;9 k2 t! ^1 n6 V: L
}
5 w8 @& V, L: m' O/ D5 f5 Y1 \1 N }
4 S; e- u M u2 l2 ~! F: @ }* b5 n5 A! n; e% \# J& r/ H
return true;8 w( i( M7 M0 T) D
}, D4 {0 j/ D2 ^( I1 R& ^
}7 q( b. r5 M& ]
# f" M/ @/ F) b: E
+ `4 a2 |6 K1 X& n$ ]- v# M【 NO.2 最少交换次数来组合所有的 1 II】
% v3 X+ {5 V. L: m8 U" J
2 F s2 E3 S! P" i解题思路
( y* Q, Q# l! ~& q& w I; m- d. T首先化环为链(将 nums 复制一次拼接到尾部)。: W5 R+ w" O4 d! S. _: Q! C$ e
) x3 q9 ?; U, M/ \5 i然后枚举最终结果的位置 —— 连续 tot 个 1 分布于 [i, i + tot) 下标时,需要转换的次数就是这段下标中 0 的数量。8 _% L1 I) p$ N( y
4 z6 M, V J6 ^/ F: J, A! h* X
每一段下标中 0 的数量可以用前缀和求出来。
/ F. T4 t' ?: a% O4 ]% E: d
, V, E/ a8 y. K' i% C代码展示2 q: Y" c! R! J- S7 U& f4 w; g
2 t d( o5 c1 I. \: s! y( F) b
class Solution {0 ?! Y) w4 ]1 A
public int minSwaps(int[] nums) {
) L- ~; @# x2 Q int[] link = new int[nums.length * 2];
& c8 m6 Y8 w+ D6 m! F- K! Q9 e for (int i = 0; i < nums.length; i++) {% J$ j" g1 f# T$ O
link[i] = link[i + nums.length] = nums[i];
/ y: b$ O, U' p" J8 ] }' E# y4 o* ?) H' J% b
int[] preSum = new int[link.length];
3 ~+ [+ b) d4 D- I; n preSum[0] = link[0];2 F" k- Q+ f2 j5 B# N. F1 D+ Q0 X
for (int i = 1; i < link.length; i++) {
; O% U- e. K2 X% Z( j, K) W preSum[i] = preSum[i - 1] + link[i];
2 ~" q2 p$ }9 V G7 _ }
, J6 ?$ M7 T9 m# @5 g" c int tot = preSum[preSum.length - 1] / 2;
3 W) r: |0 ?8 t- F" E0 j8 y" ^ if (tot == nums.length) {
O# U$ e) i" A C return 0;
$ _1 Z$ L* H2 A( P }8 s3 Z# @0 Z3 o
int max = 0;8 J: }: V3 F9 Q4 t/ ^. g" Z' I
for (int i = tot + 1; i < preSum.length; i++) {
* y: D9 ~1 q% w' Q: j max = Math.max(max, preSum[i] - preSum[i - tot]);- {: n% T& ^! s e/ ^
}
3 ~1 [" O: x { f1 s" {* h0 G- `$ H return tot - max;/ l3 W6 p" J0 A- t" j2 v8 d$ g
}1 ~* C" ?# n p: X2 N. _
}
! l* m* ~# K# H& {; e! q4 W. ~% S* }4 h# R* E" M3 b1 ` q
2 \3 S. t" c& n, w* I) G% N8 y
【 NO.3 统计追加字母可以获得的单词数】' S) f" ^. A- P* d3 w& X
* A& Q! e& S0 `# A6 r( J6 E解题思路
6 A, a5 S6 Y+ o$ ]$ W+ w+ ^3 ?" u注意审题,只能追加一个字母。# C: j9 W1 Z( g, p9 Z, O7 H9 K
* Y7 I$ p& w& s) @) c7 u. m# S$ V
因为题目不要求顺序,所以我们可以使用一个 26 位的二进制数来表示一个单词。2 Y( s, h2 H7 S
% K0 v" u) c6 |/ j! D/ l% `' K. p; E代码展示2 `6 d l) x6 x' ]( U; G
" H; m+ N6 W% w1 |* R# I. T" R$ y, cclass Solution {- w* x& z; I) @5 J. f- [* K
public int wordCount(String[] startWords, String[] targetWords) {* w9 V$ e; E' C; S0 N! e
Set<Integer> startSet = new HashSet<>();7 i% p! q1 l" p1 d9 I
for (var s : startWords) {4 ~2 ~9 ~9 m" ^8 p) [/ V
startSet.add(toBinary(s));
2 ]7 M. Z# S* E: ]7 { }
6 a6 Q5 v9 x0 x( } K int result = 0;+ R& V8 P% a7 s# m- `% J8 G/ w
for (var t : targetWords) {
8 ]" I- f: d# d, I% { int target = toBinary(t);7 d( D. o7 o/ p
// 枚举追加的是哪个字母5 C; N% f% h4 u5 w) P% v
for (char c : t.toCharArray()) {& P% W: Z: E- A' T4 z9 Q
if (startSet.contains(target - (1 << (c - 'a')))) {
) R6 F% c$ j8 z4 t+ L result++;' [* s8 E; ]4 a) Q8 P( ?9 N; E
break;) a. }5 J3 s8 b' f% j, D7 ^
}8 @! L- z4 g' r `# e. B$ C% ?
}
3 O6 t {# d" {# z }1 A% p$ @/ L$ }. U+ ?. j
return result;
& j! b; L9 i. z, O; t9 m. A1 P }" Q9 z) A" s. x0 ^1 h: C$ p
' E( o1 q1 Z# U- L7 y
private int toBinary(String s) {
4 H$ t1 x/ v; n8 M, y% k int res = 0;4 R% Q( b: m" k) a! g ?
for (char c : s.toCharArray()) {9 f# _3 m% G& C8 a2 P$ Q4 t: T
res += 1 << (c - 'a');0 E E) K5 S% q: K: W' N0 U* P/ F
}/ k1 x( [% m" ?; e# D
return res;% X: c" b& o8 t- t4 O: ]
}3 r" T( G8 {: Q+ W3 K2 p( v! R
}
$ J' D# s* p9 C$ R# C! C( x- C3 ]& K; I9 _' L
6 ?" O m' Z& I
【 NO.4 全部开花的最早一天】
4 a5 L: v/ C6 O+ a; Z9 l- u
% L# e+ u) z; W3 ?9 S解题思路, z" j& I% M3 `5 I2 V
贪心,先种开花时间长的即可。
" v. u' S# ]$ t' q) R& G# Y2 r. r j& ]) g. p3 t- T/ U5 [
代码展示
% f& v; G- L( R2 w
* \* S$ O0 `" F o& f9 n Pclass Solution {
$ s- _2 O: T0 y1 d( @ static class Flower implements Comparable<Flower> {
1 A! a8 L0 }! E* t public Flower(int plant, int grow) {( D- X3 g7 M" p, d. `
this.plant = plant;5 [$ ~- v7 C, ]: K, K* w7 [
this.grow = grow;
# ]! W# [: |0 I6 x; h1 D this.tot = grow + plant;# T3 H1 H5 `2 v
}5 k8 j( Y: Z) l' z/ x* z4 F
/ t8 G+ w% L+ i0 M' x int plant;
' W- L" w/ }8 N5 l int grow;
/ O6 p9 D0 H/ {; r4 R" r) `) b int tot;
; ^: c0 ^ V, d ]# k: U8 ]+ ^5 N6 m% C0 |/ F+ g
@Override
3 o3 v4 ~. H. } public int compareTo(Flower o) {8 [4 ]" x1 u+ U0 ^6 O8 }: W' S9 m
return o.grow - grow;* D0 F% v1 r9 n8 i6 y
}' ^; p1 a5 T4 M5 |6 b9 L
}
+ H- q" `/ k9 M" G3 p% O+ ~ N$ y( L. g' W+ U
public int earliestFullBloom(int[] plantTime, int[] growTime) {
! E& ~& ?7 ?) F$ [ t/ N Flower[] flowers = new Flower[plantTime.length];2 O7 }, }- t! H" a
for (int i = 0; i < plantTime.length; i++) {
% r. Q" J+ T7 S g l3 _1 w flowers[i] = new Flower(plantTime[i], growTime[i]);
1 U7 D2 {2 j; G% u$ i }7 M% ?# Z& u) R4 q# a5 k
Arrays.sort(flowers); R. [6 F0 a; z" D$ P" Z
int result = 0;
# J! g; ]; H7 l2 l& [6 G) @4 w int current = 0;
2 a/ s: ~1 c& E) M5 ~+ Z9 ]0 Y" O for (var f : flowers) {
$ t% X6 J% E6 z7 i result = Math.max(result, f.tot + current);' m: Z9 Y e, o
current += f.plant;
& x; E5 Y$ H6 `% w5 W, w: | }0 I6 L, E' f q1 R; T t) |
return result;
v) [' O! W3 [, q; O }
! U3 m7 F+ D, ]* V8 I}$ K- ?: }' l$ x. I0 e
|