【leetcode】Fraction to Recurring Decimal
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)".
0.16
6 ) 1.00
0
1 0 <-- Remainder=1, mark 1 as seen at position=0.
- 6
40 <-- Remainder=4, mark 4 as seen at position=1.
- 36
4 <-- Remainder=4 was seen before at position=1, so the fractional part which is 16 starts repeating at position=1 => 1(6).
class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
string ans="";
if(numerator==) return "";
long long int n=numerator;
long long int d=denominator;
n=abs(n);
d=abs(d);
if(numerator<^denominator<) ans.push_back('-');
map<long long int,int> hash;
ans+=to_string(n/d);
long long int rem=n%d;
if(rem!=) ans.push_back('.');
while(rem!=)
{
if(hash.find(rem)!=hash.end())
{
ans.insert(hash[rem],"(");
ans.push_back(')');
break;
}
hash[rem]=ans.length();
ans+=to_string(rem*/d);
rem=(rem*)%d;
}
return ans;
}
};
【leetcode】Fraction to Recurring Decimal的更多相关文章
- Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...
- 【Leetcode 166】 Fraction to Recurring Decimal
Description: Given two integers representing the numerator and denominator of a fraction, return the ...
- [LeetCode#116]Fraction to Recurring Decimal
Problem: Given two integers representing the numerator and denominator of a fraction, return the fra ...
- [LeetCode] 167. 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 分数转换 --------- java
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Java for LeetCode 166 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
原题地址 计算循环小数 先把负数转化成正数,然后计算,最后添加符号 当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数 需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧 ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
随机推荐
- MySQL 视图的基础操作(五)
1.为什么使用视图: 为了提高复杂SQL语句的复用性和表操作的安全性(例如:工资字段不想展示给所有能查看该查询结果的人),MySQL提供了视图特性.所谓视图,本质上是一种虚拟表,其内容与真实的 ...
- Saltstack pillar组件
pillar组件 pillar也是Saltstack最重要的组件之一,其作用是定义与被控主机相关的任何数据,定义好的数据可以被其他组件使用,如模板.state.API等.在pillar中定义的数据与 ...
- motto2
Baby you've done enough that cut your breath.Don't beat yourself up don't need to turn so fast.Somet ...
- vim关于 引号和 括号的 高效操作-很好很强大的!
http://blog.csdn.net/bigshady/article/details/6019963 对括号匹配, 进行跳转, 使用的是%. 匹配的括号, 都会被高亮显示, 但是: 根据光标的 ...
- hdu4982 Goffi and Squary Partition (DFS解法)
BestCoder Round #6 B http://acm.hdu.edu.cn/showproblem.php?pid=4982 Goffi and Squary Partition Time ...
- php分页代码简单实现
版权声明:本文为博主原创文章,未经博主允许不得转载. 数据库操作类代码:mysqli.func.php <?php // 数据库连接常量 define('DB_HOST', 'localhost ...
- SQL Server数据库邮件配置
一.数据库邮件介绍 数据库邮件是从SQL Server数据库引擎中发送电子邮件的企业解决方案,通过使用数据库邮件,数据库应用程序可以向用户发送电子邮件.邮件中可以包含查询结果,还可以包含来自网络中任何 ...
- Handler Should be static or leaks Occur?
解决办法: public class SampleActivity extends Activity { /** * Instances of static inner classes do not ...
- Java 的printf(转)
出处:http://blog.csdn.net/swandragon/article/details/4653600 public class TestPrintf{public static voi ...
- 霸气!Nginx 中缓存静态文件秘籍
导读 这篇教程说明你应该怎样配置 nginx.设置 HTTP 头部过期时间,用 Cache-Control 中的 max-age 标记为静态文件(比如图片. CSS 和 Javascript 文件)设 ...