【刷题-LeetCode】166 Fraction to Recurring Decimal
- Fraction to Recurring Decimal
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)"
解 当余数重复出现时,意味着循环节出现了。因此需要用一个字典pos记录每个余数出现的位置,重复出现的余数对应的两个位置之间的即为循环节
Note: 运行时发现数字会出现溢出的问题,代码中的类型改成了long long int
class Solution {
public:
string fractionToDecimal(long long int numerator, int denominator) {
string res;
if(numerator < 0 && denominator > 0){
numerator *= -1;
res += "-";
}else if(numerator > 0 && denominator < 0){
denominator *= -1;
res += "-";
}
res += to_string(divmod(numerator, denominator));
if(numerator == 0)return res;
res += ".";
map<int, int>mp;
mp[numerator] = res.size();
while(numerator){
numerator *= 10;
int tmp = divmod(numerator, denominator);
res += to_string(tmp);
if(mp.find(numerator) != mp.end()){
res.insert(mp[numerator], "(");
res += ")";
break;
}
mp[numerator] = res.size();
}
return res;
}
long long int divmod(long long int &n1, int &n2){
long long int res = n1 / n2;
n1 -= res * n2;
return res;
}
};
【刷题-LeetCode】166 Fraction to Recurring Decimal的更多相关文章
- Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...
- ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Java for LeetCode 166 Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Leetcode#166 Fraction to Recurring Decimal
原题地址 计算循环小数 先把负数转化成正数,然后计算,最后添加符号 当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数 需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧 ...
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- 【LeetCode】166. Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 【leetcode】Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 166. Fraction to Recurring Decimal
题目: Given two integers representing the numerator and denominator of a fraction, return the fraction ...
- [LeetCode#116]Fraction to Recurring Decimal
Problem: Given two integers representing the numerator and denominator of a fraction, return the fra ...
随机推荐
- HTTPS握手-混合加解密过程
SSL协议通信过程 (1) 浏览器发送一个连接请求给服务器;服务器将自己的证书(包含服务器公钥S_PuKey).对称加密算法种类及其他相关信息返回客户端; (2) 客户端浏览器检查服务器传送到CA证书 ...
- js文件需要jsp页面中的div时,此js文件必须在div之后才能获得值,否则获取不到
js文件需要jsp页面中的div时,此js文件必须在div之后才能获得值,否则获取不到 2.图2的内容为directionkey.js的内容
- git修改账号密码
查看当前用户名和邮箱 git config user.name git config user.email 修改 git config --global user.name "新用户名&qu ...
- vue常用技巧-动态btn的封装
@1.要求: 1.点击某个按钮后激活active样式,其余按钮则为normal样式 2.要满足任意个数btn(btn个数不确定) @2.思路: 1.首先,btn个数不确定则意味着必须使用v-for循环 ...
- 第十八个知识点:画一个描述ECB,CBC,CTR模式的操作
第十八个知识点:画一个描述ECB,CBC,CTR模式的操作 第8周是画三个图的任务,但是维基百科上已经有人画的很好了 https://en.wikipedia.org/wiki/File:ECB_en ...
- Propensity Scores
目录 基本的概念 重要的结果 应用 Propensity Score Matching Stratification on the Propensity Score Inverse Probabili ...
- ZFNet: Visualizing and Understanding Convolutional Networks
目录 论文结构 反卷积 ZFnet的创新点主要是在信号的"恢复"上面,什么样的输入会导致类似的输出,通过这个我们可以了解神经元对输入的敏感程度,比如这个神经元对图片的某一个位置很敏 ...
- [opencv]二维码识别开发流程及问题复盘总结
项目复盘总结 开发需求: 在桌面机器人(向下俯视)摄像头拍摄到的图像中做条形码识别与二维码识别. 条形码在图像固定位置,二维码做成卡片的形式在固定区域内随意摆放. 开发环境及相关库:ubuntu 18 ...
- Java工程编码格式由GBK转化成utf-8(编码格式互转)
在写项目的过程中我发现有的地方编码格式被设置成了 gbk 如果用eclipse等工具直接改回utf-8编码格式则会出现乱码. 下载:https://download.csdn.net/download ...
- Java初学者作业——用户输入一个小数,程序分解出整数部分和小数部分。
返回本章节 返回作业目录 需求说明: 用户输入一个小数,程序分解出整数部分和小数部分. 实现思路: 接收用户控制台输入的小数. 用强制类型转换将整数部分得到. 使用用户输入的小数减去整数部分得到小数部 ...