LeetCode-166 Fraction to recurring Decimal

Source: Internet
Author: User

Given integers representing the numerator and denominator of a fraction, return the fraction in string format.

If the fractional part was 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:
Special thanks to @Shangrila for adding this problem and creating all test cases.

Test instructions: Converting fractions to decimals

Idea: Using HashMap to deposit already calculated molecules, if a new molecule exists in the map, there is a loop, so use the molecule as the map key. But there was confusion about how the value of map would be determined.

1. Divide the numerator by the integer value of the denominator as value, and then, if a loop occurs, just look at what the value of the loop starts, and find the corresponding position to insert (), but the problem occurs in the 1/90 case;

2. When you see other people's code with different molecules, the corresponding result length as value, this can avoid the problem in 1, so the error value in this way.

Case that might hang:

0/11

1/90;

1/2147483647 (Integer.max_value);

-1/2147483647;

1/-2147483638;

The code is as follows:

 PublicString Fractiontodecimal (intA1,intA2) {        if(A1 = = 0 | | a2 = = 0)return"0"; StringBuilder SB=NewStringBuilder (); Sb.append ((A1>0) ^ (a2>0)? "-":""); LongA = Math.Abs ((Long) A1); Longb = Math.Abs ((Long) A2); Sb.append (Math.Abs (a/b)); A= (a% b) * 10; if(A! = 0) Sb.append ("."); Map<long, integer> map =NewHashmap<long, integer>(); Map.put (NewLong (A1), sb.length ());  while(A! = 0) {Sb.append (a/b);                        Map.put (A, sb.length ()); A= (a% b) * 10; if(Map.containskey (a)) {intpos =Map.get (a); Sb.insert (POS-1, "("); Sb.append (")");  Break; }        }        returnsb.tostring (); }

In the preceding code, the reason for converting the input to a long type is:

1. There is *10 calculation, with an int may overflow;

2. If the integer.min_value is converted to a positive number, the int type will overflow;

LeetCode-166 Fraction to recurring Decimal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.