leetcode166. Fraction to Recurring Decimal

题意:给出分子和分母,用字符串表示结果,循环的部分用“()”括起来。

思路:

  模拟笔算,

  最初根据1/99的情况,设想用path记录余数相同时的间隔步长,但只能用于处理(00..0n)的情况,其中n为[1,9];

  后来采取网上看到用map记录<余数,出现位置>的想法。

  通过判断map中key是否重复来判断是否循环,通过key对应的出现位置,来判断循环位数。

  列表记录商。

注意:

  被除数为0的情况;

  被除数为min_int的情况;(用long处理)

  结果为负数的情况;

  只有正数的情况。

代码:

 1 class Solution {
 2 public:
 3     string fractionToDecimal(int numerator, int denominator) {
 4         long bei = numerator;
 5         long chu = denominator;
 6         int fuflag = 0;
 7         if(chu<0)
 8         {
 9             chu *= -1;
10             bei *= -1;
11         }
12         if(bei<0)
13         {
14             fuflag = 1;
15             bei *= -1;
16         }
17         if(bei==0)
18             return "0";
19         map<long,long> mymap;
20         vector<long> shangvec;
21         long yup = 0;
22         long firstflag = 1;
23         while(bei)
24         {
25             if(firstflag == 0)
26                 bei *= 10;
27             firstflag = 0;
28             long tmp = bei / chu;
29             long yu = bei - tmp * chu;
30             shangvec.push_back(tmp);
31             map<long,long>::iterator it;
32             it = mymap.find(yu);
33             if(it!=mymap.end())
34             {
35                 long xunp = it->second;
36                 string ans = "";
37                 if(fuflag)
38                     ans += "-";
39                 ostringstream s1;
40                 s1<<shangvec[0];
41                 ans += s1.str();
42                 s1.str("");
43                 ans += ".";
44                 for(long i=1;i<=xunp;i++)
45                 {
46                     s1<<shangvec[i];
47                     ans += s1.str();
48                     s1.str("");
49                 }
50                 ans += "(";
51                 for(long i=xunp+1;i<shangvec.size();i++)
52                 {
53                     s1<<shangvec[i];
54                     ans += s1.str();
55                     s1.str("");
56                 }
57                 ans += ")";
58                 return ans;
59             }
60             mymap[yu] = yup++;
61             bei = yu;
62         }
63         string ans = "";
64         if(fuflag)
65             ans += "-";
66         ostringstream s1;
67         s1<<shangvec[0];
68         ans += s1.str();
69         s1.str("");
70         if(shangvec.size()==1)
71             return ans;
72
73         ans += ".";
74         for(int i=1;i<shangvec.size();i++)
75         {
76             s1<<shangvec[i];
77             ans += s1.str();
78             s1.str("");
79         }
80         return ans;
81     }
82 };

时间: 2024-10-10 06:51:49

leetcode166. 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

[Swift]LeetCode166. 分数到小数 | 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#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

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

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