登录后可回复主题
您需要 登录 才可以下载或查看,没有帐号?注册账号
x
【 NO.1 Count Integers With Even Digit Sum】 f( v# p' o! U# C! Y$ N
6 P# b4 _2 C# I7 _: {" k解题思路
2 Y- m, _' Y' f; N0 F签到题,枚举计算即可。# q* Q6 k4 J& S$ c5 l
7 E4 c8 }; {; y9 @: I5 {& r
代码展示4 s9 K8 t. f; O6 {6 ~( @/ `1 ~) `
4 o+ {: I6 V+ u/ Q+ s" @: E1 [5 tclass Solution {. f, c% r' m( Z+ j
public int countEven(int num) {
a+ P1 N/ R/ |( } int res = 0;9 y* h+ Y- v8 k0 D# ]" U: |5 ]3 x: s1 k
for (int i = 1; i <= num; i++) {
; c+ D# ]( {6 `4 C if (digitsSum(i) % 2 == 0) {0 s3 H5 M; ~% {0 Q+ G
res++;4 h. J- D5 B, L# e9 [( {3 Z4 v
}
, H% ~$ Q1 d# C! p! A9 _ }& e0 a8 U/ s7 z0 L" d! z5 `0 o0 ^* n
return res;- h+ e7 [: T0 ^. s1 k* t! n
} s3 d- u2 n" O+ _2 c+ v2 J0 @6 P
3 v1 G3 M/ M/ ~7 T private int digitsSum(int num) {
! A1 i* ]6 ^+ i$ g r2 [$ ` c9 Z' A int res = 0;" I" b0 {. q s2 s
for (; num > 0; num /= 10) {
- @; u/ V/ e! ]* S4 A" Q& L res += num % 10;
( L; @5 p7 m4 g }
7 f0 q# D* t8 d/ { return res;
3 [7 w( S$ |) u0 d& N" m }
. N$ B% [# v$ _& o' r( C4 i# I}
* m/ X% c6 W9 K+ {" R# ^
% h) P/ V2 J0 @* {2 D
5 \7 L" F* |& x' z; m9 h5 g/ s) P【 NO.2 Merge Nodes in Between Zeros】
/ ]2 s1 L+ O0 R& V+ s: f& p1 x2 c- i# _4 j* O% _) k. I: R
解题思路7 G, F1 A' z$ L1 I
遍历链表即可。
9 g q t2 \+ u- a5 f2 V4 |3 V% u" C7 f3 x5 j% V7 A
代码展示+ s$ r( q8 P6 i: E/ {5 D5 O* F
i5 r0 V* t1 R4 W- x. p& B) o3 Tclass Solution {1 ? j! j# k! X; `# g7 k
public ListNode mergeNodes(ListNode head) {1 |1 U1 O/ G- k6 e
ListNode res = new ListNode(0);' S, |) N$ ~: y, r5 \
ListNode tail = res;) ]$ d( z6 h8 E9 Y
int sum = 0;
" a& p. a6 B2 G t2 a for (ListNode cur = head.next; cur != null; cur = cur.next) {, C+ u& P5 J- [
sum += cur.val;4 B. H4 \. b9 V7 g8 ]3 o+ |
if (cur.val == 0) {/ V* Z7 l# Q) l( h6 }
tail.next = new ListNode(sum);
+ p* L0 l' e* g) s2 l" s tail = tail.next;( p5 M( ]; i: c
sum = 0;
! a0 g- F5 i/ ]5 K; c }
# Z1 Y" M% _; w- P/ K8 } }
( ~9 m3 s) n/ X/ T8 ~7 O, [* G return res.next;
% n: f3 k7 ]7 n5 { }. L# T8 P8 j( }3 U9 F: v
}) R c- g9 [: ?6 E& |+ q- ^4 U' e2 h6 x
) b9 @6 j3 c0 A! A2 c! A0 `. m" K) m' N
【 NO.3 Merge Nodes in Between Zeros】
4 t; D. C' Y8 T% N7 C% y Z
! R) p2 V# g+ i# _. w, Y解题思路
3 ?' [, ?- K/ p0 L' ^" Q注意题目描述:“不必用完所有的字符”。所以直接贪心即可,从最大字符开始,只要连续超过了 limit 就使用次大的字符分割一下。( p |- J8 n" v
: c3 L3 X h% v' A; C1 N+ m# w, A
代码展示0 O" `( q+ T+ M% M. L
# ]+ p+ v2 I; P: N3 Y, D" d
class Solution {
) @; E+ t8 ~ ]0 j$ T/ r7 Z public String repeatLimitedString(String s, int repeatLimit) {; i4 M" \' q2 O* w
int[] cnt = new int[26];
! k7 I; o) W& Y/ S3 z for (char c : s.toCharArray()) {* A8 w A: n5 U& m
cnt[c - 'a']++;# J5 f, W' U0 [5 O
}" E% O& [1 S/ p s3 Z9 U0 I
; N8 P6 S0 o$ d. p' O* g. Y. D( ?/ f
StringBuilder sb = new StringBuilder();5 w% ?/ @9 [: Z) h' ]
int repeat = 0;
, v9 k% h' P% R* Q6 h char cur = 0;2 i% ]: L& d9 w! L% z
for (int i = 0; i < s.length(); i++) {
+ A# x! Q. |( Y2 [2 L! q' a char c;
% }: c, `7 e0 a; u if (repeat == repeatLimit) {
' d2 t; M7 [7 V: ^, O repeat = 1;
5 }) [( E: D9 u0 [# I7 N2 D7 p% l c = poll(cnt, cur);
( M* ~' J! u3 C F cur = c;/ `2 \5 E& a+ U0 e) A
} else {, E \+ N# F; P/ }. ~
c = poll(cnt, (char) 0);8 }" ~4 ^* e4 W
if (c == cur) {; A3 G8 [4 y1 N6 V' u$ a! ?
repeat++;' x6 d8 @( c( \ ?
} else {9 X O8 Q+ m' a# H# Z( R: x
repeat = 1;
+ N; t% e8 z: I cur = c;
2 o$ Z, P: F; U- s2 d3 t }$ Q! Q, d1 I. d- J
}
* Z' H! \' a* ^% {, l9 H0 z if (c == 0) {. S# }0 F( f% |. f& \8 q1 W1 v! A0 y. M
break;& `4 l1 n4 R) C s6 v
}
" U+ a# e3 u9 |. w! e4 H. T sb.append(c);
! E! H5 I0 A! z4 e( h+ X }7 M6 a; P% A, L7 t/ d$ o) B
return sb.toString();
6 ]# R+ r* Q- h% C }) x. o) V& P1 |
' L6 o) a; y1 ^7 C7 O' B+ k
private char poll(int[] cnt, char not) {! ?' f4 V, f0 U
for (int i = 25; i >= 0; i--) {3 l* l+ f* c ~$ k' J
if (cnt[i] > 0 && not != (char) (i + 'a')) {+ y3 m5 h9 S: \$ y7 N/ s4 |
cnt[i]--;
7 J) F. ~& s) o/ |# z# j' G5 V return (char) (i + 'a');7 B7 C+ f: e- x# H: u" x- ]$ N
}! k a6 Z" n2 u, e0 @ V8 r
}
. V( c6 l0 r N/ C. J% _ return 0;; P1 `; [6 c6 Y g; r& D% R. R
}: R+ i. |# a `4 \) r+ V5 T l0 d
}
% X' K3 v) F& ^& h$ f
7 w' R2 U+ g) z
; S) s$ ^: A# K【 NO.4 Count Array Pairs Divisible by K】
0 g, C: ]! L ~$ d9 ^5 c" C" Q
1 U( {" n9 v0 [3 _# }" e解题思路9 L; ?2 M3 I) q6 ?. z( {: ^- q
预处理转换成最大公因数,详见注释。: g; x' n" q& |8 _0 K$ v" v& X
0 @" p$ J! L6 O( X
代码展示
2 c7 J+ @/ X3 |4 k9 k
" ~' J5 X+ N6 N6 u: Bclass Solution {
8 f* }" b6 ?* w& t( F1 u* B public long coutPairs(int[] nums, int k) {
& E$ P8 z/ u3 j, p2 o // 将 nums 转换成与 k 的最大公约数0 [7 v; l5 x) {6 }
// 因为每个 num 中,k 的因数以外的部分没有意义 (即使做乘法这一部分也无法帮助结果成为 k 的倍数)
8 ~7 t) w t& K0 ?9 T for (int i = 0; i < nums.length; i++) {
1 ~& Z" O# {8 K! u nums[i] = gcd(nums[i], k);
* }- A# L! O% \0 Q' I2 o4 r# z }
7 h O" w: |5 ] long res = 0;
3 c0 U9 V4 Y! ?6 u, @ int[] cnt = new int[k + 1]; // cnt[i] 表示因数 i 的出现次数
" _* A; ]" G/ p; V for (int num : nums) {
7 C$ Z5 M) r6 S5 N2 S5 [( t' s! z // 经过了最初的转换, 此时还需要因数 k / num 即可组成 k 的倍数5 C+ G% \# d$ f" t0 z
res += cnt[k / num];& I; m* H. Z4 z- T. Z- V
y+ e0 ~# b8 B6 C9 q# j! o
// 使用 num 维护 cnt, 即 num 的每个因数 i 都对应一次 cnt[i]++
; g6 ?9 K* L' j1 Z' A. L for (int j = 1; j * j <= num; j++) {
% P# ?6 q6 v0 n1 m) M4 c/ S if (num % j == 0) {* }" y0 x i0 z
cnt[j]++; s1 L2 a: u4 w1 u- A7 }5 U6 o
if (j * j != num) {- G! I+ N9 j; G* p5 x. _2 _
cnt[num / j]++;
% }, @9 g7 i8 R& ?: T }
. C: ^, A$ M) R7 Y }: {. a$ P6 t; g0 u: p
}
% n0 w1 f: D" T6 p; s- `- A- ]& S }
' Y, [5 H v/ s: h return res;
! E, Q8 R* j5 F' i* c7 v/ L( K }
; ~9 V0 A- _; s/ F" e
! e3 Y" L0 O* v m+ u0 A int gcd(int a, int b) {
: s2 V# Y* s( v8 G' o" J; T return a % b == 0 ? b : gcd(b, a % b);
/ n+ `( E4 Q6 V4 h }
/ @7 {5 P6 b$ H* A; L! h0 [ V} |