登录后可回复主题
您需要 登录 才可以下载或查看,没有帐号?注册账号
x
【 NO.1 买票需要的时间】! Z+ i8 a5 I2 z( K# i
2 R, W3 ~9 B. _解题思路
' i4 m+ }; U1 v$ V签到题,使用一个 LinkedList 模拟即可。0 Z" E) O/ L6 J# b* W
/ k7 m3 E' f( T+ {) |
代码展示2 @# x* a8 O5 v, P0 v: m
7 ]7 R4 @. k4 ]. B( i
class Solution {- \; v* r* @9 |$ H2 j
public int timeRequiredToBuy(int[] tickets, int k) {
% @* F; g L# i0 Y LinkedList<Integer> queue = new LinkedList<>();
3 ~1 d$ n- B( W# ^4 P* m6 p for (int i : tickets) {' e, E; W& y3 E3 L" N+ K+ I
queue.add(i);# b! B# ?2 n4 R' X- a
}
7 ]! i1 L" n* q% J int result = 0;
. Z- G' q2 J5 r/ F" C3 ?0 s while (!queue.isEmpty()) {
. l5 _, t( X1 x result++;
7 u& x% _! t- h+ W int t = queue.pollFirst() - 1;6 z* [) `) A6 L" D
if (t == 0 && k == 0) {
+ n* N7 s; |" F" \3 C break;0 f! R( m6 ~. [; f0 r
}. u* G2 o6 F3 e- d% u5 e
if (t > 0) {, |; m7 ~/ J7 Q
queue.addLast(t);: }. r# z! b1 r
}
2 a8 P9 |% ^* p k = (k - 1 + queue.size()) % queue.size();! g9 \9 V7 }4 o. Q% G
}, t/ N/ d/ U, S ^9 O! x- G1 n c
return result;- b/ }7 c1 z- @8 I( v" q
}3 V) d, E- H7 k& m0 H
}
! e2 i1 n/ `" p. U! I7 H; N7 _. b9 }" \9 H1 U2 e c) X
【 NO.2 反转偶数长度组的节点】
( _/ P) S4 p4 _* m
0 E1 ~* @; e2 J' m4 P3 c; N解题思路6 j# ]5 [- m0 A$ |
数组更有利于反转操作,可以先将链表转换成数组,操作完后再转换成链表。
9 M+ B" d, `4 W4 U2 U# f" ]
2 {( B; n$ F: Y- `* L# U7 n7 w3 }$ U! E代码展示2 i5 R( [9 F) e$ f# o; M |
& v' K o* C1 P; r/ \8 W! X
class Solution {
) f F. K( S& J7 g" r* a public ListNode reverseEvenLengthGroups(ListNode head) {
0 c- k8 ^7 b2 z4 ?5 B3 k5 Z List<Integer> list = new ArrayList<>();
+ M2 }( _+ u; z g" b, I: T for (ListNode i = head; i != null; i = i.next) {
% w9 J6 J1 k& _7 p6 T) l) ^: f" k list.add(i.val);
# u0 O* f8 m/ M) F# v, E }
* g9 A0 d) T6 H, T for (int start = 1, len = 2; start < list.size(); ) {
7 C7 j$ r3 `8 i" R& W( y1 Z, z3 o- Z int end = Math.min(list.size(), start + len);
1 @( o: C: m+ E, S8 ^ if ((end - start) % 2 == 0) {
6 w; }, q- V/ \% T, } // reverse [start, end)
2 j3 u0 I: \4 I, I1 s, e! @; N for (int l = start, r = end - 1; l < r; ) {+ y0 N; n" [- Q' h- y
int t = list.get(l);
8 Y6 {# V+ A# \$ ~5 H list.set(l, list.get(r));/ I. w! `9 l3 f# C$ l
list.set(r, t);
! c, {. e9 d4 R; ?# v* a& ~ l++;) O# c9 v8 s6 a; G5 N
r--;
# I$ q; V' [0 J, S: | }
" E1 w4 V1 R7 ]* ~8 L# j }+ K/ o2 _. q& F+ k0 l
start += len;4 X, f& {* ?' x& X- }8 n" A
len++;
% Z4 H' I( N. s# t8 q }9 P) Q7 v7 q3 _ L
ListNode h = new ListNode(list.get(0));
E5 A" u% l/ D ListNode cur = h;5 j5 [: E. s/ O: q& I
for (int i = 1; i < list.size(); i++) {5 O3 X+ ^8 _0 A
cur.next = new ListNode(list.get(i));
3 C* b" w5 \. s4 b: l cur = cur.next;
2 y0 M: w; n7 ?& L8 t* Z8 O; Y- j7 U }
5 c* D0 `; K; b% c. ]# d$ _ return h;. D3 V! F9 [9 |; t& t) t
}& N) H' A; Q, S3 M) Y% b
}
, Y4 P) T6 d! t6 p* @# R- G7 L' k8 P- \1 [; U: J$ l/ t
【 NO.3 解码斜向换位密码】
# I1 J! W: r' f& o! `! o2 b; L5 R' p7 u. L
解题思路
& K0 o! o- y: f# k, l6 {还原出矩阵即可,最后注意将后缀空格删去。% b# [7 }( t( z/ X0 {) s0 W6 k
$ [- e0 ] W, d6 B5 I
代码展示5 ?2 ~ b/ x4 Z& d% }- Z
6 a, z5 I3 @5 n( M$ o9 E
class Solution {) R2 d' R$ L+ P7 @# [8 n% h) B
public String decodeCiphertext(String encodedText, int rows) {& L2 U7 J) J& c2 Z3 E2 B. [
int cols = encodedText.length() / rows; N4 ~/ O7 a* ~
char[][] mtx = new char[rows][cols];
' `3 Y& x2 I$ P, E; o for (int i = 0; i < encodedText.length(); i++) {
2 u0 H U# ~$ D( x+ I8 V# R0 c0 o int x = i / cols;
n5 c. F! n4 h7 F int y = i % cols;/ n; O5 D3 Z, o1 p
mtx[x][y] = encodedText.charAt(i);
# g4 p I% B# C }4 Y7 v. T! J- P2 P
StringBuilder sb = new StringBuilder();
# ^4 l$ i0 ^4 G2 m k3 M: l for (int startY = 0; startY < cols; startY++) {
, ] r: e5 h7 V6 J, Y( ^ for (int x = 0, y = startY; x < rows && y < cols; x++, y++) {. f& K4 D0 g0 M# Q
sb.append(mtx[x][y]);
7 |$ _* y3 \+ h( F, j L }
3 M6 O. }) i& o( c z7 t }$ B+ G% H- c8 H0 t
while (sb.length() > 0 && sb.charAt(sb.length() - 1) == ' ') {
0 f' q- ?7 S" y. {$ I sb.deleteCharAt(sb.length() - 1);. Y1 b5 }6 }. I1 ^" L% Y
} L7 s- j8 U+ H
return sb.toString();
1 n2 k/ n: A: l6 P( L8 K* p" d7 F/ `5 f }
, p# I1 V7 i- V& r: l0 y}
5 D" u9 X5 X' c$ O' x! p- M% P3 n9 d3 X0 B
( a5 x6 _1 o% z3 C4 D【 NO.4 处理含限制条件的好友请求】- u! Q" ]- n% z, _% v
$ h0 b/ y# m5 g. Q: G7 W# Y( F
解题思路; b9 B4 }; x: K- L! H' |" S& H
并查集,详见代码注释。
3 v. O2 {1 X& v! \# j/ E
9 A+ z* s- e y' I" c! J代码展示
5 k- Q6 j6 P( s0 ^' T9 `2 S. o6 W8 o6 B
class Solution {# r ^8 r' U: b* j8 R2 J" Z
public boolean[] friendRequests(int n, int[][] restrictions, int[][] requests) {: S) l2 ~. E: H; f. f" U+ p% @: T$ f
boolean[] result = new boolean[requests.length];4 O# ]" X/ D4 F* g4 U; w. Y0 X
// R[i] 表示不能与 i 做朋友的人 b9 t# S2 m! a% k; c* Y
List<Set<Integer>> R = new ArrayList<>();
! C% Y& Q% O1 f4 @+ ]6 f for (int i = 0; i < n; i++) {
& E+ R! J6 Q. v1 ~* z R.add(new HashSet<>());' ~1 c p0 @ v) j& B
}+ q H t& y4 I4 _! j8 O. z
for (var r : restrictions) {
: o# F" {3 a, {' R- C1 L7 b$ V R.get(r[0]).add(r[1]);0 r) K' D, [. r. Z1 R
R.get(r[1]).add(r[0]);
8 M3 w$ z( K W) |4 _7 z1 D1 A }
! j- u5 L! r& b& F9 `( p$ A% V // uf 维护间接朋友关系; `1 @) g- T$ ]0 V( v
UnionFind uf = new UnionFind(n);1 ~( F' G* b# L2 v
for (int i = 0; i < requests.length; i++) {9 l$ u1 ~! }( D
var r = requests[i];
- p2 F, M+ S5 i2 V: k int f0 = uf.find(r[0]);
+ V3 f8 L) O( @- o! B int f1 = uf.find(r[1]);
- Y4 \( P2 _9 q, a- n9 T0 ` if (f0 == f1) { // 说明 r[0] 和 r[1] 已经是直接或间接朋友了
( s, N. D; f+ D7 S% I! V result[i] = true;
, ?+ f) O* B6 \7 l' J9 Z% y$ T continue;0 y4 y8 p: O* m. t' \: C% ]
}
- c/ d' ~7 Y( A8 \- _ // 由于我们总是将限制关系合并到根,所以只需判断根节点即可3 {: i N1 p# n% ~, T6 S
result[i] = !R.get(f0).contains(f1) && !R.get(f1).contains(f0);: o: j8 G# w) a& y9 G1 `
if (result[i]) {
4 V7 d3 H0 A# m m5 u2 Y8 E2 j2 ^ uf.merge(f0, f1); // merge 后 f1 将成为 根
% m5 p7 L% O" P8 Z; j5 d1 b3 a // 原本不能与 f0 做朋友的人也不能与 f1 做朋友了
* D7 g& _5 X/ W3 f R.get(f1).addAll(R.get(f0)); _) M) d Z5 `- h: z
for (int j : R.get(f1)) {5 ~1 X) B9 d1 w1 S6 ?1 Y5 `
R.get(j).add(f1);* V7 @2 W) u# y' T
}
4 j( D2 k' ]$ B }
! y5 c1 d0 H5 J$ t }! I/ S+ O" Q" g2 w
return result;
& d4 f. p) }3 k7 M }9 `: m4 R' M7 y( I% k7 e( ~+ U
}
. X5 M' q3 J9 f1 A
7 A* ]/ g5 ? {6 C& [" P6 t8 }: G, ~class UnionFind {
$ I8 h( x5 j, j. ?9 u& r2 G public UnionFind(int size) {! |" i% G6 ^. m( P
f = new int[size];, Y5 k* e9 ?6 M
Arrays.fill(f, -1);
% Q3 w, O- M, c% E0 ]1 F$ o }
' K4 _8 b* Z# L/ C5 y4 i8 b* m5 m- v: \6 t! h6 {7 x7 E$ G0 V
public int find(int x) {
+ s9 z4 h3 _0 E6 W" ] if (f[x] < 0); S1 R& |+ N9 q( u+ t
return x;
; e5 ]0 ?6 o5 U* W6 j; w return f[x] = find(f[x]);1 [5 X. l* e' o2 z1 t! T3 o( C: C
}8 g- @+ e( V& C7 M7 E
9 Q' M# [$ ]& Z8 q public boolean merge(int a, int b) {
" U* z+ Z6 q; | int fa = find(a);
2 {( P- [. k z' Q3 r int fb = find(b);" T" k0 O7 a) p! N
if (fa == fb)7 H4 k& _7 l, d" U! ]0 A' K$ n
return false;
1 E4 x2 B4 U& i, l$ i+ Z. j. q f[fa] = fb;
M1 y! I, f; c% X7 d, r5 m return true;
# q1 O9 U/ G% Q7 N- D }: V- E# L3 ~, _7 o1 Y: Q9 r
. t' Q! g6 w0 X3 }, R# @! I public Map<Integer, List<Integer>> sets() {
/ K' Q+ |/ _" G! {# m5 k Map<Integer, List<Integer>> res = new HashMap<>();( L8 \ n- h: J1 [5 ^6 [
for (int i = 0; i < f.length; i++) {
( ?& O8 i. j) l4 Y: T6 c5 G int fi = find(i);6 a0 d$ ^% O' W5 P# A$ g4 Q
if (!res.containsKey(fi)) {
2 Q7 i: N& t4 p; x1 c res.put(fi, new ArrayList<>());* T0 ?$ P( i9 G
}
. k/ \6 {) z: @4 p res.get(fi).add(i);
" Z& H( ] I3 C* \+ X& Q }" F( a ~( i+ ]' |0 \- g* x
return res;1 Y; e: W# F$ t- y' M$ k
}
* d! @1 l3 n v2 U9 W5 h, X( o. m/ v: V! ~) T: {3 ^
private int[] f;
) M3 x6 o2 q! L b}8 o _; I, V; v2 p: f( z. `' k) S
|