【原创】leetCodeOj --- Fraction to Recurring Decimal 解题报告
原题地址:
https://oj.leetcode.com/problems/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)".
方法:
要点有几个
0、模拟除法(自己做一遍,就是笔算复杂除法的过程。)
1、记住你产生的新的被除数以及其index,可以就用一个map来记,为了直观些我加了个set。
2、对产生的新被除数进行判断,如果重复了(map中有了),说明是循环小数,结束循环,在index前面加个括号,当然尾巴括号也要补上。
3、小心负数,由于正负不对称,为了方便我引入了long long类型,这样就不用担心溢出和负数了。
全部代码:
class Solution {
public:
string fractionToDecimal(int numerator1, int denominator1) {
bool isNegti = false;
long long numerator = numerator1;
long long denominator = denominator1;
long long interger = numerator / denominator;
if (numerator * denominator < 0) {
isNegti = true;
interger = -interger;
}
numerator = numerator < 0 ? -numerator : numerator;
denominator = denominator < 0 ? -denominator :denominator;
long long next;
if ((next = numerator % denominator) == 0)
return isNegti == true ? "-" + strval(interger) : strval(interger);
string deci = ".";
unordered_set<long long> dict;
unordered_map<long long,long long> start;
unordered_set<long long>::iterator itDict;
unordered_map<long long,long long>::iterator itStart;
char nownum;
int index = 1;
while (next) {
itDict = dict.find(next);
if (itDict != dict.end()) {
itStart = start.find(next);
int len = itStart->second;
deci = deci.substr(0,len) + '(' + deci.substr(len) + ')';
break;
}
dict.insert(next);
start[next] = index ++;
nownum = (next * 10) / denominator + '0';
deci = deci + nownum;
next = (next * 10) % denominator;
}
return isNegti == true ? "-" + strval(interger) + deci : strval(interger) + deci;
}
static string strval(long long num) {
char tmp[20];
char index = 0;
while (num >= 10) {
tmp[index ++] = (char)(num % 10 + '0');
num /= 10;
}
tmp[index] = (char)(num + '0');
string res = "";
for (int i = index; i >= 0; i --) {
res = res + tmp[i];
}
return res;
}
};
【原创】leetCodeOj --- Fraction to Recurring Decimal 解题报告的更多相关文章
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- 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】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 弗洛伊德判环
分数转小数,要求输出循环小数 如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】592. Fraction Addition and Subtraction 解题报告(Python)
[LeetCode]592. Fraction Addition and Subtraction 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuem ...
- 【刷题-LeetCode】166 Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 【原创】leetCodeOj --- Binary Search Tree Iterator 解题报告
时间挤挤总是有的 太久不做题,脑子都生锈了.来道水题练练手 题目地址: https://leetcode.com/problems/binary-search-tree-iterator/ 题目内容: ...
- 【原创】leetCodeOj --- Excel Sheet Column Title 解题报告
题目地址: https://oj.leetcode.com/problems/excel-sheet-column-title/ 题目内容: Given a positive integer, ret ...
随机推荐
- haproxy 服务端超时时间 timeout server 17000 --后台程序17秒没有响应,返回超时
haproxy 服务端超时时间: haproxy 配置: timeout server 17000 --后台程序17秒没有响应,返回超时 Jun 27 09:29:56 localhost hapro ...
- Ray Through Glasses
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30506#problem/T 题意:给你一束光,问你在一个三层的平面类传递n次的种数: 仔 ...
- iOS Dev (60) 怎样实现 UITextView 中的 placeHolder
iOS Dev (60) 怎样实现 UITextView 中的 placeHolder 作者:阿锐 地址:http://blog.csdn.net/prevention - 跟着你的 UITextVi ...
- 最想做的三个Delphi项目:Paint,IM,SQL,另外还有Smart,TMS,FMX,UML,FreePascal,Python4Delphi,Cheat Engine
都是绝美项目- 如果有时间,要做的项目:0. 整整5个Cloud项目(可带来商业收益,其中还包括手机发送, S/D/N/L/NetDriver)1. Heidi/front/SQLITE STUDIO ...
- Delphi基础Write写入结构体到文件(使用 file of myrecord就行了,真简单)
program WriteStruct; {$APPTYPE CONSOLE} uses SysUtils; //写入结构体 type TCustomer = record ID: ]; Code: ...
- facebook打开动画pop
POP源代码:https://github.com/facebook/pop demo相关链接:https://github.com/jxd001/POPdemo/blob/master/README ...
- TCP/IP详细解释--TCP/IP可靠的原则 推拉窗 拥塞窗口
TCP和UDP在同一水平---传输层.但TCP和UDP最不一样的地方.TCP它提供了一个可靠的数据传输服务,TCP是面向连接的,那.使用TCP两台主机通过第一通信"拨打电话"这个过 ...
- Object.wait()的使用方法示例(转)
本文节选自 Effective Java by Joshua Bloch 和 Concurrent Programming in Java by Doug Lea. 1.3 原子数据的同步 java语 ...
- Monkey源代码分析之执行流程
在<MonkeyRunner源代码分析之与Android设备通讯方式>中.我们谈及到MonkeyRunner控制目标android设备有多种方法.当中之中的一个就是在目标机器启动一个mon ...
- SWT中一些细节的说明
1.shell.pack();函数的效果是将程序打包,使其以合适的大小显示出来,不会太大有很多多余空间,也不会太小导致有些控件无法显示. 2. while(!shell.isDisposed()){ ...