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

本文共 2910 字,大约阅读时间需要 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 };

 

转载地址:http://txcra.baihongyu.com/

你可能感兴趣的文章
django ajax提交 Forbidden CSRF token missing
查看>>
maven常见异常
查看>>
shell基础一
查看>>
windows下查看端口占用情况
查看>>
轻松玩转window7之五:管理共享
查看>>
邮件服务器搭建,可连接客户端
查看>>
大数据时代的遨游
查看>>
大数据测试之hadoop单机环境搭建(超级详细版)
查看>>
我的友情链接
查看>>
CSS教程:div垂直居中的N种方法[转]
查看>>
不要做浮躁的嵌入式系统工程师
查看>>
linux 文件操作与目录操作
查看>>
解决IE6浏览器下position:fixed固定定位问题
查看>>
KMP串匹配算法解析与优化
查看>>
css3动画简介以及动画库animate.css的使用
查看>>
javascript DOM节点操作
查看>>
c++ invoke java in android
查看>>
meta 之 viewport
查看>>
Linux下文件 ~/.bashrc 和 ~/.bash_profile 和 /etc/bashrc 和 /etc/profile 的区别 | 用户登录后加载配置文件的顺序...
查看>>
关于在swiper轮播组件中使用echarts的'click'事件无效
查看>>