class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
}
};
166. 分数到小数
给定两个整数,分别表示分数的分子 numerator
和分母 denominator
,以 字符串形式返回小数 。
如果小数部分为循环小数,则将循环的部分括在括号内。
如果存在多个答案,只需返回 任意一个 。
对于所有给定的输入,保证 答案字符串的长度小于 104
。
示例 1:
输入:numerator = 1, denominator = 2 输出:"0.5"
示例 2:
输入:numerator = 2, denominator = 1 输出:"2"
示例 3:
输入:numerator = 4, denominator = 333 输出:"0.(012)"
提示:
-231 <= numerator, denominator <= 231 - 1
denominator != 0
原站题解
java 解法, 执行用时: 1 ms, 内存消耗: 39.1 MB, 提交时间: 2022-12-04 14:13:28
class Solution { public String fractionToDecimal(int numerator, int denominator) { long numeratorLong = (long) numerator; long denominatorLong = (long) denominator; if (numeratorLong % denominatorLong == 0) { return String.valueOf(numeratorLong / denominatorLong); } StringBuffer sb = new StringBuffer(); if (numeratorLong < 0 ^ denominatorLong < 0) { sb.append('-'); } // 整数部分 numeratorLong = Math.abs(numeratorLong); denominatorLong = Math.abs(denominatorLong); long integerPart = numeratorLong / denominatorLong; sb.append(integerPart); sb.append('.'); // 小数部分 StringBuffer fractionPart = new StringBuffer(); Map<Long, Integer> remainderIndexMap = new HashMap<Long, Integer>(); long remainder = numeratorLong % denominatorLong; int index = 0; while (remainder != 0 && !remainderIndexMap.containsKey(remainder)) { remainderIndexMap.put(remainder, index); remainder *= 10; fractionPart.append(remainder / denominatorLong); remainder %= denominatorLong; index++; } if (remainder != 0) { // 有循环节 int insertIndex = remainderIndexMap.get(remainder); fractionPart.insert(insertIndex, '('); fractionPart.append(')'); } sb.append(fractionPart.toString()); return sb.toString(); } }
javascript 解法, 执行用时: 64 ms, 内存消耗: 41.3 MB, 提交时间: 2022-12-04 14:13:08
/** * @param {number} numerator * @param {number} denominator * @return {string} */ var fractionToDecimal = function(numerator, denominator) { if (numerator % denominator == 0) { return '' + Math.floor(numerator / denominator); } const sb = []; if (numerator < 0 ^ denominator < 0) { sb.push('-'); } // 整数部分 numerator = Math.abs(numerator); denominator = Math.abs(denominator); const integerPart = Math.floor(numerator / denominator); sb.push(integerPart); sb.push('.'); // 小数部分 const fractionPart = []; const remainderIndexDic = new Map(); let remainder = numerator % denominator; let index = 0; while (remainder !== 0 && !remainderIndexDic.has(remainder)) { remainderIndexDic.set(remainder, index); remainder *= 10; fractionPart.push(Math.floor(remainder / denominator)); remainder %= denominator; index++; } if (remainder !== 0) { // 有循环节 let insertIndex = remainderIndexDic.get(remainder); fractionPart.splice(insertIndex, 0, '('); fractionPart.push(')'); } sb.push(fractionPart.join('')); return sb.join(''); }
golang 解法, 执行用时: 0 ms, 内存消耗: 2 MB, 提交时间: 2022-12-04 14:12:53
func fractionToDecimal(numerator, denominator int) string { if numerator%denominator == 0 { return strconv.Itoa(numerator / denominator) } s := []byte{} if numerator < 0 != (denominator < 0) { s = append(s, '-') } // 整数部分 numerator = abs(numerator) denominator = abs(denominator) integerPart := numerator / denominator s = append(s, strconv.Itoa(integerPart)...) s = append(s, '.') // 小数部分 indexMap := map[int]int{} remainder := numerator % denominator for remainder != 0 && indexMap[remainder] == 0 { indexMap[remainder] = len(s) remainder *= 10 s = append(s, '0'+byte(remainder/denominator)) remainder %= denominator } if remainder > 0 { // 有循环节 insertIndex := indexMap[remainder] s = append(s[:insertIndex], append([]byte{'('}, s[insertIndex:]...)...) s = append(s, ')') } return string(s) } func abs(x int) int { if x < 0 { return -x } return x }
python3 解法, 执行用时: 44 ms, 内存消耗: 15.1 MB, 提交时间: 2022-12-04 14:12:36
class Solution: def fractionToDecimal(self, numerator: int, denominator: int) -> str: if numerator % denominator == 0: return str(numerator // denominator) s = [] if (numerator < 0) != (denominator < 0): s.append('-') # 整数部分 numerator = abs(numerator) denominator = abs(denominator) integerPart = numerator // denominator s.append(str(integerPart)) s.append('.') # 小数部分 indexMap = {} remainder = numerator % denominator while remainder and remainder not in indexMap: indexMap[remainder] = len(s) remainder *= 10 s.append(str(remainder // denominator)) remainder %= denominator if remainder: # 有循环节 insertIndex = indexMap[remainder] s.insert(insertIndex, '(') s.append(')') return ''.join(s)