Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 1d37dd7

Browse files
committed
更新 README.md
1 parent e70bcf3 commit 1d37dd7

File tree

5 files changed

+63
-184
lines changed

5 files changed

+63
-184
lines changed

Algorithms/0972.equal-rational-numbers/equal-rational-numbers.go

Lines changed: 21 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -5,93 +5,29 @@ import (
55
"strings"
66
)
77

8-
func isRationalEqual(S string, T string) bool {
9-
si, sn, sr := parse(S)
10-
sn, sr = simplify(sn, sr)
11-
s, sr := convert(si, sn, sr)
12-
13-
ti, tn, tr := parse(T)
14-
tn, tr = simplify(tn, tr)
15-
t, tr := convert(ti, tn, tr)
16-
17-
return s == t &&
18-
sr == tr
19-
}
20-
21-
func parse(s string) (string, string, string) {
22-
if strings.HasSuffix(s, ".") {
23-
s = s[:len(s)-1]
24-
}
25-
26-
dot := strings.Index(s, ".")
27-
if dot == -1 {
28-
return s, "", ""
29-
}
30-
31-
integer, fraction := s[:dot], s[dot+1:]
32-
33-
l := strings.Index(fraction, "(")
34-
if l == -1 {
35-
if fraction == "0" {
36-
fraction = ""
37-
}
38-
return integer, fraction, ""
39-
}
40-
41-
nonRepeat := fraction[:l]
42-
repeat := fraction[l+1 : len(fraction)-1]
43-
44-
if repeat == "0" {
45-
repeat = ""
46-
}
47-
48-
if repeat == "" && nonRepeat == "0" {
49-
nonRepeat = ""
50-
}
8+
// 首先,牢记 note 中提到的 3 点
9+
// 3. 1 <= <IntegerPart>.length <= 4
10+
// 4. 0 <= <NonRepeatingPart>.length <= 4
11+
// 5. 1 <= <RepeatingPart>.length <= 4
12+
// 再根据 IEEE 754 中 https://zh.wikipedia.org/wiki/IEEE_754#%E8%AE%A8%E8%AE%BA%E4%B8%80 提到的 64 bit 双精度可以准确地表示 15 位十进制数
13+
// 分两种情况讨论:
14+
// 1: 没有重复部分,可以直接精准地转换成 float64
15+
// 2: 存在重复部分,可以延展 S 到 20 位长度后,再近似地转换成 float64
16+
// 因为 note 中规定了各个部分的长度,
17+
// A: 重复部分不为 9 的话,在 15 位以内就会出现差别
18+
// B: 重复部分为 9 的话, 至少 S[9:20] 中皆为 9,会在四舍五入时进位,与数学规定一致
5119

52-
return integer, nonRepeat, repeat
53-
}
54-
55-
func simplify(nonRepeat, repeat string) (string, string) {
56-
if repeat == "" {
57-
return nonRepeat, repeat
58-
}
59-
60-
if repeat == strings.Repeat(repeat[:1], len(repeat)) {
61-
repeat = repeat[:1]
62-
}
63-
64-
for repeat[:len(repeat)/2] == repeat[len(repeat)/2:] {
65-
repeat = repeat[:len(repeat)/2]
66-
}
67-
68-
for strings.HasSuffix(nonRepeat, repeat) {
69-
nonRepeat = nonRepeat[:len(nonRepeat)-len(repeat)]
70-
}
71-
72-
for i := 1; i < len(repeat); i++ {
73-
if strings.HasSuffix(nonRepeat, repeat[i:]) {
74-
repeat = repeat[i:] + repeat[:i]
75-
nonRepeat = nonRepeat[:len(nonRepeat)-len(repeat)+i]
76-
break
77-
}
78-
}
79-
80-
return nonRepeat, repeat
20+
func isRationalEqual(S string, T string) bool {
21+
return convert(S) == convert(T)
8122
}
8223

83-
func convert(integer, nonRepeat, repeat string) (int, string) {
84-
i, _ := strconv.Atoi(integer)
85-
for j := len(nonRepeat); j > 0; j-- {
86-
i *= 10
87-
}
88-
if nonRepeat != "" {
89-
n, _ := strconv.Atoi(nonRepeat)
90-
i += n
91-
}
92-
if repeat == "9" {
93-
i++
94-
repeat = ""
24+
func convert(s string) float64 {
25+
i := strings.IndexByte(s, '(')
26+
if i != -1 {
27+
base, repeat := s[:i], s[i+1:len(s)-1]
28+
s = base + strings.Repeat(repeat, (20-len(base))/len(repeat)+1)
29+
s = s[:20] // 20 could be 19, but not 18
9530
}
96-
return i, repeat
31+
res, _ := strconv.ParseFloat(s, 64)
32+
return res
9733
}

Algorithms/0972.equal-rational-numbers/equal-rational-numbers_test.go

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -91,65 +91,3 @@ func Benchmark_isRationalEqual(b *testing.B) {
9191
}
9292
}
9393
}
94-
95-
func Test_parse(t *testing.T) {
96-
type args struct {
97-
s string
98-
}
99-
tests := []struct {
100-
name string
101-
args args
102-
integer string
103-
nonRep string
104-
repeat string
105-
}{
106-
107-
{
108-
"1",
109-
args{"1"},
110-
"1",
111-
"",
112-
"",
113-
},
114-
115-
{
116-
"1.",
117-
args{"1."},
118-
"1",
119-
"",
120-
"",
121-
},
122-
123-
{
124-
"1.0",
125-
args{"1.0"},
126-
"1",
127-
"",
128-
"",
129-
},
130-
131-
{
132-
"1.0(9)",
133-
args{"1.0(9)"},
134-
"1",
135-
"0",
136-
"9",
137-
},
138-
139-
//
140-
}
141-
for _, tt := range tests {
142-
t.Run(tt.name, func(t *testing.T) {
143-
integer, nonRep, repeat := parse(tt.args.s)
144-
if integer != tt.integer {
145-
t.Errorf("parse() integer = %v, want %v", integer, tt.integer)
146-
}
147-
if nonRep != tt.nonRep {
148-
t.Errorf("parse() nonRepeat = %v, want %v", nonRep, tt.nonRep)
149-
}
150-
if repeat != tt.repeat {
151-
t.Errorf("parse() repeat = %v, want %v", repeat, tt.repeat)
152-
}
153-
})
154-
}
155-
}

Favorite.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 我收藏的 283
1+
# 我收藏的 284
22

33
|题号|题目|通过率|难度|收藏|
44
|:-:|:-|:-: | :-: | :-: |
@@ -68,7 +68,7 @@
6868
|[0279](https://leetcode.com/problems/perfect-squares/)|[Perfect Squares](./Algorithms/0279.perfect-squares)|41%|Medium|[](https://leetcode.com/list/oussv5j)|
6969
|[0282](https://leetcode.com/problems/expression-add-operators/)|[Expression Add Operators](./Algorithms/0282.expression-add-operators)|32%|Hard|[](https://leetcode.com/list/oussv5j)|
7070
|[0287](https://leetcode.com/problems/find-the-duplicate-number/)|[Find the Duplicate Number](./Algorithms/0287.find-the-duplicate-number)|49%|Medium|[](https://leetcode.com/list/oussv5j)|
71-
|[0289](https://leetcode.com/problems/game-of-life/)|[Game of Life](./Algorithms/0289.game-of-life)|44%|Medium|[](https://leetcode.com/list/oussv5j)|
71+
|[0289](https://leetcode.com/problems/game-of-life/)|[Game of Life](./Algorithms/0289.game-of-life)|45%|Medium|[](https://leetcode.com/list/oussv5j)|
7272
|[0292](https://leetcode.com/problems/nim-game/)|[Nim Game](./Algorithms/0292.nim-game)|55%|Easy|[](https://leetcode.com/list/oussv5j)|
7373
|[0295](https://leetcode.com/problems/find-median-from-data-stream/)|[Find Median from Data Stream](./Algorithms/0295.find-median-from-data-stream)|36%|Hard|[](https://leetcode.com/list/oussv5j)|
7474
|[0300](https://leetcode.com/problems/longest-increasing-subsequence/)|[Longest Increasing Subsequence](./Algorithms/0300.longest-increasing-subsequence)|40%|Medium|[](https://leetcode.com/list/oussv5j)|
@@ -95,7 +95,7 @@
9595
|[0354](https://leetcode.com/problems/russian-doll-envelopes/)|[Russian Doll Envelopes](./Algorithms/0354.russian-doll-envelopes)|33%|Hard|[](https://leetcode.com/list/oussv5j)|
9696
|[0355](https://leetcode.com/problems/design-twitter/)|[Design Twitter](./Algorithms/0355.design-twitter)|27%|Medium|[](https://leetcode.com/list/oussv5j)|
9797
|[0357](https://leetcode.com/problems/count-numbers-with-unique-digits/)|[Count Numbers with Unique Digits](./Algorithms/0357.count-numbers-with-unique-digits)|46%|Medium|[](https://leetcode.com/list/oussv5j)|
98-
|[0363](https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/)|[Max Sum of Rectangle No Larger Than K](./Algorithms/0363.max-sum-of-rectangle-no-larger-than-k)|35%|Hard|[](https://leetcode.com/list/oussv5j)|
98+
|[0363](https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/)|[Max Sum of Rectangle No Larger Than K](./Algorithms/0363.max-sum-of-rectangle-no-larger-than-k)|34%|Hard|[](https://leetcode.com/list/oussv5j)|
9999
|[0365](https://leetcode.com/problems/water-and-jug-problem/)|[Water and Jug Problem](./Algorithms/0365.water-and-jug-problem)|28%|Medium|[](https://leetcode.com/list/oussv5j)|
100100
|[0368](https://leetcode.com/problems/largest-divisible-subset/)|[Largest Divisible Subset](./Algorithms/0368.largest-divisible-subset)|34%|Medium|[](https://leetcode.com/list/oussv5j)|
101101
|[0371](https://leetcode.com/problems/sum-of-two-integers/)|[Sum of Two Integers](./Algorithms/0371.sum-of-two-integers)|50%|Easy|[](https://leetcode.com/list/oussv5j)|
@@ -119,7 +119,7 @@
119119
|[0416](https://leetcode.com/problems/partition-equal-subset-sum/)|[Partition Equal Subset Sum](./Algorithms/0416.partition-equal-subset-sum)|40%|Medium|[](https://leetcode.com/list/oussv5j)|
120120
|[0420](https://leetcode.com/problems/strong-password-checker/)|[Strong Password Checker](./Algorithms/0420.strong-password-checker)|17%|Hard|[](https://leetcode.com/list/oussv5j)|
121121
|[0421](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/)|[Maximum XOR of Two Numbers in an Array](./Algorithms/0421.maximum-xor-of-two-numbers-in-an-array)|50%|Medium|[](https://leetcode.com/list/oussv5j)|
122-
|[0424](https://leetcode.com/problems/longest-repeating-character-replacement/)|[Longest Repeating Character Replacement](./Algorithms/0424.longest-repeating-character-replacement)|43%|Medium|[](https://leetcode.com/list/oussv5j)|
122+
|[0424](https://leetcode.com/problems/longest-repeating-character-replacement/)|[Longest Repeating Character Replacement](./Algorithms/0424.longest-repeating-character-replacement)|44%|Medium|[](https://leetcode.com/list/oussv5j)|
123123
|[0432](https://leetcode.com/problems/all-oone-data-structure/)|[All O`one Data Structure](./Algorithms/0432.all-oone-data-structure)|29%|Hard|[](https://leetcode.com/list/oussv5j)|
124124
|[0435](https://leetcode.com/problems/non-overlapping-intervals/)|[Non-overlapping Intervals](./Algorithms/0435.non-overlapping-intervals)|41%|Medium|[](https://leetcode.com/list/oussv5j)|
125125
|[0437](https://leetcode.com/problems/path-sum-iii/)|[Path Sum III](./Algorithms/0437.path-sum-iii)|42%|Easy|[](https://leetcode.com/list/oussv5j)|
@@ -217,7 +217,7 @@
217217
|[0792](https://leetcode.com/problems/number-of-matching-subsequences/)|[Number of Matching Subsequences](./Algorithms/0792.number-of-matching-subsequences)|42%|Medium|[](https://leetcode.com/list/oussv5j)|
218218
|[0793](https://leetcode.com/problems/preimage-size-of-factorial-zeroes-function/)|[Preimage Size of Factorial Zeroes Function](./Algorithms/0793.preimage-size-of-factorial-zeroes-function)|38%|Hard|[](https://leetcode.com/list/oussv5j)|
219219
|[0795](https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/)|[Number of Subarrays with Bounded Maximum](./Algorithms/0795.number-of-subarrays-with-bounded-maximum)|42%|Medium|[](https://leetcode.com/list/oussv5j)|
220-
|[0798](https://leetcode.com/problems/smallest-rotation-with-highest-score/)|[Smallest Rotation with Highest Score](./Algorithms/0798.smallest-rotation-with-highest-score)|39%|Hard|[](https://leetcode.com/list/oussv5j)|
220+
|[0798](https://leetcode.com/problems/smallest-rotation-with-highest-score/)|[Smallest Rotation with Highest Score](./Algorithms/0798.smallest-rotation-with-highest-score)|40%|Hard|[](https://leetcode.com/list/oussv5j)|
221221
|[0799](https://leetcode.com/problems/champagne-tower/)|[Champagne Tower](./Algorithms/0799.champagne-tower)|33%|Medium|[](https://leetcode.com/list/oussv5j)|
222222
|[0801](https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/)|[Minimum Swaps To Make Sequences Increasing](./Algorithms/0801.minimum-swaps-to-make-sequences-increasing)|34%|Medium|[](https://leetcode.com/list/oussv5j)|
223223
|[0802](https://leetcode.com/problems/find-eventual-safe-states/)|[Find Eventual Safe States](./Algorithms/0802.find-eventual-safe-states)|43%|Medium|[](https://leetcode.com/list/oussv5j)|
@@ -248,7 +248,7 @@
248248
|[0862](https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/)|[Shortest Subarray with Sum at Least K](./Algorithms/0862.shortest-subarray-with-sum-at-least-k)|22%|Hard|[](https://leetcode.com/list/oussv5j)|
249249
|[0863](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/)|[All Nodes Distance K in Binary Tree](./Algorithms/0863.all-nodes-distance-k-in-binary-tree)|46%|Medium|[](https://leetcode.com/list/oussv5j)|
250250
|[0864](https://leetcode.com/problems/shortest-path-to-get-all-keys/)|[Shortest Path to Get All Keys](./Algorithms/0864.shortest-path-to-get-all-keys)|35%|Hard|[](https://leetcode.com/list/oussv5j)|
251-
|[0866](https://leetcode.com/problems/prime-palindrome/)|[Prime Palindrome](./Algorithms/0866.prime-palindrome)|20%|Medium|[](https://leetcode.com/list/oussv5j)|
251+
|[0866](https://leetcode.com/problems/prime-palindrome/)|[Prime Palindrome](./Algorithms/0866.prime-palindrome)|19%|Medium|[](https://leetcode.com/list/oussv5j)|
252252
|[0879](https://leetcode.com/problems/profitable-schemes/)|[Profitable Schemes](./Algorithms/0879.profitable-schemes)|36%|Hard|[](https://leetcode.com/list/oussv5j)|
253253
|[0895](https://leetcode.com/problems/maximum-frequency-stack/)|[Maximum Frequency Stack](./Algorithms/0895.maximum-frequency-stack)|55%|Hard|[](https://leetcode.com/list/oussv5j)|
254254
|[0896](https://leetcode.com/problems/monotonic-array/)|[Monotonic Array](./Algorithms/0896.monotonic-array)|55%|Easy|[](https://leetcode.com/list/oussv5j)|
@@ -283,5 +283,6 @@
283283
|[0960](https://leetcode.com/problems/delete-columns-to-make-sorted-iii/)|[Delete Columns to Make Sorted III](./Algorithms/0960.delete-columns-to-make-sorted-iii)|52%|Hard|[](https://leetcode.com/list/oussv5j)|
284284
|[0962](https://leetcode.com/problems/maximum-width-ramp/)|[Maximum Width Ramp](./Algorithms/0962.maximum-width-ramp)|41%|Medium|[](https://leetcode.com/list/oussv5j)|
285285
|[0964](https://leetcode.com/problems/least-operators-to-express-number/)|[Least Operators to Express Number](./Algorithms/0964.least-operators-to-express-number)|40%|Hard|[](https://leetcode.com/list/oussv5j)|
286+
|[0972](https://leetcode.com/problems/equal-rational-numbers/)|[Equal Rational Numbers](./Algorithms/0972.equal-rational-numbers)|40%|Hard|[](https://leetcode.com/list/oussv5j)|
286287
|[0976](https://leetcode.com/problems/largest-perimeter-triangle/)|[Largest Perimeter Triangle](./Algorithms/0976.largest-perimeter-triangle)|56%|Easy|[](https://leetcode.com/list/oussv5j)|
287288
|[0980](https://leetcode.com/problems/unique-paths-iii/)|[Unique Paths III](./Algorithms/0980.unique-paths-iii)|71%|Hard|[](https://leetcode.com/list/oussv5j)|

0 commit comments

Comments
 (0)