登录后可回复主题
您需要 登录 才可以下载或查看,没有帐号?注册账号
x
【 NO.1 Count Integers With Even Digit Sum】. z0 r B. o7 s
- ~' K% r8 ]( u) a& `6 g( F- X+ X解题思路6 I, n1 _4 J$ y, S2 ^0 s1 [/ B
签到题,枚举计算即可。- ~7 D( J; [8 p9 ^! d! v1 Q
: e6 U! Q, I4 _0 z! ] e
代码展示9 j1 m$ X: B2 f6 Y, l2 `
0 F! F# N8 F; }0 X2 ]8 ?5 e8 o
class Solution {: I$ K( Q0 b3 C
public int countEven(int num) {
* U [8 ]4 f8 T/ @3 z" \ int res = 0;* J7 M8 X7 ^2 N3 C- [
for (int i = 1; i <= num; i++) {
/ M3 @; J6 M+ x: A$ D9 o0 s if (digitsSum(i) % 2 == 0) {
& @6 e% `0 m. O: y res++;7 l7 }% f' Q' ^. k; d0 X
}
' }3 C: D' d% _- L }
9 j, p/ ^4 U4 f0 H# _) v; w: c return res;
1 t2 H9 v+ I" K/ J }$ \* h1 _4 S; V4 t4 [) N
, p# H) p$ V a% Y+ V) O/ E private int digitsSum(int num) {
/ s+ K8 [# ^6 G- U/ h int res = 0;) L, p o; l# y: R
for (; num > 0; num /= 10) {/ c5 y9 {/ l7 r/ P- j4 m' w
res += num % 10;
* {" U# e, \0 E u$ M }
' u) e( {! u' F. G& X3 w3 B return res;0 h5 A; y8 l2 u0 p% Y! ?0 b5 g
}
" y9 h4 K: N/ ^# y" t}
( i* {; P2 A, h; E3 q
7 R6 I, T9 C7 M8 c
# k Z. s* m0 q4 \, Z* T& B. m+ ?【 NO.2 Merge Nodes in Between Zeros】
* S3 K7 |, p; M3 N
d3 a) g% t7 { I5 }. W c) y解题思路
. j( z v1 ]. M* q5 ?# ?) @遍历链表即可。# h* g, [% ]" I5 o' w
: x/ Z+ O9 Z# U, f代码展示
/ e# ~8 D& Q' d x) c7 R% q# K n9 m& ?( S8 H
class Solution {; P# K% c! ?2 W; f% A1 J; I
public ListNode mergeNodes(ListNode head) {
) z0 S5 c4 V# b( R, p) x4 H) G ListNode res = new ListNode(0);- u. V# e Z2 K4 S
ListNode tail = res;" e9 O5 f$ t& p; n2 H G7 O$ _# H4 @( W' t
int sum = 0;! U7 p0 x9 U* j* X9 i2 F# k7 w, j/ B% W$ o
for (ListNode cur = head.next; cur != null; cur = cur.next) {* j0 _8 Z( Z0 s g
sum += cur.val;
3 m, f% C$ q9 I6 z# { if (cur.val == 0) {
3 l3 | l5 u, h tail.next = new ListNode(sum);
2 r3 x6 t+ e j6 C& X1 N9 _6 U tail = tail.next;) m! V# R% m. B
sum = 0;' U- \) _ r2 s9 ]/ m. f2 l
}3 E7 _0 {; m" X& J2 P
}1 F4 Q1 [1 U& L
return res.next;2 N: n4 I4 u1 F* y& [
}$ Y8 V. t% x$ Q5 i# z/ C7 Z
}! A% o2 e& K: h/ I+ \& O- A- P$ f- j# [
5 [0 Q4 ~) R: y1 Y) n
& b+ \. G6 G/ Y7 H" Q
【 NO.3 Merge Nodes in Between Zeros】
& R, p3 l8 E$ m) b6 o: _% k/ J k) ~3 W+ x, E6 q L
解题思路
* p' l$ d% \$ K; T0 D, E注意题目描述:“不必用完所有的字符”。所以直接贪心即可,从最大字符开始,只要连续超过了 limit 就使用次大的字符分割一下。
T7 e( Y' \" t
! j5 U% [6 R4 Z: [7 c代码展示/ q2 C1 x+ O1 N: B" v# j
+ I2 K$ C5 G% o( p y5 r' vclass Solution {- }" L, F& N& I
public String repeatLimitedString(String s, int repeatLimit) {
M. I! H; @1 R9 s+ E. F int[] cnt = new int[26];4 m3 J2 a6 h% O8 T2 B" O: l4 f3 B$ m
for (char c : s.toCharArray()) {! s$ `; J! \9 R9 y
cnt[c - 'a']++;1 x% \# x4 y) M
}
/ X& {$ }3 z! S4 p" P4 S# h- A* Y9 x! R) V
StringBuilder sb = new StringBuilder();+ d i5 b) Q6 [5 f$ S, ?
int repeat = 0;
' U% ^4 t0 |0 A9 v! W! h+ ~ char cur = 0; ^4 h, F q* s
for (int i = 0; i < s.length(); i++) {8 d: \. r/ M* E7 P( I) J
char c;/ H5 R: S+ X2 X- a
if (repeat == repeatLimit) {
9 ~3 a8 N6 ~: u1 T5 B% m/ L repeat = 1; e0 E7 |* _( l* R8 {
c = poll(cnt, cur);
7 b) _2 g3 r3 g8 q cur = c;+ F9 y# k) p% q4 L6 {% P3 @- @% n7 ^- r
} else {5 L& g( w5 I0 d0 O0 n9 V6 b
c = poll(cnt, (char) 0);
9 q- M- _, b/ W& c8 _+ e* K if (c == cur) {
* Z' X" |0 R" [/ u: B# Q repeat++;
4 g$ g* t' [) O* ]' ]. P @ } else {
4 ]: o( Q% x8 I6 G! z9 w" L repeat = 1;( Z% t5 N! J9 c9 o. B3 y9 G% q
cur = c;
; ?8 u' j$ Z) l0 z2 `4 w }1 y4 _9 q, d' X4 A3 u
}
. F j0 Y" g3 V" u9 l% e if (c == 0) {! U& j$ t1 b$ c/ ^ h
break;1 d' V8 l) f. y+ `4 t
}
+ v" |2 B: R- A. Y sb.append(c);/ M0 E0 U) e$ u+ @
}1 ?( y6 l# I* M7 b" y" z* |$ p
return sb.toString();# Z, @, Z) Q5 { ^% s" C+ m9 L7 }! k
}0 \3 g! J4 @0 J+ w
" Y% s$ Q2 D& g; C% Z
private char poll(int[] cnt, char not) {1 h' a0 |0 Y5 h" ]' D* P" I
for (int i = 25; i >= 0; i--) {6 ~3 D/ A0 e* S% x
if (cnt[i] > 0 && not != (char) (i + 'a')) {
* m8 E0 J; B5 s6 Z" N cnt[i]--;
+ S. \/ z% p/ y D, L* s return (char) (i + 'a');
: o5 {% E# f0 \$ k }' f) I `) B: C( k: J$ c
}6 q0 o) G) t3 t0 [
return 0;
1 V' w; r# _- u; C0 Y4 A s }' Z" p2 Q6 ?7 a- r3 D3 O1 [
}% b- P" [6 V+ o0 \1 Z, b6 u
: x8 S4 J/ D6 m+ g! J
5 N) V3 v( {1 D: i
【 NO.4 Count Array Pairs Divisible by K】
7 k0 j/ u3 M( Y
7 O0 ]1 R# ?9 v% I解题思路, a5 }5 Y$ X5 J7 B6 e# }, m
预处理转换成最大公因数,详见注释。1 V, L. t: r L3 k ^
0 B" ^' B6 H5 b
代码展示2 z4 s/ @$ ]4 h' } e2 f; Y
# T5 [: i( C$ p5 A* t' |
class Solution {& R) v# y' q0 Y9 t4 _2 D, |
public long coutPairs(int[] nums, int k) {
3 X+ T! D/ i1 {% p // 将 nums 转换成与 k 的最大公约数7 n2 C: `) l# V7 W
// 因为每个 num 中,k 的因数以外的部分没有意义 (即使做乘法这一部分也无法帮助结果成为 k 的倍数)2 j0 h% f2 |4 Q9 R. @- M) q2 X
for (int i = 0; i < nums.length; i++) {
6 Y' O1 S% u+ h& e2 T b nums[i] = gcd(nums[i], k);
8 j& u$ D0 P* x7 `( ]& \, N }
- S7 j6 w) V* T% w long res = 0;
( i+ E4 w7 t- ?. N# U! h; v4 e int[] cnt = new int[k + 1]; // cnt[i] 表示因数 i 的出现次数
1 b# z0 W9 z) p- ~ for (int num : nums) {; C& E# u+ f6 ]( m
// 经过了最初的转换, 此时还需要因数 k / num 即可组成 k 的倍数
$ Z7 H) Q. w [: G# e* `, f2 l res += cnt[k / num];: U1 l) U9 f, U1 `7 x2 I
) C' \7 v+ N; Q- ^) v
// 使用 num 维护 cnt, 即 num 的每个因数 i 都对应一次 cnt[i]++
' M2 b# }" ]! M/ Y: p$ W for (int j = 1; j * j <= num; j++) {2 N$ z) u; @' s% w' P8 \( s' k
if (num % j == 0) {
9 x z8 Y. }4 o( t9 e; D/ W cnt[j]++;
/ A. B/ U7 Z/ N+ H6 j if (j * j != num) {2 V, l! R* e* m) l! K$ C
cnt[num / j]++; I8 h2 p3 _ a
}8 d* {6 q; S( t/ [7 g
}9 T+ `3 l4 v- V- F
}
+ k7 a8 K ]* O1 q" Z }! l8 t7 a3 s+ ?0 s$ V+ @, d
return res;$ e- p* F: |" Q) A+ l; }( N
}
0 U; K1 h5 d0 N$ ^/ R7 V4 @5 p, A3 }9 Z. B; M2 r% \5 I
int gcd(int a, int b) {2 n6 ]* W2 M6 o! d( I' t* O X
return a % b == 0 ? b : gcd(b, a % b);4 e. t: r; X2 `
}
* f2 L5 t) D# {9 z} |