博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Fraction to Recurring Decimal
阅读量:7088 次
发布时间:2019-06-28

本文共 2970 字,大约阅读时间需要 9 分钟。

Well, the key to this problem is on how to identify the recurring parts. After doing some examples using pen and paper, you may find that for the decimal parts to recur, the remainders should recur. So we need to maintain the remainders we have seen. Once we see a repeated remainder, we know that we have reached the end of the recurring parts and should enclose it with a ). However, we still need to insert the ( to the correct position. So we maintain a mapping from each remainder to the position of the corresponding quotient digit of it in the recurring parts. Then we use this mapping to retrieve the starting position of the recurring parts.

Now we have solved the trickiest part of this problem.

There are some remaining problems to solve to achieve a bug-free solution.

  1. Pay attention to the sign of the result;
  2. Handle cases that may cause overflow like numerator = -2147483648, denominator = -1appropriately by using long long;
  3. Handle all the cases of (1) no fractional part; (2) fractional part does not recur; and (3) fractional part recurs respectively.

To handle problem 3, we divide the division process into the integral part and the fractional part. For the fractional part, if it does not recur, then the remainder will become 0 at some point and we could return. If it does recur, the method metioned in the first paragraph has already handled it.

Taking all these into considerations, we have the following code. Note that I implement anint2str function, which may be replaced by the system to_string if you like.

1 class Solution { 2 public: 3     string fractionToDecimal(int numerator, int denominator) { 4         if (!numerator) return "0";  5         string res; 6         if (numerator < 0 ^ denominator < 0) res += '-'; 7         long long numer = numerator < 0 ? (long long)numerator * (-1) : (long long)numerator; 8         long long denom = denominator < 0 ? (long long)denominator * (-1) : (long long)denominator; 9         long long integral = numer / denom;10         res += int2str(integral);11         long long rmd = numer - integral * denom;12         if (!rmd) return res;13         res += '.';14         rmd *= 10;15         map
mp;16 while (rmd) {17 long long quotient = rmd / denom;18 if (mp.find(rmd) != mp.end()) {19 res.insert(mp[rmd], 1, '(');20 res += ')';21 break;22 }23 mp[rmd] = res.size();24 res += int2str(quotient);25 rmd = (rmd - denom * quotient) * 10;26 }27 return res;28 }29 private:30 string int2str(long long num) {31 string str;32 while (num) {33 int digit = num % 10;34 str += digit + '0';35 num /= 10;36 }37 if (str.empty()) return "0";38 reverse(str.begin(), str.end());39 return str; 40 }41 };

 

转载于:https://www.cnblogs.com/jcliBlogger/p/4601095.html

你可能感兴趣的文章
前端性能优化常用总结
查看>>
flutter安装开发环境-问题记录
查看>>
第十四课时: 登录/登出以及JWT认证
查看>>
渲染机制/页面性能/错误监控
查看>>
Dom中高big 事件总结(持续更新中)
查看>>
Immutable.js 源码解析 --List 类型
查看>>
【修真院“善良”系列之十六】代码结构中Dao,Service,Controller,Util,Model是什么意思,为什么划分...
查看>>
js数据结构-栈
查看>>
前端构建_webpack
查看>>
Looper源码
查看>>
微信小程序开发系列五:微信小程序中如何响应用户输入事件
查看>>
程序员如何优雅的记录笔记(同步云端,图床,多端发布)
查看>>
极速高清——给你带来全新的高清视野
查看>>
数据结构之链表【上】
查看>>
Go并发实战笔记整理
查看>>
奇葩问题
查看>>
使用 Laravel 5.5+ 更好的来实现 404 响应
查看>>
PHP 网络编程小白系列 —— Accept 阻塞模型
查看>>
流畅的python读书笔记-第十六章-携(协)程
查看>>
Python学到什么程度才可以去找工作?掌握这4点足够了!
查看>>