【原创】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 ...
随机推荐
- 产生n不同随机数的算法
昨天无聊,就模仿仙剑5外传中的卡牌游戏做了一个小游戏,结果在开发这个小游戏的时候,碰到了产生多个不同随机数的问题.我们知道,仙剑中的卡牌游戏是随机产生16张图片,并且这16张图片是两个一组的,因为只有 ...
- 关于PHP的内置服务器的使用
今天刚开始正式学习PHP(之前有一点了解),推荐学习的网站是w3school.一开始不知道tomcat服务器不支持PHP脚本,直接把.php文件放到tomcat里面去运行,结果嵌入的php代码段没有什 ...
- [Word使用笔记]分类简介
什么Vistual Studio , Eclipse , Xcode , 都弱爆了,Word比他们难多了 - -! 此分类用于记录Word的一些使用
- 怎样在Android本地视频播放器开发
在上一章Android本地视频播放器开发--SDL编译编译中编译出sdl的支持库,当时我们使用的2.0,但是有些api被更改了,所以在以下的使用者中我们使用SDL1.3的库,这个库我会传上源码以及编译 ...
- 开启cocos2dx 3.0的Console功能
下面内容用于自己知识的备忘,想看具体内容,请參照例如以下地址. 原英文文地址: http://discuss.cocos2d-x.org/t/cocos3-0-tutorial-console-tut ...
- opencv 训练自己的分类器汇总
原地址:http://www.cnblogs.com/zengqs/archive/2009/02/12/1389208.html OpenCV训练分类器 OpenCV训练分类器 一.简介 目标检测方 ...
- vc 基于对话框多线程编程实例——线程之间的通信
vc基于对话框多线程编程实例——线程之间的通信 实例:
- 超人学院Hadoop大数据技术资源分享
超人学院Hadoop大数据技术资源分享 http://bbs.superwu.cn/forum.php?mod=viewthread&tid=807&fromuid=645 很多其它精 ...
- 【Cocos2d-X开发学习笔记】第30期:游戏中数据的存储(下)
本系列学习教程使用的是cocos2d-x-2.1.4(最新版为3.0alpha0-pre) ,PC开发环境Windows7,C++开发环境VS2010 一.对数据进行编解码 在上一期中,我们使用了CC ...
- cocos2d-x-3.1 事件分发机制 (coco2d-x 学习笔记七)
触摸事件 Sprite* sp1 = Sprite::create("Images/t1.png"); sp1->setPosition(Vec2(visibleSize.w ...