[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 ...
随机推荐
- Vue.js双向绑定的实现原理和模板引擎实现原理(##########################################)
Vue.js双向绑定的实现原理 解析 神奇的 Object.defineProperty 这个方法了不起啊..vue.js和avalon.js 都是通过它实现双向绑定的..而且Object.obser ...
- centos安装firefox flash插件
centos下的firefox flash插件默认不是最新版的,安装过程如下: 将安装地址添加到repolist中 sudo yum -y install http://linuxdownload.a ...
- paip.输入法编程--英文ati化By音标原理与中文atiEn处理流程 python 代码为例
paip.输入法编程--英文ati化By音标原理与中文atiEn处理流程 python 代码为例 #---目标 1. en vs enPHati 2.en vs enPhAtiSmp 3.cn vs ...
- paip.java OutOfMemoryError 解决方法o33
paip.java OutOfMemoryError 解决方法o33 java.lang.OutOfMemoryError: Requested # java.lang.OutOfMemoryErro ...
- Activemq 平台搭建与C#示列
ActiveMQ ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS ...
- iOS开发——高级技术&地图功能的实现
地图功能的实现 因为有个项目要在地图中显示位置,所以用到了MapKit. 记录下来,以免以后忘记. 加入MapKit library 首先得在项目中加入MapKit,如图 MapView 先增加一个V ...
- Leetcode 203 Remove Linked List Elements 链表
去掉链表中相应的元素值 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next ...
- 详解Bootstrap表单组件
表单常见的元素主要包括:文本输入框.下拉选择框.单选框.复选框.文本域.按钮等.下面是不同的bootstrap版本: LESS: forms.less SASS: _forms.scss boot ...
- WPA-PSK无线网络破解原理及过程(转)
本文将主要讲讲WPA-PSK类型的无线网络安全问题,首先我们看下802.11协议相关的基础知识. 802.11常见的几种认证方式: 1.不启用安全 2.WEP 3.WPA/WPA2-P ...
- C#中服务端接受前端JSON字符串转换成字典集合
我们是否可以把从前端接受的JSON字符串转换成字典集合呢? 比如从前端接收:{'size':'10', 'weight':'10kg'} 在服务端转换成:[{size:"10"}, ...