Fraction to Recurring Decimal(STRING-TYPE CONVERTION)
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)的更多相关文章
- 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 ...
- 【LeetCode】166. Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- 【leetcode】Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...
- 【刷题-LeetCode】166 Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- LeetCode Fraction to Recurring Decimal
原题链接在这里:https://leetcode.com/problems/fraction-to-recurring-decimal/ 题目: Given two integers represen ...
- 166. Fraction to Recurring Decimal -- 将除法的商表示成字符串(循环节用括号表示)
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- [LeetCode#116]Fraction to Recurring Decimal
Problem: Given two integers representing the numerator and denominator of a fraction, return the fra ...
随机推荐
- Linux 设置IP地址,并能连接外网
1,如果是 centos6,请修改 vi /etc/sysconfig/network-scripts/ifcfg-eth0 2,如果是 centos7,请修改 => vi /etc/sysc ...
- MVC基于角色权限控制--菜单展示
在用户成功登陆后台页面后,我们需要将当前用户拥有的权限通过菜单的形式展现出来,将未具备的权限隐藏 新建一个HomeController,用于展示后台首页和获取用户权限数据 namespace CZBK ...
- SPring cloud (3)A Ribbon 负载均衡 配置初步
1.引用pom <dependency> <groupId>org.springframework.cloud</groupId> <artifactId&g ...
- golang:send mail using smtp package
go语言发送邮件,可以使用smtp包,两个关键函数: func PlainAuth(identity, username, password, host string) Auth func SendM ...
- linux下通过sed命令直接修改文件内容
sed是实现对流的编辑.通常,我们使用sed可以实现内容的编辑后然后保存成另外的一个文件,如果正确的话,才写入到源文件.但是某些时候,我们需要直接修改文件,因为,保存文件到一个文件,然后再覆盖原文件的 ...
- leetcode题解 Generate Parentheses
原文链接:https://leetcode.com/problems/generate-parentheses 给出数字n,求n对括号组成的合法字符串. 刚做出来,敲完代码,修改了一次,然后提交,ac ...
- jquery下插入标签以及clone的应用
//内部插入 插入一个儿子 //var $ele = $("<h1></h1>")//创建h1标签 // $ele.html('hello') // $el ...
- homebrew, carthage以及redis的安装和启动
homebrew的介绍以及redis的安装 brew install redis https://www.cnblogs.com/xd502djj/p/6923690.html redis的启动, ...
- HashSet和LinkedHashSet解析
一.简介 1.Set概念 Set可以理解为集合,非常类似数据概念中的集合,集合三大特征:1.确定性:2.互异性:3.无序性,因此Set实现类也有类似的特征. 2.HashSet HashSet继承自A ...
- Stephen Hawking Taught Us a Lot About How to Live
勇气.好奇心.幽默感,那些霍金教给我们的事Stephen Hawking Taught Us a Lot About How to LiveStephen Hawking, the English c ...