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

Analysis:

Fristly, you man think it‘s a difficult problem. But when you clam down, you may notice it‘s not a hard problem, since you have already familar with its underlying algorithm. What you need to do is to write a good code to solve the problem elegantly!

Idea:
The reason why we have decimal part is the numerator could not be fully divided by denominator. We could divide the process into following part:
1. get quotient part.
long quotient = dividend / divisor;
res += quotient;

2. get decimal part.
Recall what we have done through mannually way.
            1 ....
     9 |-----------
         1  0
            9
        ------------
            1   0

We need to get remainder, and if the remainder is not equal to 0, we multiply the remainder with 10. And continue the process.
while (remainder != 0) {
    ...
    map.put(remainder, res.length());
    long de = remainder / divisor;
    res += String.valueOf(de);
    remainder = (remainder % divisor) * 10;
}

However, the problem asks us to circle out the repeative part, if the decialmal part is infinite.
How could we detect the circle part out? Use our old friend: HashSet, HashMap.
And we know if a remainder appear gain, it must be the sign of a circle!!!
1. HashSet
We indeed can use it to detect a circle, however, it has no memory of where the circle starts.

2. HashMap
We certainly can use it to detect circle, what‘s more, we can record the next index of each remainder!!! Thus when we reach a circle, we can easily point the circle out.
------------------------------------------------------------------
HashMap<Long, Integer> map = new HashMap<Long, Integer> ();
while (remainder != 0) {
    if (map.containsKey(remainder)) {
        int beg = map.get(remainder);
        res = res.substring(0, beg) + "(" + res.substring(beg, res.length()) + ")";
        return res;
    }
    map.put(remainder, res.length());
    long de = remainder / divisor;
    res += String.valueOf(de);
    remainder = (remainder % divisor) * 10;
}

2.1 record the start(next index) of each remainder
map.put(remainder, res.length()); 

2.2 get the circular part out.
res = res.substring(0, beg) + "(" + res.substring(beg, res.length()) + ")";

Mistake:
Directly use Math.abs over Integer.
Note: when Integer = Math.MIN_VALUE,
Math.abs(Math.MIN_VALUE) has no effect!!! Otherwise, it could cause overflow!!!
Fix: store the integer into a long, then use abs over the long.
long dividend = Math.abs(numerator);
long divisor = Math.abs(denominator);

Solution:

public class Solution {
    public String fractionToDecimal(int numerator, int denominator) {
        if (numerator == 0)
            return "0";
        if (denominator == 0)
            return "";
        String res = "";
        if ((numerator < 0) ^ (denominator < 0))
            res += ‘-‘;
        long dividend = numerator;
        long divisor = denominator;
        dividend = Math.abs(dividend);
        divisor = Math.abs(divisor);
        long quotient = dividend / divisor;
        res += quotient;
        long remainder = (dividend % divisor) * 10;
        if (remainder == 0)
            return res;
        res += ‘.‘;
        HashMap<Long, Integer> map = new HashMap<Long, Integer> ();
        while (remainder != 0) {
            if (map.containsKey(remainder)) {
                int beg = map.get(remainder);
                res = res.substring(0, beg) + "(" + res.substring(beg, res.length()) + ")";
                return res;
            }
            map.put(remainder, res.length());
            long de = remainder / divisor;
            res += String.valueOf(de);
            remainder = (remainder % divisor) * 10;
        }
        return res;
    }
}
时间: 2024-12-20 20:03:56

[LeetCode#116]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】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 – 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 th

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)". 按照上例子实现结果.主要就是如何处理循环的数字. 我们发现如果循环了,那么余数也是出现了循环,所以从余数入手,一