登录后可回复主题
您需要 登录 才可以下载或查看,没有帐号?注册账号
x
【 NO.1 找出数组中的第一个回文字符串】
! v9 N5 @$ G0 P% i* c1 X. V5 S% f3 g. w% F/ M5 ?9 S& c
解题思路
$ D; c% M- Y6 N Z( L签到题,遍历一次即可。$ f1 B1 G' D" f) a+ k( ^7 F
% z! M* W9 c0 ~( F0 P代码展示
& ~4 N, }' W7 C" P1 U2 I5 V: u. x3 q! k/ Z3 n" t
class Solution {
. {) N& ~' H# b8 y- p public String firstPalindrome(String[] words) {; h. ^; `9 w) R5 W, S! W' O
for (var w : words) {
h9 N; R |: R) T) j. Z, S boolean found = true;9 v' l* u% W* U
for (int i = 0, j = w.length() - 1; i < j; i++, j--) {& t% [# D# S+ E4 ^( B$ o
if (w.charAt(i) != w.charAt(j)) {" q3 {) o, c* j: p0 r
found = false;% }$ I: F2 h7 V! H% ~! P% f4 i- Q
break;) r0 |; S4 F# w! y [
}- p2 g+ ]$ s4 `* A$ ~# M! ?
}* ?5 b0 q4 J* @& i' D& s
if (found) {' ~; j' x8 |! X
return w;4 i% X$ X5 l4 w: l3 [
}
. @% t9 u" Y, V5 \) f: Z! y' q }; c' C. J S/ t2 [3 R; u
return "";1 k! H* F7 i" p, t. N' C5 T' B2 X/ @
}
0 i) R+ Q2 I. \: v7 o}
5 |3 R& a* y& k/ S7 h
. u4 W. E" h5 L( g$ u
0 o( l, z- d1 ^6 p8 T8 O【 NO.2 向字符串添加空格】
) d( D- \2 U' J) W; Q( ]0 W$ }6 _4 v2 o l" t
解题思路
4 r) e% K( w6 Q$ h' V" h/ d1 g使用一个 StringBuilder 维护新的字符串。1 v( [. c% I0 C- ?. i3 m1 v
& Q$ _. @! Y* K- A% p ]
代码展示) Q% T$ I* a* X& a+ Z
; v9 Z+ Y: x2 p
class Solution {
. ]6 D# i3 d% X/ X; M4 j% P8 c public String addSpaces(String s, int[] spaces) {+ I0 R2 }( c3 Z; S( q
StringBuilder sb = new StringBuilder();
" `) v) Z" o3 _2 [6 J' D0 c5 B7 N- j int sp = 0;- T- Q) i; K3 h6 ~: z7 `
for (int i = 0; i < s.length(); i++) {
2 q( i5 k6 [" x if (sp < spaces.length && i == spaces[sp]) {
# {" } R# ^( j' m7 b8 W sp++;
7 x$ x2 h% Q8 b6 R sb.append(' ');3 j2 w" q' c+ z! d, H; I
}) ?* ~! A" g' I: d3 @. e+ K
sb.append(s.charAt(i));
5 G7 _1 S! X' k# D& b# [ }3 s& c* E/ G$ w: j5 x% s% u
return sb.toString();
) V, X9 ` s" t1 ` }
0 l* R2 D4 d; y: t1 F0 c}& D% s# E& D1 F8 h+ Z
* u' Y" G2 w4 J) B
E( b- ^ J" z2 d
【 NO.3 向字符串添加空格】
) t! m" J; v# N0 f4 I D" G' m8 c9 x4 N, R
解题思路9 {1 H+ \& M6 ~0 L x4 [3 x) N! C
双指针。& ?3 W$ @, T, o; A: @% R
9 J5 Y$ f+ \4 L! D+ T$ O
代码展示9 q; C; Y% P3 O! r9 `
+ ]& T" D) d2 J6 d: F" ~, H/ Cclass Solution {
* M( r0 Y# o$ o) R# Q1 h1 Z l0 f public long getDescentPeriods(int[] prices) {
' b9 W" k6 d4 l& Y7 ^ long result = 0;
) e @0 A( B8 x, M$ x- ` for (int i = 0, j = 0; i < prices.length; i++) {/ U' N& H3 x8 b* R( @: |
if (i > 0 && prices[i] != prices[i - 1] - 1) {
% {. N2 p0 }. X/ \( D$ j j = i;
) A/ M+ N2 V- P5 V }
" j8 e8 Z `. K8 {) }) U' F1 F // [j, i] 是一个平滑下跌阶段6 c/ W6 Z3 [ e' {1 t! q, q# Q1 e
result += i - j + 1;# s S$ [" Q! Z; k8 G" s
}1 S! O) h# [' D; A- V
return result;
- s u! a1 \" `2 H$ Q8 p: K+ F }
8 Y/ U! L( D0 s" u; }2 ^}+ J! L9 q4 r- a0 O$ \
# h3 {0 t# \2 p4 a8 D W
- _, J. F* \3 i+ |0 ]% j
【 NO.4 使数组 K 递增的最少操作次数】
9 V! e7 J2 M2 j4 q" g9 g
- R9 K0 A' S2 M' u- F" u( Q解题思路% m4 M4 X: K) [- }8 c u9 o
原数组可以拆分成 K 个子数组,这 K 个子数组之间互不影响。
& \) k7 q* x3 x3 M
2 Q; W5 _3 J f: e/ B) i然后问题就变成了使一个数组变成递增的至少要改变几个元素,直接求最长递增子序列即可,使用 nlogn 的算法。! m+ \; U, a+ q' u7 D9 u* u
. V- r0 v( K% D8 @* y. x& o代码展示
5 Q. h0 g. r8 X# \
( b. S9 X2 _& M; |2 N2 I n; \class Solution {8 X( a$ r, n* B# l7 H; G6 L
public int kIncreasing(int[] arr, int k) {7 u3 P9 V8 F& D6 m! i
int result = 0;
4 h$ Z4 ~' z5 {2 _ for (int i = 0; i < k; i++) {
+ ^, A, {5 t9 y2 Y) V" W* g List<Integer> list = new ArrayList<>();
- Q8 R, [3 w" z, r for (int j = i; j < arr.length; j += k) {' r! M; k: O' }4 E7 I
list.add(arr[j]);
" \0 f5 ^1 J; Z }
. c! C2 ]& Q$ v. B5 h1 d. Y, G+ g, \ result += increasing(list);4 I5 Q' x& r% ~) R6 l* L4 C
}: }; r% [6 b5 k F( T+ A! m
return result;
! }1 W* q6 t2 l+ G+ L }: b- [. S* b9 r& e) |6 s
3 N1 n7 @4 B! T1 q4 r3 u
private int increasing(List<Integer> nums) { [2 H o% g# ^5 k
// 将 nums 变成递增% _4 u7 b) |; F$ o
// nlogn 求 LIS" d' L' S% U0 C" t
int[] dp = new int[nums.size()];1 n% x; \9 s2 o- c3 I$ f2 X
int len = 0;- A* g. @2 O( i7 ?; A1 v7 b5 P) q
for (int num : nums) {
$ |5 E8 w* t) e3 ~6 t int l = 0, r = len;, y3 V8 E$ X2 K8 T. i ~
while (l < r) {; r, K' a" D' x$ d* I
int mid = (l + r) / 2;2 ]5 O5 x4 ]* m! N* z
if (dp[mid] <= num) { // 非严格递增,等于也可
( ~1 O) Z3 k1 L l = mid + 1;
) m$ B$ ~; N s' }9 f) C/ G) k } else {2 e( j4 V* h+ z$ z+ f- I
r = mid;. D+ R+ S- P8 @2 s1 Z- `
}
/ r& p1 s0 N3 e! X* |6 h }+ s9 }: {) y( ]4 d+ g- B+ W
if (r >= len) {8 ~2 M! a. n% J& X# i# V
len++;; w- l+ [7 L4 q0 M/ f v% N
}
. h5 S! v- i O0 g/ o. X dp[r] = num;; c1 h0 L. F- O7 t$ l3 z' J
}# B" e; ~, e0 v0 L6 Z4 M
6 h+ m5 X# O4 Y, u% f8 a' k return nums.size() - len;% T+ H$ k, a/ K, d O' C8 G [
}
, z v* e" y, H4 V} |