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)".
Hint:
No scary math, just apply elementary math knowledge. Still remember how to perform a long division?
这题就是按定义做。
如果不能整除,就不断进行余数补零除以除数。
维护一个映射表map<long long, int> m, 用来记录每个余数对应返回值ret中的位置。
(1)当出现重复的余数r时,说明找到了循环体,根据m[r]找到ret中位置,加上相应的'('和')'将循环体括起来即可返回。
(2)当余数r为0时,返回ret。
注意点:可能出现INT_MIN/-1的越界情况,因此第一步现将int转为long long int
class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
if(denominator==)
return "";
if(numerator==)
return "";
string res;
long long num=numerator;
long long den=denominator;
if((num<)^(den<))
{
res+="-";
num=abs(num);
den=abs(den);
}
long long temp=num/den;
res+=to_string(temp);
if (num%den==)
return res;
res+=".";
temp=num%den;
map<long long,int> m;
while(temp!=)
{
if(m.find(temp)!=m.end())
{
res.insert(m[temp], , '('); //insert (iterator p, size_t n, char c);
res += ')';
return res;
}
m[temp] = res.size();
temp*=;
res+=to_string(temp/den);
temp%=den;
}
return res;
}
};
Fraction to Recurring Decimal——数值处理&&哈希表的更多相关文章
- 【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解题报告—— 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 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- 【刷题-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 分数转循环小数
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- LeetCode(166) Fraction to Recurring Decimal
题目 Given two integers representing the numerator and denominator of a fraction, return the fraction ...
随机推荐
- 删边(cip)
删边(cip) 给出一个没有重边和自环的无向图,现在要求删除其中两条边,使得图仍然保持连通. 你的任务是计算有多少组不合法的选边方案.注意方案是无序二元组. Sol 神题,无从下手啊. 考虑点dfs建 ...
- git使用笔记(十一)rebase
By francis_hao Oct 22,2017 git-rebase,改变commit的基础参照 概要 git rebase [-i | --interactive] [options ...
- The database cluster was initialized with RELSEG_SIZE 1048576, but the server was compiled with RELSEG_SIZE 8388608
由于一次误操作,将线上机器的数据库程序目录删除,虽然不影响程序的正常使用,数据也未丢失,但后面如果出现服务器宕机或数据库宕机,数据库将无法启动,而且数据库对应的编译参数也已无法查看,所以征得开发同意后 ...
- lightoj 1341
lightoj 1341 Aladdin and the Flying Carpet 链接:http://lightoj.com/volume_showproblem.php?problem=134 ...
- CMDB资产管理系统开发【day27】:cmdb API安全认证
1.API验证分析 API三关验证 客户端和服务端中都存放一份相同的随机字符串,客户端发请求的时候把随机字符串和当前时间进行MD5加密,同时带着当前时间通过请求头发送到API,进入三关验证. 第一关是 ...
- Azure Pipelines
https://docs.microsoft.com/en-us/azure/devops/pipelines/?view=vsts
- [技巧篇]12.从Spring的编码过滤器说起
有一枚学生问问了我一个问题,突然灵感爆发,他使用的Spring的过滤器,前台利用GET方式向后端发出一个请求,由于里面含有中文数据,结果在后端显示的是乱码,他问我为什么?明明在Spring里面也配了字 ...
- 杭电多校第八场-A-Character Encoding
题目描述 In computer science, a character is a letter, a digit, a punctuation mark or some other similar ...
- NOIP2006 数列
codevs 1141 数列 http://codevs.cn/problem/1141/ 2006年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目描述 ...
- mysql 索引 和mysql 的引擎
1.索引的特点 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的引用指针.更通俗的说,数据库索引好比是一本书前面的目录,能加快数据库的查询速度. ...