LeetCode – Refresh – Fraction to Recurring Decimal

Notes:

1. When numerator is 0, return "0". Check this corner case, because 0 / -5 will return -0.

2. Use long long int for divd and divs, mainly for divs. Because when divs = INT_MIN, fabs(divs) is large.

3. Do not forget to rest *= 10 to get the next level.

 1 class Solution {
 2 public:
 3     string fractionToDecimal(int numerator, int denominator) {
 4         if (numerator == 0) return "0";
 5         string result;
 6         if (numerator < 0 ^ denominator < 0) result = "-";
 7         long long int divd = fabs(numerator), divs = fabs(denominator), rest = divd % divs;
 8         result += to_string(divd / divs);
 9         if (rest == 0) {
10             return result;
11         }
12         result += ‘.‘;
13         unordered_map<int, int> mapping;
14         while (rest > 0) {
15             if (mapping.find(rest) != mapping.end()) {
16                 result.insert(mapping[rest], "(");
17                 result += ‘)‘;
18                 return result;
19             }
20             mapping[rest] = result.size();
21             rest *= 10;
22             result += to_string(rest/divs);
23             rest %= divs;
24         }
25         return result;
26     }
27 };
时间: 2024-10-21 20:48:15

LeetCode – Refresh – Fraction to Recurring Decimal的相关文章

【LeetCode】Fraction to Recurring Decimal

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. For example, Given numera

[LeetCode#116]Fraction to Recurring Decimal

Problem: 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. For example, Given numerator = 1, denominator

【LeetCode】Fraction to Recurring Decimal【Solution】

[题目] 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. For example, Given numerator = 1, denominator = 2,

[leetcode] 166. Fraction to Recurring Decimal 解题报告

题目链接: https://leetcode.com/problems/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 p

Java for LeetCode 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. For example, Given numerator = 1, denominator = 2, retu

[LeetCode]58. 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. For example, Given numerator = 1, denominator = 2, retu

LeetCode (29) 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. For example, Given numerator = 1, denominator = 2,

[LeetCode] 167. 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

leetcode[166] Fraction to Recurring Decimal

For example, Given numerator = 1, denominator = 2, return "0.5". Given numerator = 2, denominator = 1, return "2". Given numerator = 2, denominator = 3, return "0.(6)". 按照上例子实现结果.主要就是如何处理循环的数字. 我们发现如果循环了,那么余数也是出现了循环,所以从余数入手,一