166. Fraction to Recurring Decimal (Math)
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)".
class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
//deal with overflow (eg: -2147483648/-1, -1/-2147483648)
long l_numerator = (long) numerator;
long l_denominator = (long) denominator;
//deal with negative
string ret = "";
int p = ; //pointer to the return string
if((l_numerator < && l_denominator > ) || (l_numerator > && l_denominator < )) {
ret += "-";
p++;
}
l_numerator = abs(l_numerator);
l_denominator = abs(l_denominator);
long quotient = l_numerator/l_denominator;
long residue = l_numerator%l_denominator;
unordered_map<int,int> res_pos_map;
stringstream ss;
string sTmp;
ss << quotient;
ss >> sTmp;
ret += sTmp;
p+=sTmp.size();
if(residue!=) {
ret += ".";
p++;
res_pos_map[residue]=p;
}
while(residue!=){
l_numerator = residue * ;
quotient = l_numerator/l_denominator;
residue = l_numerator%l_denominator;
ss.clear();
ss << quotient;
ss >> sTmp;
ret += sTmp;
p++;
if(res_pos_map.find(residue)==res_pos_map.end()){
res_pos_map[residue]=p;
}
else{
ss.clear();
ss << quotient;
ss >> sTmp;
ss.clear();
ss << residue*/l_denominator;
ss >> sTmp;
ret = ret.substr(,res_pos_map[residue]) + "(" + ret.substr(res_pos_map[residue]) + ")";
return ret;
}
}
return ret;
}
};
166. Fraction to Recurring Decimal (Math)的更多相关文章
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...
- 【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 分数转换 --------- java
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- 166. Fraction to Recurring Decimal
题目: Given two integers representing the numerator and denominator of a fraction, return the fraction ...
- Java for LeetCode 166 Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- 166. Fraction to Recurring Decimal -- 将除法的商表示成字符串(循环节用括号表示)
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- 166 Fraction to Recurring Decimal 分数到小数
给定两个整数,分别表示分数的分子和分母,返回字符串格式的小数.如果小数部分为循环小数,则将重复部分括在括号内.例如, 给出 分子 = 1, 分母 = 2,返回 "0.5". ...
随机推荐
- python 进程之间的数据共享
from multiprocessing import Process,Manager import os def f(d,n): d[os.getpid()] = os.getppid()#对字典d ...
- day 41 标准文档流 浮动
一.标准文档流 什么是标准文档流 宏观的将,我们的web页面和ps等设计软件有本质的区别,web 网页的制作,是个“流”,从上而下 ,像 “织毛衣”.而设计软件 ,想往哪里画东西,就去哪里画 标准文档 ...
- tomcat接口调用时延开关
项目中有些页面时延不稳定,需要看每次接口调用时延,怎么看,有两种方法:一种是直接去catalina.out日志中看,一种是直接去localhost_access_log日志中看,第一种需要在代码中实现 ...
- eclipse开启时报错问题
eclipse启动时报如下错误: Unable to read workbench state.Workbench UI layout will be reset 不能找到正式的工作台,工作台UI的布 ...
- HOOK - 低级鼠标Hook
参考博客 一.SetWindowsHookEx HHOOK WINAPI SetWindowsHookEx( __in int idHook, \\钩子类型 __in HOOKPROC lpfn, \ ...
- centos7.6+samba+设置可读可写不可删权限
samba原文 https://www.cnblogs.com/muscleape/p/6385583.html 设置可读可写不可删权限原文: https://blog.51cto.com/guanh ...
- Hibernate Criteria使用
hibernate中Criteria的完整用法 Criteria 是一个完全面向对象,可扩展的条件查询API,通过它完全不需要考虑数据库底层如何实现.SQL语句如何编写,是Hibernate框架的核心 ...
- 爬虫——requests模块
一 爬虫简介 #1.什么是互联网? 互联网是由网络设备(网线,路由器,交换机,防火墙等等)和一台台计算机连接而成,像一张网一样. #2.互联网建立的目的? 互联网的核心价值在于数据的共享/传递:数据是 ...
- android 开发 View _3_ View的属性动画ValueAnimator
ValueAnimator ValueAnimator继承自抽象类Animator.要让属性动画渐变式地更改对象中某个属性的值,可分两步操作:第一步,动画需要计算出某一时刻属性值应该是多少:第二步,需 ...
- 日志模块logging
import logging # 设置日志基础样式 logging.basicConfig(level=logging.INFO, format='levelname:%(levelname)s fi ...