登录后可回复主题
您需要 登录 才可以下载或查看,没有帐号?注册账号
x
【 NO.1 对奇偶下标分别排序】
# [" x; H2 r9 c" S- {( l+ S4 ^" g' x% I/ u! S$ {
解题思路6 n# N, y5 c# H$ o6 ?7 q- W ~0 m
拆分、排序、合并即可。
7 `8 t' W, P) X6 ^! [3 k4 X& D; [# u" E& E9 }# j$ K; z2 c/ }/ d
代码展示4 x) B! [5 F5 v8 i F! {3 I
) g5 D: R* w2 ^' l, |" s
class Solution {
2 m$ K! W- y: u+ _5 m6 u public int[] sortEvenOdd(int[] nums) {2 T; A) w* p5 ~ O
List<Integer> odd = new ArrayList<>();9 Q' z# u5 N$ X5 @& j
List<Integer> even = new ArrayList<>();& M N% X: w4 S# L$ t; _
for (int i = 0; i < nums.length; i++) {8 v7 N8 q6 T- ?, f
if (i % 2 == 0) {
- a! w* j0 c- g% x' }( x) F4 D$ n even.add(nums[i]);! E) ~& z- U9 e& w3 Y3 o Z9 \5 {
} else {& d9 }4 u2 Z3 t
odd.add(nums[i]);
5 ]; C% \3 o7 K" D& v, I b }
3 p( f5 [! J6 `# A }
1 R% y: f( A$ z( J: q Collections.sort(even);
4 r$ R; k- N3 U3 i i Collections.sort(odd, (a, b) -> (b - a));
8 d6 x8 [4 c5 R List<Integer> res = new ArrayList<>();
: M6 R; W/ }% C( W* g: E0 ~ while (!odd.isEmpty() && !even.isEmpty()) {; k8 u# [! u: f/ Z8 F1 Y
res.add(even.get(0));- N8 p# j" Z' F F
res.add(odd.get(0));
$ i/ A) y) c9 @' a1 f even.remove(0);6 G7 ]- E1 R! L; {/ [9 L" Y& ^
odd.remove(0);/ a$ G9 }% j" n) x* ?- Y
}
^* j4 P9 K6 M7 \3 D0 n% j odd.forEach(res::add);& @7 K6 {5 t7 h% |! E5 _1 h1 a
even.forEach(res::add);
: B$ B7 `. T$ d0 B+ t% G- i return res.stream().mapToInt(i -> i).toArray();
* J# |" U0 a" w3 a9 j4 ~ }
/ G/ E% u5 X& K}
" ^, U6 S3 F+ W" g$ Z5 E' K+ o3 K9 ^, V
" l" F% B9 b4 z2 ~
【 NO.2 重排数字的最小值】
( V1 K6 G# ^! i1 w$ u" ?- @" R9 L" N
解题思路- G4 P% }, W/ C7 O! r1 v( O4 ?" J
正负数分开处理,负数相当于取绝对值的最大值。+ r% F4 Q8 f, ]% E
/ |% M2 @1 F0 b. _求最大值只需要按照数字排序即可,求最小值则先取出一位最小的非零数字作为首尾,然后按照数字排序即可。
- }% U8 v" I/ f0 ^9 f' p
8 Y: S4 R" J1 N2 N! u- @$ Q代码展示 H5 Q/ O0 }5 c; K" f% z' ^
* Q" X6 T: X1 H. s; `class Solution {" H) P0 w2 H+ y7 X J" _
public long smallestNumber(long num) { C6 ^1 T1 o* q7 t
if (num < 0) {7 I! m+ ?, |. H) O: M3 h
return -max(-num);
2 V7 c( T G3 k0 V: v3 P; ?7 R$ j# j+ o }
/ u8 f& d4 E1 ~2 W return min(num);" {7 l$ @. d! v* u! T
}
% ~4 C" e' z0 g) ?( I9 J5 k0 f d& u7 ~* t) s% {4 [6 L- e
private int[] cnt(long num) {
1 F* S2 J8 t2 k+ |0 h2 W int[] res = new int[10];/ `- b4 B7 {. ]. R
while (num > 0) {* l1 ]2 G3 o! e' q
res[(int) (num % 10)]++;
z; }& M- @( a# P' b' A num /= 10;
9 } [, j. q t& I9 P; C( Y7 q9 a& q }+ U) R1 h" U1 {& A- M5 d7 a: C
return res;" v+ i$ T0 c& V# N$ I
}
5 f: n- b9 V8 h: H5 {6 P. [
9 J, {* b2 d# s9 J( j& |+ L private long max(long num) {! R5 ?$ l2 h6 F
int[] d = cnt(num);5 P h+ F) I$ @" O- G4 |
long res = 0;
: p! C1 ^4 G& u5 f+ y$ K for (int i = 9; i >= 0; i--) {# u( w- G+ B! Z/ t, f P, M C
for (int j = 0; j < d[i]; j++) {
: j; q0 J* }( Y- r res = res * 10 + i;
: ^ c4 @% h/ P" \ C1 c) P }
" o6 s. @! q5 D5 M }2 D) ]" N3 B( |6 c
return res;, k) ?$ G. Q& x- ^
}
& a/ Z8 ]1 x0 f: f+ q' G9 @5 N' j4 ~7 [/ m- I1 f1 h3 n4 V9 d% N
private long min(long num) {
" Q c, V: N N: K5 S6 X" B2 n! ` int[] d = cnt(num);3 l6 F& A \3 h: p1 `5 }
long res = 0;
4 a' ]9 H( w7 O2 a/ X+ ?, \ for (int i = 1; i < 10; i++) {
6 y6 ^4 k8 W3 f8 W% e if (d[i] > 0) {! f, a2 M1 \! D2 f: _, d1 V
res = i;$ \1 D6 ?/ K/ W" p7 O. V3 L
d[i]--;
& ]' [* A5 r& |- q" ~3 ` break;
% i$ `$ t% k+ v }
3 j5 U+ x: ^4 x4 e5 Y) {: j6 k) V }2 g' B% g% B) N& S- X$ f
for (int i = 0; i <= 9; i++) {& M$ K$ j0 x0 d N7 y6 q
for (int j = 0; j < d[i]; j++) {$ t& V. ]1 M& c" o3 V3 q1 P
res = res * 10 + i;
+ d+ }- p D& v }: |5 i) k) P+ x
}
. b& c% p3 ~7 ?2 t: Q9 U return res;; r6 E- ?4 a0 r& z6 [- ?/ g0 H7 D
}
1 X m& ~- N, X0 C& i/ M}
* A3 M1 Y) }8 A* ]3 F1 E: ^4 ~ V. ~1 ?) e8 V# B# }
0 N0 U' t& S: P
【 NO.3 设计位集】 l+ @) u) ]; G( l) W3 J: X
3 z- e) m+ I* b/ m% I解题思路
7 k- r1 j0 \$ e- [6 e* g使用一个 rev 标志位表示当前是否反转过即可。
" s$ T! g9 ^) J+ M: b/ K/ k w- D9 K& U0 y/ V) f6 h- l
代码展示# D6 M0 D! ?/ `
5 ?6 i+ @% \. m5 n: f! j- P7 W
class Bitset {+ i1 k9 u1 y: U3 j
; x# O, K3 U0 z8 n s1 H+ c boolean[] bits;
; i" _8 l; t; t- [9 \* Y# W& i boolean rev;
0 G: ^5 N$ _- F4 W int cnt; // count of 1
- g6 S% c; e. O) b2 }; Z6 ^* ?+ y$ `+ T& o4 W, W6 M) K) Y
public Bitset(int size) {9 {4 Q2 R7 i8 M" o
bits = new boolean[size];
4 g$ X# H2 R0 U9 o( t9 l rev = false;
3 A/ f0 G. t, s cnt = 0;! I2 b1 J& S0 n( b8 p2 a3 z
}1 H Y3 c/ u: x9 E$ Z9 e" K
) O+ @2 D k$ z/ R+ Y* i5 t
public void fix(int idx) {' f- h# t* D7 i
if ((!rev && !bits[idx]) || (rev && bits[idx])) {/ y9 v% g; g6 i# E) d* ^
cnt++;
5 }. o9 D) e9 y" ?2 S bits[idx] = !rev;6 u/ ]/ ^/ R& V7 n! x; U
}* Z2 h# V% q! h7 H- l9 f
}. D% Z* |; q+ l$ \+ f- V" `0 k
3 t. x+ d+ b0 W+ a# N5 b, s" [ public void unfix(int idx) {7 O; t; u4 X' Y% Z, y- A
if ((!rev && bits[idx]) || (rev && !bits[idx])) {
7 m6 I& r# I2 Y+ d) Q6 t cnt--;: A3 E3 c& N( P3 [$ O; K
bits[idx] = rev;
6 A# t- V6 V2 a5 Y }
A c4 q2 j) c' F# [$ V }
' u/ \. F( y; J' C% Y. n
* }: G4 K' G! N2 @- A public void flip() {& _/ I& m# K8 f# h# B
rev = !rev;
0 h; {3 R) c' Y+ W cnt = bits.length - cnt;- F# ]1 V P" Q. i
}' |9 v- W/ h: [ u. t& W% s
! ~/ q0 Q4 Q& @6 U) [5 j l2 x$ v public boolean all() {! ?5 F3 L1 n2 H' ]. c T2 W$ H
return cnt == bits.length;$ J' [. ~' P- c3 F, W' y; x
}
& d! i; d: X1 d; @) s/ m8 C
* P* P- r6 ?1 s& N5 c$ I! x: ~- c7 y public boolean one() {2 H8 g2 z9 K' m" T" u2 N
return cnt > 0;7 E% q0 L% b2 u
}- \8 Z, k6 _2 i1 G+ Z
$ O% w2 i9 h4 S _: F; C
public int count() {
: J' l7 q! J: R$ e2 p6 n return cnt;
1 s, A6 E0 r) u w" B! f7 r$ ] }+ o+ {. l! V! P s. {, q
" I( q0 d x/ x2 W
public String toString() {
! x* O( \" i9 {1 D0 A7 `! v( w, R! S StringBuilder sb = new StringBuilder();
$ c j, }. i: U) G& B for (var b : bits) {8 T# S' J. Y7 T0 K& u7 M- J* n
if (b == rev) {( o; }5 e$ c9 n" q, t
sb.append('0');4 D# _8 ]3 i3 ]* T3 M) H" A
} else {* r0 L( q% X3 O R/ d
sb.append('1');
% a! }" H% v6 m* A }. ~; s/ M- U! }, U, D! y
}8 v/ ]+ d7 D. K' G
return sb.toString();
$ \5 T$ v: b4 B/ f5 \ }! R9 p! \% S0 n! [; |9 L6 z# h
}5 j4 I4 B4 c+ r1 s: g
4 \" ?9 O4 ^. D; L
8 V" ^- c+ G0 U( m( X* o/ }【 NO.4 移除所有载有违禁货物车厢所需的最少时间】
( K: [ [/ d8 h6 g; _% x
3 {+ c9 ~, b2 I$ |. x8 S/ s( R解题思路
8 y( c2 H" s+ }9 {动态规划,分别考虑前缀和后缀,详见注释。
# V/ `7 @2 u3 I% [$ j8 s4 N0 p4 l. T" R$ n8 \4 }' e
代码展示
% R$ Z+ Q2 O. u& @
( ]8 _% x% r" X# c) }8 \5 X3 ~class Solution {
: X3 n/ ~# ]/ ?. z8 F$ K public int minimumTime(String s) {
$ \1 A2 A2 n( n% K$ {: t; s int n = s.length();
& X* G. p0 L. r. H, H7 t6 P i# E/ T# i' v
// pre[i] 表示使得前 i 个车厢都不违禁所需的最短时间: H2 S! F- g% L0 H) N3 p; c1 \
int[] pre = new int[n + 1];
" e! H; A7 v" p& z8 [: [ for (int i = 1; i <= n; i++) {
1 p8 e8 G: P/ M- } // 若当前车厢不违禁,则 pre[i] = pre[i - 1]( L/ A: k" Z: S! G6 `
// 若当前车厢违禁,则有两种方案:1 I' V9 Y2 a' X4 r- d1 h0 }
// 1. 在 pre[i - 1] 的基础上使用操作三, V5 n. H9 a) G& `* m) L
// 2. 使用 i 次操作一
' c/ ?. s2 b" A pre[i] = pre[i - 1];
, g( ?6 i! s/ m/ P" {; k if (s.charAt(i - 1) == '1') {5 v" T+ [8 T. [0 `5 s5 C3 P
pre[i] = Math.min(pre[i - 1] + 2, i);
$ E; C f/ `3 y* }; q }
0 X* q( h& _4 [& Q* u. Y8 Z# | } q! m" w& H) a8 B3 o6 O3 ]) R
// suffix[i] 表示后 i 个车厢都不违禁所需的最短时间/ l4 Y* ]/ p- l& t; R/ C
int[] suffix = new int[n + 1];
" a2 R( b3 p/ m$ \" I# T5 b% Q( ] for (int i = 1; i <= n; i++) {
1 s" c7 h9 F/ J suffix[i] = suffix[i - 1];
6 g0 X! h) t+ g2 A# S if (s.charAt(n - i) == '1') {
6 u4 p$ s0 l) |; B suffix[i] = Math.min(suffix[i - 1] + 2, i);
& m+ }; j7 l+ p" k0 Y1 } }. w$ c2 w9 \4 ^! M* ?( A* |
}) ^0 L/ ], l9 [% _
* b9 I% P7 x' \. A% `
// 将 pre[i] 和 suffix[n - i] 加和就可以表示一种完整的方案
) t6 r; l8 }8 I3 s int res = n * 2;
4 M( [+ a* R$ p3 n9 K for (int i = 0; i <= n; i++) {+ s* i5 h. D2 N: w) f8 A
res = Math.min(res, pre[i] + suffix[n - i]);
9 H2 W( x" E6 T" r* s }
7 f7 {9 t; J! A. ~- n; h8 q return res;& G X9 O" C; z+ x9 G* f
}
0 j8 u6 V0 n+ a4 |! c} |