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, return “0.5”.
Given numerator = 2, denominator = 1, return “2”.
Given numerator = 2, denominator = 3, return “0.(6)”.
分析
由上描述,本题要求得整数相除的结果,对循环小数用括号扩之;
首先,对于除数和被除数的特殊情况需分类处理;
然后,得到整数部分;
再次,分析小数部分,若有循环小数得到正确下标增加括号;
注意:整数的溢出问题;
先将int类型保存至long long类型;
AC代码
class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
string str = "";
//除数为0,为异常情况
if (denominator == 0)
return str;
//被除数为0,结果为0
if (numerator == 0)
return "0";
//异或,numerator<0和denominator<0仅有一个为真
if (numerator < 0 ^ denominator < 0)
str += '-';
//转化为正数,INT_MIN转化为正数会溢出,故用long long;long long int n=abs(INT_MIN)得到的n仍然是负的,所以写成下面的形式
long long r = numerator; r = abs(r);
long long d = denominator; d = abs(d);
//得到整数部分并保存
str += to_string(r / d);
r = r % d;
//可以整除,直接返回
if (r == 0)
return str;
//添加小数点
str += ".";
//下面处理小数部分,用哈希表
unordered_map<int, int> map;
while (r){
//检查余数r是否在哈希表中,是的话则开始循环了
if (map.find(r) != map.end()){
str.insert(map[r], 1, '(');
str += ')';
break;
}
map[r] = str.size(); //这个余数对应于result的哪个位置
//正常运算
r *= 10;
str += to_string(r / d);
r = r % d;
}
return str;
}
//整数到字符串的转换函数
string intToStr(long long num)
{
string str = "";
if (num < 10)
{
char c = num + '0';
return str + c;
}
else
{
while (num)
{
int d = num % 10;
char c = d + '0';
str += c;
num /= 10;
}//while
reverse(str.begin(), str.end());
return str;
}//else
}
};
LeetCode(166) Fraction to Recurring Decimal的更多相关文章
- 【Leetcode 166】 Fraction to Recurring Decimal
Description: Given two integers representing the numerator and denominator of a fraction, return the ...
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- 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】166 Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 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】Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
随机推荐
- Jenkins执行yarn install报错:error An unexpected error occurred:"... ... :Unexpected end of JSON input"
解决方式: # cd /usr/local/n/versions/node/11.6.0/lib/(node的安装目录下) # rm -rf node_modules # yarn cache cle ...
- vs2012 support BI
Microsoft SQL Server Data Tools - Business Intelligence for Visual Studio 2012 http://www.microsoft. ...
- AI入门丨开源学习资源推荐
现在AI大热,网上的资源也非常多,让人眼花缭乱.非科班的我,经过半年的摸索,也算马马虎虎入了坑.下面整理了我认为不错的学习资源,大部分我都看过,以分享给更多的人.我会不断保持更新,也欢迎大家补充. P ...
- mysql索引方式
/* 所有MySQL列类型可以被索引.根据存储引擎定义每个表的最大索引数和最大索引长度. 所有存储引擎支持每个表至少16个索引,总索引长度至少为256字节.大多数存储引擎有更高的限制. 索引的存储类型 ...
- CentOS7.5 搭建mycat1.6.6
1.环境及版本 操作系统: CentOS 7.5 数据库:MySQL 5.7.23 jdk:1.8.0_191 mycat:1.6.6.1 cat /etc/centos-release mysq ...
- LCA 离线做法tarjan
tarjan(int u) { int v; for(int i=h[u];i;i=nex[i])//搜索边的 { v=to[i]; tarjan(v); marge(u,v); vis[v]=; } ...
- IO流----转换流、缓冲流
打开一个文本文件,另存为: Ansi就是系统默认编码(就是gbk) 建一个编码是utf-8的txt文件, 例: import java.io.FileWriter; import java.io.IO ...
- jQuery知识点小结
博主之前学习一段时间后做了点Demo,借此机会发出来分享,其实学jQuery只要简单看看文档即可,但有些细枝末节的东西文档会默认使用者是了解的,所以还是得系统学习系统训练:Talk is cheap, ...
- 织梦dedecms后台登陆密码充值工具
下载后解压,根据自己语言版本,把文件radminpass.php放入网站根目录下执行即可 点击下载:织梦dedecms后台登陆密码充值工具
- Nuget~管理自己的包包
很久很久以前,自己就想有个包包,最近又从网上淘了一个,价格不便宜呢,99块,还是个小腰包,不过作工还算精良,我喜欢的类型,帆布休闲包,可以将我的手机,耳机,水,小烟,小酒,小伞都放里,方便至极,哈哈!