【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 ...
随机推荐
- Java并发编程核心方法与框架-CompletionService的使用
接口CompletionService的功能是以异步的方式一边生产新的任务,一边处理已完成任务的结果,这样可以将执行任务与处理任务分离.使用submit()执行任务,使用take取得已完成的任务,并按 ...
- 并发下常见的加锁及锁的PHP具体实现代码(转)
在最近的项目中有这样的场景 1.生成文件的时候,由于多用户都有权限进行生成,防止并发下,导致生成的结果出现错误,需要对生成的过程进行加锁,只容许一个用户在一个时间内进行操作,这个时候就需要用到锁了,将 ...
- 是时候放弃Uploadify了
1.Uploadify是什么? Uploadify是来自国外的一款优秀jQuery插件,主要功能是批量上传文件. 这话是复制百度百科的,是:基于jQuery开发的文件上传插件. 2.为什么大家都在用? ...
- android-解决EditText的inputType为Password时, 字体不一致的问题
今天做项目的时候,发现当edittext 的InputType为password时,它的字体和原来不一样: 网上找了一下,给出了解决办法: 第一: 去掉xml文件中的password配置,在代码中编写 ...
- ajax实例详解(2)
说到ajax当然离不开json格式了.在ajax的数据传递过程中用到最多的便是json格式了吧. 什么是json格式,在刚开始用它的时候,着实让人迷茫了一阵子呀.说的白了点所谓json不就是一种数据的 ...
- hdu4915 Parenthese sequence 贪心O(n)解法(new)
hdu4915 Parenthese sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- Eclipse将引用了第三方jar包的Java项目打包成jar文件的两种方法
方案一:用Eclipse自带的Export功能 步骤1:准备主清单文件 “MANIFEST.MF”, 由于是打包引用了第三方jar包的Java项目,故需要自定义配置文件MANIFEST.MF,在该项目 ...
- 系统研究Airbnb开源项目airflow
开源项目airflow的一点研究 调研了一些几个调度系统, airflow 更满意一些. 花了些时间写了这个博文, 这应该是国内技术圈中最早系统性研究airflow的文章了. 转载请注明出处 htt ...
- java httpclient发送json 请求 ,go服务端接收
/***java客户端发送http请求*/package com.xx.httptest; /** * Created by yq on 16/6/27. */ import java.io.IOEx ...
- 【转】Eclipse里项目名有红叉,但是展开后里面又没有红叉叉
Eclipse里项目名有红叉,但是下面的每一个文件都没有红叉 有三种可能: 1:classpath有问题 2:编译级别有问题 3:jar包有问题,我碰过从maven上获取了问题jar包,工程里提示能找 ...