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

Commit 0d404b8

Browse files
committed
972 错误的思路
1 parent 5bdcdc7 commit 0d404b8

File tree

2 files changed

+75
-16
lines changed

2 files changed

+75
-16
lines changed

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

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import (
66
)
77

88
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)
9+
S = normalize(S)
10+
sn, sr := parse2(S)
11+
sn, sr = simplify2(sn, sr)
12+
s, sr := convert2(sn, sr)
1213

13-
ti, tn, tr := parse(T)
14-
tn, tr = simplify(tn, tr)
15-
t, tr := convert(ti, tn, tr)
14+
T = normalize(T)
15+
tn, tr := parse2(T)
16+
tn, tr = simplify2(tn, tr)
17+
t, tr := convert2(tn, tr)
1618

1719
return s == t && sr == tr
1820
}
@@ -22,11 +24,31 @@ func normalize(s string) string {
2224
if !strings.Contains(s, ".") {
2325
s += "."
2426
}
27+
if strings.HasSuffix(s, ".") {
28+
s += "0"
29+
}
2530
dot := strings.Index(s, ".")
2631
if dot < 4 {
2732
s = strings.Repeat("0", 4-dot) + s
2833
}
29-
return strings.Replace(s, ".", "", 1)
34+
return strings.Replace("1"+s, ".", "", 1)
35+
}
36+
37+
func parse2(s string) (string, string) {
38+
if !strings.Contains(s, "(") {
39+
return s, ""
40+
}
41+
42+
i := strings.Index(s, "(")
43+
44+
nonRepeat := s[:i]
45+
repeat := s[i+1 : len(s)-1]
46+
47+
if repeat == "0" {
48+
repeat = ""
49+
}
50+
51+
return nonRepeat, repeat
3052
}
3153

3254
func parse(s string) (string, string, string) {
@@ -63,6 +85,34 @@ func parse(s string) (string, string, string) {
6385
return integer, nonRepeat, repeat
6486
}
6587

88+
func simplify2(nonRepeat, repeat string) (string, string) {
89+
if repeat == "" {
90+
return nonRepeat, repeat
91+
}
92+
93+
if repeat == strings.Repeat(repeat[:1], len(repeat)) {
94+
repeat = repeat[:1]
95+
}
96+
97+
for repeat[:len(repeat)/2] == repeat[len(repeat)/2:] {
98+
repeat = repeat[:len(repeat)/2]
99+
}
100+
101+
for strings.HasSuffix(nonRepeat, repeat) {
102+
nonRepeat = nonRepeat[:len(nonRepeat)-len(repeat)]
103+
}
104+
105+
for i := 1; i < len(repeat); i++ {
106+
if strings.HasSuffix(nonRepeat, repeat[i:]) {
107+
repeat = repeat[i:] + repeat[:i]
108+
nonRepeat = nonRepeat[:len(nonRepeat)-len(repeat)+i]
109+
break
110+
}
111+
}
112+
113+
return nonRepeat, repeat
114+
}
115+
66116
func simplify(nonRepeat, repeat string) (string, string) {
67117
if repeat == "" {
68118
return nonRepeat, repeat
@@ -91,6 +141,15 @@ func simplify(nonRepeat, repeat string) (string, string) {
91141
return nonRepeat, repeat
92142
}
93143

144+
func convert2(nonRepeat, repeat string) (int, string) {
145+
i, _ := strconv.Atoi(nonRepeat)
146+
if repeat == "9" {
147+
i++
148+
repeat = ""
149+
}
150+
return i, repeat
151+
}
152+
94153
func convert(integer, nonRepeat, repeat string) (int, string) {
95154
i, _ := strconv.Atoi(integer)
96155
for j := len(nonRepeat); j > 0; j-- {

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ var tcs = []struct {
1313
ans bool
1414
}{
1515

16+
{
17+
"1.0",
18+
"1",
19+
true,
20+
},
21+
1622
{
1723
"350.(111)",
1824
"350.(11)",
@@ -55,12 +61,6 @@ var tcs = []struct {
5561
true,
5662
},
5763

58-
{
59-
"1.0",
60-
"1",
61-
true,
62-
},
63-
6464
{
6565
"0.(52)",
6666
"0.5(25)",
@@ -167,19 +167,19 @@ func Test_normalize(t *testing.T) {
167167
{
168168
"1",
169169
args{"1"},
170-
"0001",
170+
"10001",
171171
},
172172

173173
{
174174
"1234.",
175175
args{"1234."},
176-
"1234",
176+
"11234",
177177
},
178178

179179
{
180180
"1234.(1)",
181181
args{"1234.(1)"},
182-
"1234(1)",
182+
"11234(1)",
183183
},
184184

185185
// Add test cases.

0 commit comments

Comments
 (0)