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. [记录] Mysql 复制表格结构

    有时候我们需要原封不动的复制一张表的表结构来生成一张新表,MYSQL提供了两种便捷的方法 一.LIKE方法 like方法能一模一样的将一个表的结果复制生成一个新表,包括复制表的备注.索引.主键外键.存 ...

  2. JS call和apply方法使用

    总是对call和apply方法使用存在迷惑,特此记录一下 一句话理解这两个方法: call和apply是为了动态改变this而出现的,当一个object没有某个方法,但是其他的有,我们可以借助call ...

  3. react-native获取设备信息app版本信息,react-native-device-info

    安装 yarn add react-native-device-info react-native link react-native-device-info link 之后就可以直接使用了,ios ...

  4. delphi RTTI 三 属性读取与赋值

    delphi RTTI 读取属性 运行期 通过RTTI给属性赋值 http://docwiki.embarcadero.com/CodeExamples/Berlin/en/Event_RTTI_In ...

  5. curl发送xml , xml和数组互转

    public function postXml($url, array $data) { // pack xml $xml = $this->arrayToXml($data); // curl ...

  6. 虚拟机扩容mac

    VMware虚拟机Mac增大容量: 1.设置硬盘容量大小 2.打开虚拟机的终端,找到需要扩展的硬盘.输入命令 :diskutil list 注意 :我的硬盘名字叫yz,这一行可以看见当前分配容量,最后 ...

  7. 【Noip模拟 20161004】局域网

    问题描述 所有SZSZ 学生翘首以盼的新教学楼总算快要竣工了,接下来到了网络布线的时候.网络系统的总布局是由nn台计算机组成的有线局域网,每根网线长度为dd,正常情况下,网线是可以缠绕使其变短但是不能 ...

  8. EMQ笔记

    飞行窗口(Inflight Window)保存当前正在发送未确认的Qos1/2消息.窗口值越大,吞吐越高:窗口值越小,消息顺序越严格. 当客户端离线或者飞行窗口(Inflight Window)满时, ...

  9. docker仓库harbor搭建随笔

    docker除了自己的registry仓库工具外,还有vmware出品的harbor,harbor集成了ui界面,用户级别认证,重要的是对镜像管理比较全面,可以删除镜像,下面是 简单的部署指南 首先: ...

  10. LISTVIEW显示JPEG缩略图

    http://www.ctsys.cn/files/SHOW_FILES.ASPX?ID=22 许多的JPEG图片浏览器(如由我设计的<JPEG浏览缩放器>),都可以将JPEG缩略图放置到 ...