[LeetCode] 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.
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)".
Credits:
Special thanks to @Shangrila for adding this problem and creating all test cases.
#include <iostream>
#include <unordered_map>
#include <string>
#include <sstream>
using namespace std; class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
bool flag1 = numerator<;
bool flag2 = denominator<;
unsigned numer = flag1?-numerator:numerator;
unsigned denom = flag2?-denominator:denominator; if(denom==) return "";
if(numer==) return "";
stringstream ss;
ss<<numer/denom;
string ret = ss.str();
unsigned long long int lft = numer%denom; unordered_map<unsigned int,unsigned int> mp;
string ret1="";
unsigned int cnt = ;
while(lft){
if(mp[lft]==){
mp[lft]=cnt++;
ret1 += (lft*)/denom + '';
lft = (lft*)%denom;
continue;
}
ret1 = ret1.substr(,mp[lft]-) + "(" + ret1.substr(mp[lft]-) + ")";
break;
}
if(ret1 != "") ret += "." + ret1;
if(flag1^flag2) ret = "-" + ret;
return ret;
}
}; int main()
{
Solution sol;
cout<<sol.fractionToDecimal(-,-)<<endl;
return ;
}
[LeetCode] Fraction to Recurring Decimal 哈希表的更多相关文章
- [LeetCode] Fraction to Recurring Decimal 分数转循环小数
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- LeetCode Fraction to Recurring Decimal
原题链接在这里:https://leetcode.com/problems/fraction-to-recurring-decimal/ 题目: Given two integers represen ...
- 【leetcode】Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal
1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...
- 【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
题目 Given two integers representing the numerator and denominator of a fraction, return the fraction ...
随机推荐
- java web module of login
Reffer to the book<java web整合开发王者归来>. It's jsp page. Offer the values of username and password ...
- 深入了解javascript事件流
摘要:事件流这个东西是比较重要的,为了让自己更加理解js中的事件流,必须整理整理,梳理一下事件流的各种东西啊.本文大部分内容参考<javascript高级程序设计第三版> 先来一段书里的原 ...
- latex数字加粗后变宽
latex的数字默认用的是Times New Roman字体,这个字体有个不优美之处就是加粗后会变宽,如下图所示: 平常倒是也无所谓.昨天在把实验数据整理进表格时,为了凸显每个数据集上各个实验方法的优 ...
- Java程序员的日常——经验贴(纯干货)二
继昨天的经验贴,今天的工作又收获不少. windows下编辑器会给文件添加BOM 在windows的编辑器中,为了区分编码,通常会添加一个BOM标记.比如,记事本.nodepade++.sublime ...
- Emmet语法大全手册
这是基于官方手册整理制作的,因为那个手册网页打开很慢,所以就整理在这里了.以备不时之需. Syntax Child: > nav>ul>li <nav> <ul ...
- 利用jsoup爬虫工具,爬取数据,并利用excel导出
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.FileInputStream; i ...
- thinkphp框架下404页面设置
404页面即系统在找不到请求的操作方法和找不到请求的控制器名称时的一种报错行为的优化. 第一步:在thinkphp框架中的Home/Comtroller中建一个EmptyController.clas ...
- Android中Parcelable接口的使用
在做开发的过程中,序列化是非常常见的.比如要将对象保存本地磁盘或者在网络上传输等.实现序列化有两种方式,一种是实现Serializable接口,第二种是实现Parcelable. Serializab ...
- IE6-IE11兼容性问题列表及解决办法总结
相比IE6-IE9那版,主要添加IE10和IE11的新变化. 以下是目录及下载链接: 目录 概述 2 第一章:HTML 3 第一节:IE7-IE8更新 3 1. 如果缺少结束标记的 P 元素后跟 TA ...
- 【Vegas原创】RHEL6多界面切换方法
CTRL+ALT+Fn(n=1-6) F1,是图形化界面 F2-F6,是字符型界面. 每个界面可以干不同的事情,很牛叉.