[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, return “0.5”.

Given numerator = 2, denominator = 1, return “2”.

Given numerator = 2, denominator = 3, return “0.(6)”.

这道题要求给定两个整数,给出两个数相除的小数形式,如果是无线循环小数,则用括号()将循环部分括起来。

这道题说难不难,但是竟然花了我一晚上的时间,总结一下原因出在一下几点:

1. 一开始没想到由分数转到无线循环小数的定义是什么。后来看到论坛里的一张图片知道了/(ㄒoㄒ)/~~太蠢了简直

2. 提示Memory Limit Exceeded,试了很多方法,以为是string的用法效率问题,后来发现又是考虑问题不!全!面!没有考虑到INT_MIN也就是涉及溢出的问题。

3.0和负数的问题也没考虑==我真是太low了

4. 有一次提交wrong answer,给的例子是1和6,才恍然大悟原来还是没真正弄清楚无限循环小数的定义啊,不是除的过程中得到的余数等于一开始的余数就结束啊==

下面贴上代码:

class Solution {
public:
   string fractionToDecimal(int numerator, int denominator) {
       if(numerator==0)
            return "0";
        string ans;
        if (numerator < 0 ^ denominator < 0)
            ans += ‘-‘;
        long long n = numerator;
        long long d = denominator;
        n = abs(n);
        d = abs(d);
        long long remainder=n%d;
        ans += to_string(n / d);
        int len = ans.size();
        if (remainder){
            unordered_map<int, int> map;
            ans += ".";
            while (remainder){
                if (map.end() != map.find(remainder)){
                    ans.insert(map[remainder], 1, ‘(‘);
                    ans += ‘)‘;
                    break;
                }
                map[remainder] = ans.size();
                remainder *= 10;
                ans += to_string(remainder / d);
                remainder = remainder%d;
            }
        }
        return ans;
    }

};

刚才发现LeetCode现在多了一个MY PROGRESS的功能,看到自己这么低的AC率,都是泪QAQ。

贴上今晚的提交结果过程,用于记住今晚所犯的错误!!

时间: 2024-10-08 16:50:21

[LeetCode]Fraction to Recurring Decimal的相关文章

[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] 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, retur

【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

【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