算法描述:

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.

Example 1:

Input: numerator = 1, denominator = 2
Output: "0.5"

Example 2:

Input: numerator = 2, denominator = 1
Output: "2"

Example 3:

Input: numerator = 2, denominator = 3
Output: "0.(6)"

解题思路:模拟题。需要注意的点:1 正负号,2 溢出 ,3 小数点 ,4利用unordered_map判断重复

    string fractionToDecimal(int numerator, int denominator) {if(numerator==0) return "0";string res = "";if((numerator <0 && denominator > 0 ) || (numerator >0 && denominator <0)) res += "-";long long num1 = llabs(numerator);long long num2 = llabs(denominator);res += to_string(num1/num2);long long mo = num1%num2;if(mo == 0) return res;res += ".";unordered_map<int,int> unmap;while(mo > 0){if(unmap.find(mo)!=unmap.end()){res.insert(unmap[mo],1,'(');res += ")";break;}unmap[mo]=res.size();res += to_string(mo*10 / num2);mo = mo*10 % num2;}return res;}

转载于:https://www.cnblogs.com/nobodywang/p/10400255.html

LeetCode-166- Fraction to Recurring Decimal相关推荐

  1. Leetcode 166. Fraction to Recurring Decimal

    这个是我比较原始的解法.在这里有个trick就是把int扩展为long.要考虑负数情况,以及负数最小值的情况. public class Solution {public String fractio ...

  2. leetcode 592. Fraction Addition and Subtraction | 592. 分数加减运算(最大公因数gcd,最小公倍数lcm)

    题目 https://leetcode.com/problems/fraction-addition-and-subtraction/ 题解 这题既简单又麻烦,一道 hard 的 easy 题,被划分 ...

  3. int 小数_[LeetCode] 166. 分数到小数

    题目描述 : https://leetcode-cn.com/problems/fraction-to-recurring-decimal/ 题目描述: 给定两个整数,分别表示分数的分子 numera ...

  4. LeetCode 166. 分数到小数(小数除法)

    1. 题目 给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数. 如果小数部分为循环小数,则将循环的部分括在括号内. 示例 1: 输入: num ...

  5. leetcode 166分数到小数

    手动排除特殊情况: 对于一般情况,使用位运算和加减法来计算除法,使用sign记录结果符号:(这部分为leetcode 29题的答案) 使用hashmap来记录循环体出现的开始位置(如果有的话),使用f ...

  6. Leetcode 166. 分数到小数 解题思路及C++实现

    解题思路: 这道题目的核心是怎么找到循环部分:当出现重复的余数时,就会出现循环. 前面需要考虑比较多种情况:除数是否为0:结果的正负:结果的整数部分和小数部分分开求解. 在求解小数部分的时候,需要用到 ...

  7. LeetCode 166. 分数到小数

    思路: 循环体出现的依据是,产生重复的余数,即每次mod*10 后再和 除数b计算出新余数,和之前的比较即可. 先对int型数据进行转换为long 以免int负数最小值化正越界 判定正负数, 结果是否 ...

  8. 寒假LeetCode打卡

    文章目录 @[toc] 链表专题 LeetCode 19. Remove Nth Node From End of List LeetCode 83. Remove Duplicates from S ...

  9. nomasp 博客导读:Lisp/Emacs、Algorithm、Android

    Profile Introduction to Blog 您能看到这篇博客导读是我的荣幸,只要我的技术有提升,这个博客就会一直更新下去,感谢您的支持,欢迎您的关注与留言.目前博客有多个专栏,分别是关于 ...

最新文章

  1. xml python2.6_如何使用前缀选项解析python 2.6中的参数为-f file.xml
  2. 马尔科夫网络和一阶马尔科夫链
  3. 【若依(ruoyi)】table定制列宽
  4. 计算机 教育 研究生分数查询,专业硕士在考试结束之后几周内可以去查分呢现在只能是通过电脑来查分了吗...
  5. 异步fifo_同步FIFO设计实现
  6. 山东大学项目实训开发日志——基于vue+springboot的医院耗材管理系统(二)
  7. 牛腩新闻发布系统(7)——总结
  8. saltstack的探索-利用脚本增加用户
  9. 了解数据库索引及其原理
  10. 1.2音响系统放大器
  11. excel文件修复工具_DBF Doctor下载_DBF文件修复软件官方版下载[管理工具]
  12. 深富策略:险资密集调研 周期股板块能否崛起?
  13. 辅音字母组合功能音中的浊化现象
  14. 后端程序员的 uni-app 教程
  15. 解决电脑右侧数字键盘无法打出数字问题
  16. 打印机服务器文件,打印机服务器ftp配置文件
  17. php 微信支付 ca证书,微信企业付款 CA证书出错,请登录微信支付商户平台下载证书...
  18. 使用JavaScript创建SVG矢量图Code128编码
  19. 如何删除oracle 的用户及其数据
  20. pythoncharm怎么保存_pycharm软件实现设置自动保存操作

热门文章

  1. 每张脸值5美元,谷歌花钱买数据强化刷脸,还把隔空操控手机变成现实
  2. docker中使用systemctl启动服务
  3. session、flask session知识的相关收集
  4. Fabric环境搭建
  5. HttpServletRequest HttpServletResponse ServletException 重新打开后报红解决方法
  6. django时间与系统时间差8小时
  7. Linux系统程序包管理工具 RPM
  8. linux查看内核版本、系统版本、系统位数(32or64)
  9. Serializable Clonable
  10. MDSF:在线查看【模型驱动软件工厂】文章汇总