【Leetcode 166】 Fraction to Recurring Decimal
Description:
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)".
Solution:
long division: 长除法
Trick: Determining whether two nums have different sign(+ or -) can use follow codes:
if((n<)^(d<)) //异号
or
if((n>)^(d>)) //异号
Above code avoiding product's value overflow.
Code:
class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
if(!numerator) return "";
long long n = numerator, d = denominator;
string ret = "";
if((n<)^(d<)) ret += '-';
if(n < ) n = -n;
if(d < ) d = -d;
ret += to_string(n/d);
if(n % d == ){
return ret;
}ret += '.';
map<long long, int>hash;
for(long long r = n % d; r; r %= d){
if(hash.find(r) != hash.end()){
ret.insert(hash[r],"(");
ret += ")";
return ret;
}
hash[r] = ret.size();
r *= ;
ret += to_string(r / d);
}
}
};
【Leetcode 166】 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
题目 Given two integers representing the numerator and denominator of a fraction, return the fraction ...
- 【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】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 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...
- 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 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 【LeetCode练习题】Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...
随机推荐
- PAT 1145 Hashing - Average Search Time
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- 2.5.4 华丽的 printf 输出
如同echo命令,printf命令可以输出简单的字符串: [many@avention my_sh]$ printf "Hello, world\n" ...
- 使用 XMLHttpRequest实现Ajax
[XMLHttpRequest的概述] 1.XMLHttpRequest最早是在IE5中以ActiveX组件的形式实现的.非W3C标准 2.创建XMLHttpRequest对象(由于非标准所以实现方法 ...
- Tensorflow Eager execution and interface
Lecture note 4: Eager execution and interface Eager execution Eager execution is (1) a NumPy-like li ...
- [luoguP3252] [JLOI2012]树(DP)
传送门 树上前缀和. 在树上找一条权值和为 s 的链,其中这个链上的点按深度递增(递减)(不同) dfs 每搜到一个点求它的前缀和 sum[x],放入 set 中. 在 set 中找 sum[x] - ...
- 20181010关于pt-kill自动杀死运行超长的进程
转自: http://blog.chinaunix.net/uid-16844903-id-4442030.htmlhttp://blog.chinaunix.net/uid-31396856-id- ...
- HDU 1248寒冰王座-全然背包或记忆化搜索
寒冰王座 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- Python基础--高速改造:字符串
Python的字符串值得一说. 先看: >>>"Hello world!" 'Hello world!' 我们写是双引號,可是打印出来后是单引號. 差别何在? 答 ...
- JAVA 如何反编译JAR文件
1 直接的jar文件可以用winrar解压,然后得到class文件,但是这里得到的class文件也是编译过的二进制文件,用传统的文本编辑器无法打开. 2 用XJad这个软件可以反编译Jar文件,直接找 ...
- Android中不同方向嵌套滑动的解决方式(ListView为样例)
前言: 就像手机QQ的聊天消息列表.一个纵向滑动的ListView列举全部消息,但每一条消息能够横向滑动. 而默认情况下,仅仅能有一个地方消化处理触摸事件,要么ListView吃掉这个事件.要么子It ...