王廷瑋|數位醫療|智慧醫療: 166. Fraction to Recurring Decimal WFU

2024年7月8日 星期一

166. Fraction to Recurring Decimal

166. Fraction to Recurring Decimal


給定兩個整數,分別表示分數的分子和分母,返回分數的字符串形式。

如果小數部分是循環小數,則將循環部分用括號括起來。

如果有多個答案,返回其中任意一個。

保證對於所有給定的輸入,答案字符串的長度小於 10^4。

範例 :

輸入:numerator = 1, denominator = 2
輸出:"0.5"


Python


class Solution:
def fractionToDecimal(self, numerator: int, denominator: int) -> str:
if numerator == 0:
return "0"
result = []
# Determine the sign of the result
if (numerator < 0) != (denominator < 0):
result.append("-")
# Work with absolute values to avoid sign complications
numerator, denominator = abs(numerator), abs(denominator)
# Add the integer part
integer_part = numerator // denominator
result.append(str(integer_part))
remainder = numerator % denominator
if remainder == 0:
return "".join(result)
result.append(".")
# Dictionary to store the remainder and its corresponding index in the result
remainder_map = {}
while remainder != 0:
if remainder in remainder_map:
result.insert(remainder_map[remainder], "(")
result.append(")")
break
remainder_map[remainder] = len(result)
remainder *= 10
digit = remainder // denominator
result.append(str(digit))
remainder %= denominator
return "".join(result)

16.73MB, 34ms


C++


#include <string>
#include <unordered_map>
#include <cstdlib>

using namespace std;

class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
if (numerator == 0) return "0";
string result;
// Determine the sign of the result
if (numerator < 0 ^ denominator < 0) {
result += "-";
}
// Convert to long long to avoid overflow issues
long long n = llabs(numerator);
long long d = llabs(denominator);
// Add the integer part
result += to_string(n / d);
long long remainder = n % d;
if (remainder == 0) return result;
result += ".";
// Map to store the remainder and its corresponding index in the result string
unordered_map<long long, int> remainderMap;
while (remainder != 0) {
if (remainderMap.find(remainder) != remainderMap.end()) {
result.insert(remainderMap[remainder], "(");
result += ")";
break;
}
remainderMap[remainder] = result.size();
remainder *= 10;
result += to_string(remainder / d);
remainder %= d;
}
return result;
}
};

7.7MB, 0ms


Javascript


/**
* @param {number} numerator
* @param {number} denominator
* @return {string}
*/
var fractionToDecimal = function(numerator, denominator) {
if (numerator === 0) return "0";
let result = "";
// Determine the sign of the result
if (Math.sign(numerator) !== Math.sign(denominator)) {
result += "-";
}
// Convert to absolute values to avoid sign complications
let n = Math.abs(numerator);
let d = Math.abs(denominator);
// Add the integer part
result += Math.floor(n / d);
let remainder = n % d;
if (remainder === 0) return result;
result += ".";
// Map to store the remainder and its corresponding index in the result string
let remainderMap = new Map();
while (remainder !== 0) {
if (remainderMap.has(remainder)) {
result = result.slice(0, remainderMap.get(remainder)) + "(" + result.slice(remainderMap.get(remainder)) + ")";
break;
}
remainderMap.set(remainder, result.length);
remainder *= 10;
result += Math.floor(remainder / d);
remainder %= d;
}
return result;
};

48.82MB, 47ms