QUESTION

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)".

1ST TRY

class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
char* ch = new char();
string ret;
string fractionStr = "";
int remain;
vector<bool> flag(, false); //integer part
*ch = numerator/denominator +'';
ret = ch; //fraction part
while()
{
remain = numerator%denominator;
if(remain == ) return ret;
else if(flag[remain]) return ret + ".(" + fractionStr + ")";
else
{
flag[remain] = true;
*ch = numerator/denominator +'';
fractionStr += ch;
}
}
}
};

Result: Runtime Error

Last executed input: -50, 8

2ND TRY

考虑了负数情况

class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
string intStr = "";
string fractionStr = "";
string tmpStr = "";
int remain;
vector<bool> flag(, false); // int 转 string
stringstream ss; //negative or not
if(numerator > && denominator < )
{
intStr = "-";
denominator = ~(denominator-);
}
else if(numerator < && denominator > )
{
intStr = "-";
numerator = ~(numerator-);
}
else if(numerator < && denominator < )
{
numerator = ~(numerator-);
denominator = ~(denominator-);
} //integer part
ss << numerator/denominator;
ss >> tmpStr;
intStr += tmpStr;
remain = numerator%denominator;
if(remain == ) return intStr; //fraction part
while()
{
if(remain == ) return intStr + "." + fractionStr;
else if(flag[remain]) return intStr + ".(" + fractionStr + ")";
else
{
flag[remain] = true;
numerator = remain * ;
remain = numerator%denominator;
ss.clear();
ss << numerator/denominator;
ss >> tmpStr;
fractionStr += tmpStr;
}
}
}
};

Result: Runtime Error

Last executed input: -2147483648, -10

3RD TRY

考虑溢出的情况

class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
long long int num = (long long int) numerator;
long long int den = (long long int) denominator; string intStr = "";
string fractionStr = "";
string tmpStr = "";
int remain;
set<int> flag; // int 转 string
stringstream ss; //negative or not
if(num > && denominator < )
{
intStr = "-";
den = ~(den-);
}
else if(num < && den > )
{
intStr = "-";
num = ~(num-);
}
else if(num < && den < )
{
num = ~(num-);
den = ~(den-);
} //integer part
ss << num/den;
ss >> tmpStr;
intStr += tmpStr;
remain = num%den;
if(remain == ) return intStr; //fraction part
while()
{
if(remain == ) return intStr + "." + fractionStr;
else if(flag.find(remain)!=flag.end()) return intStr + ".(" + fractionStr + ")";
else
{
flag.insert(remain);
num = remain * ;
remain = num%den;
ss.clear();
ss << num/den;
ss >> tmpStr;
fractionStr += tmpStr;
}
}
}
};

Result: Wrong

Input: 1, 6
Output: "0.(16)"
Expected: "0.1(6)"

4TH TRY

循环的位置得准确,所以用map代替set

class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
long long int num = (long long int) numerator;
long long int den = (long long int) denominator; string ret = "";
string tmpStr = "";
int remain;
map<int,int> flag; // int 转 string
stringstream ss; //negative or not
if(num > && denominator < )
{
ret = "-";
den = ~(den-);
}
else if(num < && den > )
{
ret = "-";
num = ~(num-);
}
else if(num < && den < )
{
num = ~(num-);
den = ~(den-);
} //integer part
ss << num/den;
ss >> tmpStr;
ret += tmpStr;
remain = num%den;
if(remain == ) return ret; //fraction part
ret += ".";
while()
{
if(remain == ) return ret;
else if(flag.find(remain)!=flag.end())
{
ret.insert(flag[remain], , '(');
return ret + ")";
}
else
{
flag[remain] = ret.length();
num = remain * ;
remain = num%den;
ss.clear();
ss << num/den;
ss >> tmpStr;
ret += tmpStr;
}
}
}
};

Result: Wrong

Input: -1, -2147483648
Output: "0.000000000000000000000000000000-1"
Expected: "0.0000000004656612873077392578125"

5TH TRY

remain也要申请为long long int, 否则在num = remain * 10;会溢出

class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
long long int num = (long long int) numerator;
long long int den = (long long int) denominator; string ret = "";
string tmpStr = "";
long long int remain;
map<int,int> flag; // int 转 string
stringstream ss; //negative or not
if(num > && denominator < )
{
ret = "-";
den = ~(den-);
}
else if(num < && den > )
{
ret = "-";
num = ~(num-);
}
else if(num < && den < )
{
num = ~(num-);
den = ~(den-);
} //integer part
ss << num/den;
ss >> tmpStr;
ret += tmpStr;
remain = num%den;
if(remain == ) return ret; //fraction part
ret += ".";
while()
{
if(remain == ) return ret;
else if(flag.find(remain)!=flag.end())
{
ret.insert(flag[remain], , '(');
return ret + ")";
}
else
{
flag[remain] = ret.length();
num = remain * ;
remain = num%den;
ss.clear();
ss << num/den;
ss >> tmpStr;
ret += tmpStr;
}
}
}
};

Result: Accepted

Fraction to Recurring Decimal(STRING-TYPE CONVERTION)的更多相关文章

  1. LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal

    1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...

  2. 【LeetCode】166. Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

  3. 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)

    [LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...

  4. 【leetcode】Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

  5. Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环

    分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...

  6. 【刷题-LeetCode】166 Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

  7. LeetCode Fraction to Recurring Decimal

    原题链接在这里:https://leetcode.com/problems/fraction-to-recurring-decimal/ 题目: Given two integers represen ...

  8. 166. Fraction to Recurring Decimal -- 将除法的商表示成字符串(循环节用括号表示)

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

  9. [LeetCode#116]Fraction to Recurring Decimal

    Problem: Given two integers representing the numerator and denominator of a fraction, return the fra ...

随机推荐

  1. liunx poi excel下载内容乱码本地tomcat正常

    结论:在jsp中加上out.clear即可(前提保证生成的excel在服务器上是正确的,只是浏览器传输才出现乱码). dowload.jsp完整代码 <%@ page language=&quo ...

  2. Java 判断当前系统为Window或者Linux

    public static boolean isOSLinux() {         Properties prop = System.getProperties();         String ...

  3. jsfl 将库中声音放置到时间轴上

    setBrokenFrames("cz1.mp3",2); /**/ /*<--------------将库中声音放置到时间轴上------------->*/ /** ...

  4. call 和 apply

    call和apply作用一样,都是为了转移this,区别在于传入参数的方式不同. this指当前方法所在的对象,如果方法的外面没有对象,则默认是window.由于闭包虽在调用的方法中,但是在创建的时候 ...

  5. 如何做Go的性能优化?(转)

    Go的性能优化其实总的来说和C/C++等这些都差不多,但也有它自己独有的排查方法和陷阱,这些都来源于它的语言特性和环境. 1.性能优化前提——任何好的东西都是在正确的前提上 代码界的很多事是和我们生活 ...

  6. 一个Tparams小测试

    var aParams: TParams; aPar: TParam; I:Integer; begin aParams := TParams.Create(nil); aPar := aParams ...

  7. Servlet开发

    源地址:http://www.cnblogs.com/xdp-gacl/p/3760336.html 一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun ...

  8. ReactiveX 学习笔记(5)合并数据流

    Combining Observables 本文的主题为合并 Observable 的操作符. 这里的 Observable 实质上是可观察的数据流. RxJava操作符(四)Combining An ...

  9. shell流程控制与循环结构

    shell脚本中的流程控制有if/else语句.case语句,循环结果包括for循环.while循环.until循环等内容. if语句 (1)最简单的if语句.使用格式有2种方式,分别如下 使用格式1 ...

  10. C++多态,虚函数,虚函数表,纯虚函数

    1.多态性   指相同对象收到不同消息或不同对象收到相同消息时产生不同的实现动作. C++支持两种多态性:编译时多态性,运行时多态性.    a.编译时多态性:通过重载函数实现 ,模板(2次编译)  ...