166. Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.
If the fractional part is repeating, enclose the repeating part in parentheses.
Example 1:
Input: numerator = 1, denominator = 2 Output: "0.5"
Example 2:
Input: numerator = 2, denominator = 1 Output: "2"
Example 3:
Input: numerator = 2, denominator = 3 Output: "0.(6)"
Approach #1: C++
class Solution {
public:
typedef long long LL;
string fractionToDecimal(int numerator, int denominator) {
if (!numerator) return "0";
string res;
if (numerator < 0 ^ denominator < 0) res += '-';
LL numer = numerator < 0 ? (LL)numerator * (-1) : (LL)numerator;
LL denom = denominator < 0 ? (LL)denominator * (-1) : (LL)denominator;
LL quotient = numer / denom;
LL remainder = numer % denom;
res += to_string(quotient);
if (!remainder) return res;
res += '.';
remainder *= 10;
cout << remainder << endl;
unordered_map<LL, LL> mp;
while (remainder) {
quotient = remainder / denom;
if (mp.find(remainder) != mp.end()) {
res.insert(mp[remainder], 1, '(');
res += ')';
break;
}
mp[remainder] = res.size();
res += to_string(quotient);
remainder = (remainder % denom) * 10;
}
return res;
}
};
Approach #2: Java
class Solution {
public String fractionToDecimal(int numerator, int denominator) {
StringBuilder result = new StringBuilder();
String sign = (numerator < 0 == denominator < 0 || numerator == 0) ? "" : "-";
long num = Math.abs((long) numerator);
long den = Math.abs((long) denominator);
result.append(sign);
result.append(num / den);
long remainder = num % den;
if (remainder == 0)
return result.toString();
result.append(".");
HashMap<Long, Integer> hashMap = new HashMap<Long, Integer>();
while (!hashMap.containsKey(remainder)) {
hashMap.put(remainder, result.length());
result.append(10 * remainder / den);
remainder = 10 * remainder % den;
}
int index = hashMap.get(remainder);
result.insert(index, "(");
result.append(")");
return result.toString().replace("(0)", "");
}
};
Approach #3: Python
class Solution(object):
def fractionToDecimal(self, numerator, denominator):
"""
:type numerator: int
:type denominator: int
:rtype: str
"""
res = ""
if numerator/denominator < 0:
res += '-'
if numerator%denominator == 0:
return str(numerator/denominator)
numerator = abs(numerator)
denominator = abs(denominator)
res += str(numerator/denominator)
res += '.'
numerator %= denominator
i = len(res)
table = {}
while numerator != 0:
if numerator not in table.keys():
table[numerator] = i
else:
i = table[numerator]
res = res[:i] + "(" + res[i:] + ")"
return res
numerator = numerator * 10
res += str(numerator/denominator)
numerator %= denominator
i += 1
return res
In this case if we use Python3 it will report error.
| Time Submitted | Status | Runtime | Language |
|---|---|---|---|
| a few seconds ago | Accepted | 32 ms | python |
| 11 minutes ago | Accepted | 6 ms | java |
| 23 minutes ago | Accepted | 0 ms | cpp |
永远渴望,大智若愚(stay hungry, stay foolish)

浙公网安备 33010602011771号