登录后可回复主题
您需要 登录 才可以下载或查看,没有帐号?注册账号
x
【 NO.1 找出两数组的不同】
" A: G9 }7 E" I# M& U4 ~1 G- j0 I8 H# c
解题思路6 P! x, u; s9 z( |( z, G
可以使用 sort + binary search 求差集。
7 ^% J; ]0 @4 b7 Y, u1 c3 C7 j$ \; p: Y% A- S
代码展示
7 R( ^0 Q; A) U1 i" {8 v5 v- J5 A& A ?/ L
class Solution {
" H+ ?0 ~4 \4 z public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {2 ]* R) j/ s. v# z) H
Arrays.sort(nums1);
7 C( ^7 u: W2 ? Arrays.sort(nums2);% Z- o0 ]' [/ ~6 ^) ]5 ^9 {) W0 b
List<Integer> res0 = Arrays.stream(nums1).6 q. m( r! E; ~: O* _+ P* {
filter(i -> Arrays.binarySearch(nums2, i) < 0).; }' ]1 E4 X% v, [7 E8 e8 _
distinct().boxed().collect(Collectors.toList());
" y9 H( D3 \' w& W, p! F List<Integer> res1 = Arrays.stream(nums2)./ a+ i; \6 d* J0 X6 [2 b8 n' y
filter(i -> Arrays.binarySearch(nums1, i) < 0).
8 o, w6 ]4 ]2 ~ distinct().boxed().collect(Collectors.toList());7 M8 v+ A# Z" P6 h- @. t" R
return List.of(res0, res1);# t! z1 i3 h" q0 R- M
}
* K/ ]" \6 ~. u/ F: c- d}+ B( |% n" V, l. c( y s& \+ u
+ f7 j* n% Q8 u' n m$ P9 c" z2 f
3 G g( ^( b4 O【 NO.2 美化数组的最少删除数】
, I& |9 a7 V- R* G C* K4 ]; ~- ^7 `; W. ~
解题思路- s3 b$ ^+ Z [( r! _
从左到右遍历即可。
% r0 s% h3 g7 a1 N% X0 F" D! Q" j# |3 B1 T$ u( }4 r
代码展示
. s/ v( Q/ j% Q P, K$ j
9 Q& T, u$ l8 a( r$ @class Solution {
1 J) g! i# ^1 g& G public int minDeletion(int[] nums) {
$ P) ]# Q! j% N/ T" K" f' A- K int res = 0;. V: F, A' K3 f9 E+ o7 Q0 q
for (int i = 0; i < nums.length; ) { // i 应当和 i + 1 不同
1 N$ D' g- v1 S9 a; \ if (i == nums.length - 1) { // i 是最后一个,应当删除3 I5 G' T" F+ r/ t
res++;
F* V) @: _( w break;
- q; z( u0 @+ F8 O0 n- M. Z2 C& a6 T4 _ }2 g& ?6 L5 f& a4 i* P* z
if (nums[i] == nums[i + 1]) { // i 和 i + 1 相同,应当删除,相当于 i++4 H4 m% S( {$ x5 t% n0 b& W
i++;
* V+ m0 S5 V3 w P$ D7 z1 h res++;
, S3 x* |# t: W# v2 g' _: D } else { // i 和 i + 1 不同,可以保留,i += 2 以判断下一组; w; w1 q& R1 e8 S/ r, Y/ e0 y
i += 2;
7 U" L! ?/ c0 W; G) w% N }
! ~4 u% |; {$ w6 k* Z2 t- W }5 w, r% c" e8 G
return res;
6 {8 z% e, U. Q* B0 J4 K% j r }
) J0 u/ [) E' ] e, s: I}# ^6 O! c) Y5 _$ j( R% @4 N/ {
+ |( L# o% a% v1 b
0 z, ~7 w/ J( V Z0 u9 v1 H【 NO.3 找到指定长度的回文数】
+ Y% K a! I9 j$ J1 Q% U1 N* c: J: k3 r
解题思路
! C) ]5 {) h3 n1 P: d举个例子:长度为 6 的第 k 大的回文数的前半边其实就是 100 + k - 1,由前半边复制出后半边即可。
+ g( g" t7 k: o5 ]; N' J' Y2 Y# o/ B3 F8 ^- a7 M
奇数长度的回文数在复制出后半边前去掉末尾数即可。
& }% e$ t' {% v( I
9 Y$ c" I: g+ i( H3 O4 m代码展示
2 _. v) g" T2 ~2 f1 a3 b. u, X7 s( X& l) B2 D1 R8 W
class Solution {. ~9 L9 T8 S/ E' C, }* Q
public long[] kthPalindrome(int[] queries, int intLength) {$ K8 [% m8 t* }" Q8 k
long[] res = new long[queries.length];3 i0 O: T2 \. U# j. g
for (int i = 0; i < queries.length; i++) {3 ]) v5 g' E, c/ {
res[i] = kthPalindrome(intLength, queries[i]);
: D# h( R' s5 ]( \ }
, L2 D" k) X% J% n- G$ M# o! A return res;
2 }+ y7 y! f. h* T }
. [/ @) [' F' \% n& f# N: w* t$ d9 M; d |
// 返回长度为 intLength 的第 k 小的回文数
( [5 N+ J6 @3 J4 g* A2 ?+ y private long kthPalindrome(int intLength, int k) {
: c2 r$ b* `) \; d1 e2 x // 处理偶数
" G/ }: K9 Y3 L2 S% X) l if (intLength % 2 == 0) {
y6 _# G8 V" J long half = pow10(intLength / 2 - 1) + k - 1;& U$ H) }# m$ |) D
if (length(half) * 2 > intLength) {
- S0 F6 m: b- y) z/ C return -1;
1 M4 D& z: I0 ]7 \; J8 ?3 Y }
+ o1 Q# I v/ b9 k long res = half;, v5 G- g: k$ N- b H# y$ p
while (half > 0) {
7 o. l8 N% y: A1 t' i res = res * 10 + (half % 10);
2 o& S6 c1 v4 Y3 j3 W. U, I2 }' } s half /= 10;6 G! [# X! c7 ~" m! I! y
}
6 R4 Z# { Y% b( g0 T { return res;( D5 n) h7 x' V+ O% @, I( M: T- Y5 c
}
" {8 D3 u& H, o9 }6 a8 Z% g7 x0 B! K. X+ `+ R
if (intLength == 1) {% c, U Q9 Q! }. s
return k > 9 ? -1 : k;
! j. X: X+ \( N7 S } T2 r" ^8 c/ F# R7 a" m
6 I0 {, F6 u# e; }0 x" t' R! C2 W // 处理奇数
6 S9 m7 J* Y1 f/ o9 T2 @. z: } long half = pow10(intLength / 2) + k - 1;: x3 J' }1 M9 a7 A' l2 h V( {
if (length(half) * 2 - 1 > intLength) {
; E9 Z* D, r. v3 U; \ Q return -1;
( N+ z: v4 c2 u% x* E& ~: l }
* }- B- E- m# L. `7 r; I4 Q" z long res = half;
' g6 X! \' l$ ?. v: P5 u, F3 F half /= 10; // 去掉末尾数9 {( P+ ?$ Q! L* x! P* s5 t! v- A+ g
while (half > 0) {
6 m0 m: U" X3 g6 F res = res * 10 + (half % 10);
6 s! C& B# s6 N: p/ E7 a half /= 10;
$ I4 u0 U6 L6 } }
( x: I( w( A; c C return res;
) t2 o: X4 Q! B; @2 s! F" `) @( b( J }& p1 O. R& i, s) Y4 _5 c }
1 D: F) T( N2 _9 o
private int length(long n) {
9 E5 B- r/ U1 |, b9 \% m3 ? int res = 0;
0 ]) x4 ?8 v- Z9 W while (n > 0) {. C9 Y D8 x' P$ A. y3 _
n /= 10;5 ?6 |2 [% k7 H" q% j- E3 Q
res++;
- U4 a$ `2 b+ y4 j" k }
" a' {6 b6 k% a P. o; @/ |8 P return res;9 K9 W4 ^& ?: V+ [( d
}, e: h" U R9 l/ E5 ]! \% z
7 f$ u. B Z3 [- H6 u private long pow10(int n) {
3 Z+ e7 Y1 k. i. F) [: }4 w1 v long res = 1;
. \/ P, f5 ?1 j, s* A for (int i = 0; i < n; i++) {$ J' a) ]7 P2 M% d- X
res *= 10;
, h1 Q1 N& Z- A7 H' B. q }. W8 Q m' b- v. F1 ?4 x
return res;
2 ?! o% R3 v- _8 B: w* B3 W. ~* Z }" T: S2 v2 G4 r
}* n$ I, R/ v( u5 g" n3 K0 i1 x
5 y! z `% L7 t$ G1 I8 ~
4 I0 f% |8 W, f9 j
【 NO.4 从栈中取出 K 个硬币的最大面值和】
; C# r |$ H" u e# s. Q" k* ~9 n, ?* f- \ }
解题思路2 }" |; Z' l- {6 y6 T* v ]
典型的动态规划问题。
+ V* K. C/ Y z. E6 j1 o% R1 q0 r$ H# u6 t5 K& a
定义状态:dp[i][j] 表示前 i 个栈取 j 次得到的最大和。
7 i" D) G6 k" B& B, Z2 d% L
: c& o1 H# q# L, w3 E2 Y) l7 ^状态转移:在第 i 个栈取几个,即 dp[i][j] = max{dp[i-1][j-t] + sum(i, t)},其中 sum(i, t) 表示在第 i 个栈取 t 个硬币得到的和。
; _4 h. J7 x* k/ V
9 g5 w0 @4 a: D, P2 L- ]: s, \代码展示7 T2 d* r% z/ O! k9 s ]
0 N9 W( [3 R6 U7 i$ }
class Solution {
8 K5 ^1 I/ H* S/ [7 U public int maxValueOfCoins(List<List<Integer>> piles, int k) {$ z& m! h7 K! ?' x r. p
int[][] dp = new int[1001][2001];
- ]9 @ G I$ O for (int j = 1; j <= k; j++) {
+ e- B' X5 t1 y6 g for (int i = 1; i <= piles.size(); i++) {
% y: d- t3 X1 _' P3 q h. Y var p = piles.get(i - 1);1 t* X- i4 s) M: `* r
int sum = 0;
2 E/ \! }! b% `8 T) g8 c for (int t = 0; t <= j && t <= p.size(); t++) { // 枚举在 i 取 t 个" B/ b+ p2 n9 \: r
dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - t] + sum);
% c# ^7 F0 U% M6 Y! q0 {% A8 _ if (t < p.size()) {9 |4 }! e) w" j
sum += p.get(t);
- R! ~- p# m3 p1 |4 B* O/ G }
) v' u* k, |8 w# j. a4 L0 n* o0 H# M. w& q/ I
}
* v4 v0 ^0 p ^6 x! e- s! n }
6 \* x$ i/ R% E) u- P. h }4 n. K8 m6 e! ]5 L
return dp[piles.size()][k];& d' I( g9 e. J$ t9 i
}* h+ t, M/ s) ~6 U
}
' L" F, f7 e4 F3 H6 l |