Skip to content

Commit 6a28588

Browse files
committed
feat: add solutions to lc problem: No.0166.Fraction to Recurring Decimal
1 parent 6d65212 commit 6a28588

File tree

6 files changed

+406
-35
lines changed

6 files changed

+406
-35
lines changed

solution/0100-0199/0166.Fraction to Recurring Decimal/README.md

Lines changed: 142 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,167 @@
6060
<li><code>denominator != 0</code></li>
6161
</ul>
6262

63-
6463
## 解法
6564

6665
<!-- 这里可写通用的实现逻辑 -->
6766

67+
在进行除法时使用 HashMap 存储余数及其关联的索引,这样每当出现相同的余数时,我们就知道有一个重复的小数部分。
68+
6869
<!-- tabs:start -->
6970

7071
### **Python3**
7172

7273
<!-- 这里可写当前语言的特殊实现逻辑 -->
7374

7475
```python
75-
76+
class Solution:
77+
def fractionToDecimal(self, numerator: int, denominator: int) -> str:
78+
if numerator == 0:
79+
return '0'
80+
res = []
81+
neg = (numerator > 0) ^ (denominator > 0)
82+
if neg:
83+
res.append('-')
84+
num, d = abs(numerator), abs(denominator)
85+
res.append(str(num // d))
86+
num %= d
87+
if num == 0:
88+
return ''.join(res)
89+
res.append('.')
90+
mp = {}
91+
while num != 0:
92+
mp[num] = len(res)
93+
num *= 10
94+
res.append(str(num // d))
95+
num %= d
96+
if num in mp:
97+
idx = mp[num]
98+
res.insert(idx, '(')
99+
res.append(')')
100+
break
101+
return ''.join(res)
76102
```
77103

78104
### **Java**
79105

80106
<!-- 这里可写当前语言的特殊实现逻辑 -->
81107

82108
```java
109+
class Solution {
110+
public String fractionToDecimal(int numerator, int denominator) {
111+
if (numerator == 0) {
112+
return "0";
113+
}
114+
StringBuilder sb = new StringBuilder();
115+
boolean neg = (numerator > 0) ^ (denominator > 0);
116+
sb.append(neg ? "-" : "");
117+
long num = Math.abs((long) numerator);
118+
long d = Math.abs((long) denominator);
119+
sb.append(num / d);
120+
num %= d;
121+
if (num == 0) {
122+
return sb.toString();
123+
}
124+
sb.append(".");
125+
Map<Long, Integer> mp = new HashMap<>();
126+
while (num != 0) {
127+
mp.put(num, sb.length());
128+
num *= 10;
129+
sb.append(num / d);
130+
num %= d;
131+
if (mp.containsKey(num)) {
132+
int idx = mp.get(num);
133+
sb.insert(idx, "(");
134+
sb.append(")");
135+
break;
136+
}
137+
}
138+
return sb.toString();
139+
}
140+
}
141+
```
142+
143+
### **C++**
144+
145+
```cpp
146+
using LL = long long;
147+
148+
class Solution {
149+
public:
150+
string fractionToDecimal(int numerator, int denominator) {
151+
if (numerator == 0) return "0";
152+
string res = "";
153+
bool neg = (numerator > 0) ^ (denominator > 0);
154+
if (neg) res += "-";
155+
LL num = abs(numerator);
156+
LL d = abs(denominator);
157+
res += to_string(num / d);
158+
num %= d;
159+
if (num == 0) return res;
160+
res += ".";
161+
unordered_map<LL, int> mp;
162+
while (num)
163+
{
164+
mp[num] = res.size();
165+
num *= 10;
166+
res += to_string(num / d);
167+
num %= d;
168+
if (mp.count(num))
169+
{
170+
int idx = mp[num];
171+
res.insert(idx, "(");
172+
res += ")";
173+
break;
174+
}
175+
}
176+
return res;
177+
}
178+
};
179+
```
83180
181+
### **Go**
182+
183+
```go
184+
func fractionToDecimal(numerator int, denominator int) string {
185+
if numerator == 0 {
186+
return "0"
187+
}
188+
res := []byte{}
189+
neg := numerator*denominator < 0
190+
if neg {
191+
res = append(res, '-')
192+
}
193+
num := abs(numerator)
194+
d := abs(denominator)
195+
res = append(res, strconv.Itoa(num/d)...)
196+
num %= d
197+
if num == 0 {
198+
return string(res)
199+
}
200+
mp := make(map[int]int)
201+
res = append(res, '.')
202+
for num != 0 {
203+
mp[num] = len(res)
204+
num *= 10
205+
res = append(res, strconv.Itoa(num/d)...)
206+
num %= d
207+
if mp[num] > 0 {
208+
idx := mp[num]
209+
res = append(res[:idx], append([]byte{'('}, res[idx:]...)...)
210+
res = append(res, ')')
211+
break
212+
}
213+
}
214+
215+
return string(res)
216+
}
217+
218+
func abs(x int) int {
219+
if x < 0 {
220+
return -x
221+
}
222+
return x
223+
}
84224
```
85225

86226
### **...**

solution/0100-0199/0166.Fraction to Recurring Decimal/README_EN.md

Lines changed: 140 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,159 @@
3737
<li><code>denominator != 0</code></li>
3838
</ul>
3939

40-
4140
## Solutions
4241

4342
<!-- tabs:start -->
4443

4544
### **Python3**
4645

4746
```python
48-
47+
class Solution:
48+
def fractionToDecimal(self, numerator: int, denominator: int) -> str:
49+
if numerator == 0:
50+
return '0'
51+
res = []
52+
neg = (numerator > 0) ^ (denominator > 0)
53+
if neg:
54+
res.append('-')
55+
num, d = abs(numerator), abs(denominator)
56+
res.append(str(num // d))
57+
num %= d
58+
if num == 0:
59+
return ''.join(res)
60+
res.append('.')
61+
mp = {}
62+
while num != 0:
63+
mp[num] = len(res)
64+
num *= 10
65+
res.append(str(num // d))
66+
num %= d
67+
if num in mp:
68+
idx = mp[num]
69+
res.insert(idx, '(')
70+
res.append(')')
71+
break
72+
return ''.join(res)
4973
```
5074

5175
### **Java**
5276

5377
```java
78+
class Solution {
79+
public String fractionToDecimal(int numerator, int denominator) {
80+
if (numerator == 0) {
81+
return "0";
82+
}
83+
StringBuilder sb = new StringBuilder();
84+
boolean neg = (numerator > 0) ^ (denominator > 0);
85+
sb.append(neg ? "-" : "");
86+
long num = Math.abs((long) numerator);
87+
long d = Math.abs((long) denominator);
88+
sb.append(num / d);
89+
num %= d;
90+
if (num == 0) {
91+
return sb.toString();
92+
}
93+
sb.append(".");
94+
Map<Long, Integer> mp = new HashMap<>();
95+
while (num != 0) {
96+
mp.put(num, sb.length());
97+
num *= 10;
98+
sb.append(num / d);
99+
num %= d;
100+
if (mp.containsKey(num)) {
101+
int idx = mp.get(num);
102+
sb.insert(idx, "(");
103+
sb.append(")");
104+
break;
105+
}
106+
}
107+
return sb.toString();
108+
}
109+
}
110+
```
111+
112+
### **C++**
113+
114+
```cpp
115+
using LL = long long;
116+
117+
class Solution {
118+
public:
119+
string fractionToDecimal(int numerator, int denominator) {
120+
if (numerator == 0) return "0";
121+
string res = "";
122+
bool neg = (numerator > 0) ^ (denominator > 0);
123+
if (neg) res += "-";
124+
LL num = abs(numerator);
125+
LL d = abs(denominator);
126+
res += to_string(num / d);
127+
num %= d;
128+
if (num == 0) return res;
129+
res += ".";
130+
unordered_map<LL, int> mp;
131+
while (num)
132+
{
133+
mp[num] = res.size();
134+
num *= 10;
135+
res += to_string(num / d);
136+
num %= d;
137+
if (mp.count(num))
138+
{
139+
int idx = mp[num];
140+
res.insert(idx, "(");
141+
res += ")";
142+
break;
143+
}
144+
}
145+
return res;
146+
}
147+
};
148+
```
54149
150+
### **Go**
151+
152+
```go
153+
func fractionToDecimal(numerator int, denominator int) string {
154+
if numerator == 0 {
155+
return "0"
156+
}
157+
res := []byte{}
158+
neg := numerator*denominator < 0
159+
if neg {
160+
res = append(res, '-')
161+
}
162+
num := abs(numerator)
163+
d := abs(denominator)
164+
res = append(res, strconv.Itoa(num/d)...)
165+
num %= d
166+
if num == 0 {
167+
return string(res)
168+
}
169+
mp := make(map[int]int)
170+
res = append(res, '.')
171+
for num != 0 {
172+
mp[num] = len(res)
173+
num *= 10
174+
res = append(res, strconv.Itoa(num/d)...)
175+
num %= d
176+
if mp[num] > 0 {
177+
idx := mp[num]
178+
res = append(res[:idx], append([]byte{'('}, res[idx:]...)...)
179+
res = append(res, ')')
180+
break
181+
}
182+
}
183+
184+
return string(res)
185+
}
186+
187+
func abs(x int) int {
188+
if x < 0 {
189+
return -x
190+
}
191+
return x
192+
}
55193
```
56194

57195
### **...**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using LL = long long;
2+
3+
class Solution {
4+
public:
5+
string fractionToDecimal(int numerator, int denominator) {
6+
if (numerator == 0) return "0";
7+
string res = "";
8+
bool neg = (numerator > 0) ^ (denominator > 0);
9+
if (neg) res += "-";
10+
LL num = abs(numerator);
11+
LL d = abs(denominator);
12+
res += to_string(num / d);
13+
num %= d;
14+
if (num == 0) return res;
15+
res += ".";
16+
unordered_map<LL, int> mp;
17+
while (num)
18+
{
19+
mp[num] = res.size();
20+
num *= 10;
21+
res += to_string(num / d);
22+
num %= d;
23+
if (mp.count(num))
24+
{
25+
int idx = mp[num];
26+
res.insert(idx, "(");
27+
res += ")";
28+
break;
29+
}
30+
}
31+
return res;
32+
}
33+
};

0 commit comments

Comments
 (0)