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 ...
随机推荐
- 关于sql链接超时的问题
也许你会说,我在连接字符串中已经 设置了 Connect Timeout=80000 ,并且数据库中超时连接也是设置的值是一个很大的值.为啥到了30秒,仍然超时了呢?? 这是因为: ...
- 使用spring validation完成数据后端校验
前言 数据的校验是交互式网站一个不可或缺的功能,前端的js校验可以涵盖大部分的校验职责,如用户名唯一性,生日格式,邮箱格式校验等等常用的校验.但是为了避免用户绕过浏览器,使用http工具直接向后端请求 ...
- linux配置sphinx
1. 配置索引 cd /usr/local/sphinx/etc/ cp sphinx.conf.dist sphinx.conf //备份配置文件,防止改错 vim sphinx.conf 配置文件 ...
- WPF线性渐变画刷应用之——炫彩线条
效果图: Xaml代码: <Rectangle Width="800" Height="10"> <Rectangle.Fill> &l ...
- android除去标题栏或全屏
想要除去标题栏只要加上下面两句代码在Activity的onCreate方法中即可(要在setContentView之前添加). requestWindowFeature(Window.FEATURE_ ...
- BerOS File Suggestion(stl-map应用)
Polycarp is working on a new operating system called BerOS. He asks you to help with implementation ...
- Android忽略文件
转自我自己的博客...Android Studio上传项目到GitHub
- 多线程 死锁 wait(int i) notifyAll()
public class ThreadDemo5 { public static void main(String[] args){ Pool pool = new Pool(); Productor ...
- Hibernate 再接触 树状结构设计以及学生课程成绩表的设计
1 树状结构的设计 package com.bjsxt.hibernate; import java.util.HashSet; import java.util.Set; import javax. ...
- ArcGIS案例学习笔记3_1_ArcMap编辑练习
ArcGIS案例学习笔记3_1_ArcMap编辑练习 计划时间:第三天上午 目的:ArcMap编辑练习 教程: pdf page67 数据: gis_ex10/ex07 方法: 1.新建shp文件 目 ...