[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 ...
随机推荐
- JavaScript 中的 this ,看着一篇就够了
tip 在 js 中,this 这个上下文总是变化莫测,很多时候出现 bug 总是一头雾水,其实,只要分清楚不同的情况下如何执行就 ok 了. 全局执行 首先,我们在全局环境中看看它的 this 是什 ...
- 常用js方法整理common.js
项目中常用js方法整理成了common.js var h = {}; h.get = function (url, data, ok, error) { $.ajax({ url: url, data ...
- 深入理解javascript事件流
摘要:事件流这个东西是比较重要的,为了让自己更加理解js中的事件流,必须整理整理,梳理一下事件流的各种东西啊.本文大部分内容参考<javascript高级程序设计第三版> 先来一段书里的原 ...
- SHINY-SERVER R(sparkR)语言web解决方案 架设shiny服务器
1. shiny server简介 shiny-server是一种可用把R 语言以web形式展示的服务,其实RStudio公司自己构建了R Shiny Application运行的平台(http:// ...
- js系列(9)js的运用(一)
本节开始介绍javascript在html页面中的运用. (1)link样式表的动态绑定:(http://files.cnblogs.com/files/MenAngel/text04 ...
- PHP实现微信公众账号开发
1.首先需要一个可以外网访问的接口url. 我这里是申请的新浪免费云服务器,http://xxxxx.applinzi.com/wx.php,具体自己可以去新浪云中心申请地址为:http://www. ...
- 了解 JavaScript (4)– 第一个 Web 应用程序
在下面的例子中,我们将要构建一个 Bingo 卡片游戏,每个示例演示 JavaScript 的不同方面,通过每次的改进将会得到最终有效的 Bingo 卡片. Bingo 卡片的内容 美国 Bingo ...
- C/C++在Java项目、Android和Objective-C三大平台下实现混合编程
Android和iOS开发都支持C++开发,可以一套代码多平台使用.同时C++难以反编译的特性也可以为Android开发带来代码的保密,另一native特性也可以提高代码的运行效率. 一.为什么使用C ...
- iOS 7.1 UITableView添加footerView 后 最后一行分割线无法显示
今天用故事版 遇到个奇怪的问题: 我要用 tbView(tableView)展示写信息.最后一行我要显示些文案什么的.考虑用 footerView ,开心coding ..,show下 哪里有些不对吧 ...
- PPTP VPN 一键安装包(图文,OpenVZ适用)[zz]
[介绍] VPN的英文全称是“Virtual Private Network”,中文名叫“虚拟专用网络”.VPN可以通过特殊加密的通讯协议连接到Internet上,在位于不同地方的两个或多个内部网之间 ...