[Leetcode] 166. Fraction to Recurring Decimal 解题报告
来源:互联网 发布:c语言绘图 编辑:程序博客网 时间:2024/06/05 18:08
题目:
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)".
思路:
需要注意一些corner cases(例如分子或分母为零的情况,以及由于除以-1而导致的int类型数字溢出的情况等),具体可以参考下面的代码片段。这里说一下核心思路:1)处理整数部分,很简单,无须赘述;2)如果fraction的结果不是整数,那么给结果字符串添加“.”,并且处理小数部分。在处理小数部分的时候,需要建立一个哈希表,哈希表的key是余数,value是出现此余数时结果字符串的长度。如果余数不为零,则首先在哈希表中查找该余数是否出现过,如果出现过,则说明我们已经遇到了小数中的循环部分,此时在适当位置加上括号,返回结果;否则我们就在哈希表中添加该余数,并且更新余数和结果字符串,进入下一层循环。
代码:
class Solution {public: string fractionToDecimal(int numerator, int denominator) { string ret; if(denominator == 0) { // invalid input return ""; } if(numerator == 0) { // special case return "0"; } long long numers = abs((long long)numerator); // in case of overflow long long denoms = abs((long long)denominator); bool negative = (numerator > 0) ^ (denominator > 0); // integer part if(negative) { ret += "-"; } ret += to_string(numers / denoms); long remain = numers % denoms; if(remain == 0) { return ret; } ret += "."; unordered_map<long, int> hash; // map from remain to index while(remain != 0) { if(hash.count(remain) > 0) { ret.insert(ret.begin() + hash[remain], '('); ret += ')'; return ret; } else { hash[remain] = ret.size(); remain *= 10; ret += to_string(remain / denoms); remain = remain % denoms; } } return ret; }};
阅读全文
1 0
- LeetCode-Fraction to Recurring Decimal-解题报告
- [leetcode] 166. Fraction to Recurring Decimal 解题报告
- [Leetcode] 166. Fraction to Recurring Decimal 解题报告
- 166. Fraction to Recurring Decimal Leetcode Python
- Leetcode-166.Fraction to Recurring Decimal
- [Leetcode]166. Fraction to Recurring Decimal @python
- [leetcode] 166. Fraction to Recurring Decimal
- leetcode 166. Fraction to Recurring Decimal
- [LeetCode]166. Fraction to Recurring Decimal
- LeetCode *** 166. Fraction to Recurring Decimal
- LeetCode 166. Fraction to Recurring Decimal
- [LeetCode]problem 166. Fraction to Recurring Decimal
- leetcode-java-166. Fraction to Recurring Decimal
- LeetCode 166. Fraction to Recurring Decimal
- [LeetCode] 166. Fraction to Recurring Decimal
- Leetcode-166. Fraction to Recurring Decimal
- leetcode 166. Fraction to Recurring Decimal
- Leetcode 166. Fraction to Recurring Decimal
- 有限域和离散对数问题(ECC椭圆曲线算法2)
- Maven学习(四)
- dijkstra和floyd算法计算图中最小路径
- 天池离线赛
- 整合maven+spring+springmvc+mybatis实现ssm通用增删查改基础开发框架
- [Leetcode] 166. Fraction to Recurring Decimal 解题报告
- leetcode 96. Unique Binary Search Trees
- Java应用Tomcat执行过程之性能调优
- 傅里叶变换原理
- 算法导论----读书记
- 这里提供了在Linux上显示某个进程的线程的几种方式
- 垂直居中布局方案
- spring mvc controller间跳转 重定向 传参
- myeclipse下dwr.xml配置文件没有自动提示解决办法