Fraction to Recurring Decimal -- leetcode

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, return "0.5".
  • Given numerator = 2, denominator = 1, return "2".
  • Given numerator = 2, denominator = 3, return "0.(6)".

Credits:

基本思路:

在计算小数部分时,

需要考虑的是,余数在啥时候开始重复。

用一个map,映射余数和位置。

如果一个余数已经在 map中存着了,那说明开始重复了。

而重复的起始位置,可通过map查知。在该位置处插入 ‘("标记即可。

要注意的事,整数int在转换成其绝对值时,INT_MIN的绝对值,在int范围内将溢出,需要用到64位来存储

class Solution {
public:
    string fractionToDecimal(int numerator, int denominator) {
        if (!numerator)
            return "0";

        string ans;
        if (numerator<0 ^ denominator<0)
            ans += '-';

        uint64_t n = abs((int64_t)numerator);
        uint64_t d = abs((int64_t)denominator);

        ans += to_string(n/d);
        uint64_t r = n % d;
        if (!r)
            return ans;

        ans += '.';
        unordered_map<int, int> pos;
        while (r) {
            if (!pos.insert(make_pair((int)r, ans.size())).second) {
                ans.insert(pos[r], 1, '(');
                ans += ')';
                break;
            }
            r *= 10;
            ans += to_string(r/d);
            r %= d;
        }

        return ans;
    }
};
时间: 2024-08-28 16:40:18

Fraction to Recurring Decimal -- leetcode的相关文章

【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]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#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

【Leetcode 166】 Fraction to Recurring Decimal

Description: 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, denomina

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] 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