登录后可回复主题
您需要 登录 才可以下载或查看,没有帐号?注册账号
x
【 NO.1 买票需要的时间】$ q7 ?; ]+ U9 t" I% a7 e) _1 Q
* b: I I6 z" {解题思路
5 y* b3 }& B+ u签到题,使用一个 LinkedList 模拟即可。
$ V, }' I: i) X! y x7 D8 q0 X3 R" d$ y
代码展示
) ~- ?4 Q' W% w) z9 {; u: C7 @- o6 p3 [8 }, k
class Solution {3 S! a! Y0 b8 i2 j5 P9 H- \; A' m
public int timeRequiredToBuy(int[] tickets, int k) {8 o" j3 E0 h. ~& x
LinkedList<Integer> queue = new LinkedList<>();
. I8 l* d1 N+ _# d+ z0 A for (int i : tickets) {
$ F5 v) h/ v- V5 A& g queue.add(i);" |+ f# C& `) }! `- P. i
}( x( n& B3 }. n* z* N; y
int result = 0;
7 ]/ H3 Q; [* {, I' S9 ` while (!queue.isEmpty()) {( i4 i7 o" M& b
result++;
3 A$ D: A+ Z, P% A+ m- }; c int t = queue.pollFirst() - 1;
+ m# m) p6 l B. z+ o0 a1 Z if (t == 0 && k == 0) {& C, l4 ^' K- k& M
break; p, A5 }( I" _! h
}
- f: w! b9 I1 i r4 T# L if (t > 0) {( q& d Z3 J' l3 X' J! u
queue.addLast(t);/ k7 }, e9 |! N% j( n( o% Q
}
5 I. n; D9 h9 L( l. `# i Z k = (k - 1 + queue.size()) % queue.size(); A; {4 V6 v- c( }3 o
}! a1 @/ T& X" a
return result;: [2 u5 ^1 @+ |# @/ k, A
}) f; a: X7 ^6 ]5 U$ A' Y# h" S
}* H: _2 G9 t5 U- Y" x
* r! o# J+ ]1 s# V
【 NO.2 反转偶数长度组的节点】
! u* ]) {- g9 X7 c$ a6 z; @
1 f! e: L( y9 x- ?8 {7 w解题思路) b/ i4 r3 ]1 G7 Q' Z
数组更有利于反转操作,可以先将链表转换成数组,操作完后再转换成链表。+ g$ g+ {% Q% u [8 g3 S3 P
% o, _) i- i; J1 `' w! ]代码展示
0 g d4 U5 j7 W+ D3 Y
# P Y+ V- P+ w# Q" ]class Solution {6 [! u2 R$ u6 k4 ?1 ]2 Z
public ListNode reverseEvenLengthGroups(ListNode head) {* |0 b7 ^7 [" W3 F2 L# B: U9 X
List<Integer> list = new ArrayList<>();4 F3 U- k. _0 ~; [
for (ListNode i = head; i != null; i = i.next) {
5 T! z4 ^4 N4 T) ]: X9 y- [& N list.add(i.val);
; k. ~" e# T% N7 [- z }
# Z: ?. W8 ~& M$ c for (int start = 1, len = 2; start < list.size(); ) {
7 M8 h' A5 D9 C( A: v, }4 B int end = Math.min(list.size(), start + len);! C5 h2 m. g2 `
if ((end - start) % 2 == 0) {
9 U: u$ |/ \( ~' C // reverse [start, end)
( `7 [, P+ _# O U6 w5 G2 d for (int l = start, r = end - 1; l < r; ) {
! F+ M# c( g& ^# p5 k4 C: E( ~" ^ int t = list.get(l);& ~3 W: U# O# h% Q: ?" d5 E0 ]
list.set(l, list.get(r));
' E# g6 A! U# `) v list.set(r, t);1 X) N% t/ J$ W+ b6 B
l++;
I( I9 M2 _9 B( l+ [3 v r--;* a- U! S! c- E0 U& u$ G- v
}
C( x# d# V9 L b- U }. k# q/ r) e% W, g# Y7 \; K4 D
start += len; K- R8 ^0 ^6 J3 }( h& k5 C
len++;
& f1 u: z6 }9 W. m' A }
- B& Y+ o4 y; F3 H l- ^' i ListNode h = new ListNode(list.get(0));
4 X: x4 t+ [+ T ] ListNode cur = h;
. }, T/ b' d* k- O" c z( B for (int i = 1; i < list.size(); i++) {
4 r8 H1 C+ {0 e, C+ [: H5 q cur.next = new ListNode(list.get(i));9 m/ N) \* p' v" @7 {# J
cur = cur.next;7 G( g. {* t9 r5 i+ l
}
. J. a& X$ R, F4 `+ J4 E( u return h;! l- n- F# q/ V) Q% H Q0 ~
}
# L2 j% F7 x1 c1 @; p* Z& Z/ V} R3 `5 s& z1 b
) h$ y4 F* y: Y- ], O+ X1 x+ t
【 NO.3 解码斜向换位密码】
* S8 W! I& i5 E
3 w( m: h9 ]8 a( q# N" K( m1 u解题思路
6 F2 x, _( t) Q0 w/ j K还原出矩阵即可,最后注意将后缀空格删去。
% P; t2 H2 Q) ]' _, j% R3 ]+ F
- k* S7 [+ Y2 n2 S* x代码展示* R( T2 p6 v1 i u
, E) o' A4 j' m
class Solution {7 K$ {' I5 z$ B: P9 ~$ B/ [6 E: p
public String decodeCiphertext(String encodedText, int rows) {
: [% g9 V9 |7 |1 ?3 t+ T& I1 [ int cols = encodedText.length() / rows;
4 O- x. i- \! W) j# }! {7 ]( J8 f char[][] mtx = new char[rows][cols];
: s8 q3 R7 M) B+ e+ Q for (int i = 0; i < encodedText.length(); i++) {: S5 H' n- F2 R+ M, ?0 j3 ~# P
int x = i / cols;' H% P" r7 t7 G- H* `# Y- y
int y = i % cols;& p3 @' a/ p) P. n; ~# X$ j, V, [
mtx[x][y] = encodedText.charAt(i);
: a# y" K" h0 I* D1 H( i }
; B+ @ D( X0 t0 {) s. d+ l; b' b StringBuilder sb = new StringBuilder();9 f S4 z' x' H ]' @8 z: Y; s! |, M0 d
for (int startY = 0; startY < cols; startY++) {) m8 X! V" r \, Z; Q5 |" t
for (int x = 0, y = startY; x < rows && y < cols; x++, y++) {4 m3 g l/ w1 c( S z( j" r: [8 Z
sb.append(mtx[x][y]);! r2 Q* q9 C5 D
}; L7 j+ _5 @% {4 J6 j
}
+ z6 J3 m7 y W! f: D while (sb.length() > 0 && sb.charAt(sb.length() - 1) == ' ') {
3 z) \" ^! H# i8 D! U- j& D sb.deleteCharAt(sb.length() - 1);3 _3 V0 @. [7 P* f( m
}
! u* Z- C/ t2 d& z# h6 Q return sb.toString();
% S' d: Z' p6 g- Z* q+ R3 s }
0 i* k: C) s0 B3 [" K6 b0 P}3 r7 U# B! m# w# r" K6 C
* |! D: f+ U8 `, Z7 G
, X0 Q. y, `" u* v1 h【 NO.4 处理含限制条件的好友请求】
: n' p0 ~( h! O+ O0 a5 A A
" \% J! e( P* r2 d: S解题思路
' Z' `; y1 X# O) K# p并查集,详见代码注释。
9 l* ^& r5 k5 b
# d) R$ l) l% W8 R: f" @代码展示
; ^3 N8 o2 b" \7 x) \" v& M
+ M0 G. ?# E4 p5 @6 jclass Solution {
6 _7 P7 ~" Y* l3 S5 e public boolean[] friendRequests(int n, int[][] restrictions, int[][] requests) {& @7 q! X: `; Z" x% L
boolean[] result = new boolean[requests.length];
* K0 Z" `/ t* K; Q/ I" t% O- ]$ y // R[i] 表示不能与 i 做朋友的人
+ W; T% a8 X+ k# S" m9 `) } List<Set<Integer>> R = new ArrayList<>();
- B4 ~3 a! q/ a/ g( S for (int i = 0; i < n; i++) {
' k2 ]& o1 t8 E$ ? R.add(new HashSet<>());, }; K! k6 }( k* V3 \' P
}
. } x4 n- }3 I; I2 G for (var r : restrictions) {
; T2 J& f) e, q$ g- o3 L5 X R.get(r[0]).add(r[1]);
; E+ ]. @( `7 ]8 k6 a R.get(r[1]).add(r[0]);
6 E5 `+ }! A& k+ l }
+ Z7 c( I# ~; y' D7 S- J' B8 m // uf 维护间接朋友关系
6 I, W; A; w. N- X. t- T UnionFind uf = new UnionFind(n);
, u \4 `& }- D, w! P2 v for (int i = 0; i < requests.length; i++) {. g. s! a r) W. @
var r = requests[i];) e/ p5 L- T, d2 e( v
int f0 = uf.find(r[0]);
$ N0 ?; A. b* e; o int f1 = uf.find(r[1]);# g& [5 y* P; f# p5 Z( _: \
if (f0 == f1) { // 说明 r[0] 和 r[1] 已经是直接或间接朋友了% `6 n/ ^, d; o* c0 y' q. H
result[i] = true;- L3 Q j$ d8 x7 j4 a) p) t
continue;
4 C) d1 Q- ?& X" E" t }1 O/ K2 r) w p$ A8 Q& K' f; r" Y
// 由于我们总是将限制关系合并到根,所以只需判断根节点即可; }" i3 p' q( l$ r% [- K6 X
result[i] = !R.get(f0).contains(f1) && !R.get(f1).contains(f0);2 a+ i6 w5 S% B/ \
if (result[i]) {
- A# G3 b/ c8 b, v& z# H/ R uf.merge(f0, f1); // merge 后 f1 将成为 根
# J" G* y- W! o0 P4 D* G! k7 w) r/ ]8 g // 原本不能与 f0 做朋友的人也不能与 f1 做朋友了1 }5 H2 e) s" R5 F1 P7 t5 T6 O
R.get(f1).addAll(R.get(f0));
8 C3 A9 m; ?8 l! { for (int j : R.get(f1)) {
D# V6 X. D \: F& Y R.get(j).add(f1);$ V& k+ i$ l5 [" `; V' m) L7 C+ {+ D: A
}8 s2 A u5 p( t) C) g# ?
}
% r" m8 r; N; ?, U X) I/ `3 Y# R( ? }/ {8 D* N* M& T( f' K
return result;* M) s& C! x3 _$ N- O; X/ o
}. b* K6 W {6 ~/ i: K
}
! p5 y3 E; J" g: V, Q6 S5 W+ E1 @( \3 `2 o8 c5 b4 S& V
class UnionFind {+ \/ p. ~5 c5 F1 E+ p' p$ Q1 A7 g
public UnionFind(int size) {4 v# \6 W; `& @& L
f = new int[size];
& ~* u Q. y) M/ A( A) H( A$ l. c Arrays.fill(f, -1);, J. z9 v" ^/ V7 W7 u: l. C# `
}( a. \" U- _- \) _; U* A* M0 ^7 Z
9 O- |+ d8 \# e! h public int find(int x) { p# l, f2 Z, b3 f' W
if (f[x] < 0)
+ s3 O4 W- K/ `* q return x;7 v- f- j+ j2 c9 o$ b# |7 \
return f[x] = find(f[x]);
# E7 G% k. B- \ o. B }# j2 ]4 h" D% N: z2 l; q
0 f* { {5 n2 Z5 X3 ^
public boolean merge(int a, int b) {
$ {2 x9 a) `& y int fa = find(a);% n0 E' u+ l3 D
int fb = find(b);# ^8 W4 B8 R* O
if (fa == fb)
* T8 f) O: Y4 Z" B( A; K) D return false;+ H0 A3 i$ p S; R+ Q1 I' Z
f[fa] = fb; t7 @9 V2 z8 Z2 w: p
return true;
8 l7 E! F6 B& K }
2 D; a+ x! I- j Z& k0 s& \) p! w) @# S3 p% Q
public Map<Integer, List<Integer>> sets() {7 M" S# H$ A( f. x
Map<Integer, List<Integer>> res = new HashMap<>();
; H. r9 J ?# b6 @* ^4 y n s for (int i = 0; i < f.length; i++) {
( ~0 b m9 M5 {+ v7 ^6 l) U% c int fi = find(i);* ~% A. L0 l( F3 s9 f# R8 W+ j
if (!res.containsKey(fi)) {
9 @3 N2 _% @! ?" _4 u. j- l res.put(fi, new ArrayList<>());
9 l7 h' z& T- n; q: D2 V, |+ v% R }8 M7 h/ y7 H8 r; B: F& k
res.get(fi).add(i);
; m! |1 O, s3 N1 B }
; q) s* p# u( u5 o return res;' P3 }% P* R& @
}
; f5 e8 A. Y* ^/ o/ |: I, ]% }8 `1 y6 K9 \
private int[] f;" N6 z- i3 B$ }& v, H" `, Q5 p
}+ |- }9 t5 o; e( G7 L" {
|