题目

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)”.

分析

由上描述,本题要求得整数相除的结果,对循环小数用括号扩之;

首先,对于除数和被除数的特殊情况需分类处理;

然后,得到整数部分;

再次,分析小数部分,若有循环小数得到正确下标增加括号;

注意:整数的溢出问题;

先将int类型保存至long long类型;

AC代码

class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
string str = "";
//除数为0,为异常情况
if (denominator == 0)
return str;
//被除数为0,结果为0
if (numerator == 0)
return "0";
//异或,numerator<0和denominator<0仅有一个为真
if (numerator < 0 ^ denominator < 0)
str += '-';
//转化为正数,INT_MIN转化为正数会溢出,故用long long;long long int n=abs(INT_MIN)得到的n仍然是负的,所以写成下面的形式
long long r = numerator; r = abs(r);
long long d = denominator; d = abs(d); //得到整数部分并保存
str += to_string(r / d); r = r % d; //可以整除,直接返回
if (r == 0)
return str; //添加小数点
str += ".";
//下面处理小数部分,用哈希表
unordered_map<int, int> map;
while (r){
//检查余数r是否在哈希表中,是的话则开始循环了
if (map.find(r) != map.end()){
str.insert(map[r], 1, '(');
str += ')';
break;
}
map[r] = str.size(); //这个余数对应于result的哪个位置
//正常运算
r *= 10;
str += to_string(r / d);
r = r % d;
}
return str; } //整数到字符串的转换函数
string intToStr(long long num)
{
string str = "";
if (num < 10)
{
char c = num + '0';
return str + c;
}
else
{
while (num)
{
int d = num % 10;
char c = d + '0';
str += c;
num /= 10;
}//while
reverse(str.begin(), str.end());
return str;
}//else
}
};

GitHub测试程序源码

LeetCode(166) Fraction to Recurring Decimal的更多相关文章

  1. 【Leetcode 166】 Fraction to Recurring Decimal

    Description: Given two integers representing the numerator and denominator of a fraction, return the ...

  2. 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)

    [LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...

  3. Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环

    分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...

  4. 【LeetCode】166. Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

  5. 【刷题-LeetCode】166 Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

  6. 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 ...

  7. 【leetcode】Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

  8. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  9. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

随机推荐

  1. JS中void(0)的含义

    看别人些的JavaScript脚本可以看到这样的代码: <a href="javascript:doTest2();void(0);">here</a> 但 ...

  2. C#关键字:yield

    yield是C#为了简化遍历操作实现的语法糖.在语句中使用 yield 关键字,表示在该关键字所在的方法.运算符或 get 访问器是迭代器.有两种形式: yield return <expres ...

  3. h5复制粘贴板,打开APP功能

    <div class="container"> <img src="../themes/mall/img/i_red_ad4.jpg"> ...

  4. AJPFX辨析continue与break的区别

    1.break : (1).结束当前整个循环,执行当前循环下边的语句.忽略循环体中任何其它语句和循环条件测试.(2).只能跳出一层循环,如果你的循环是嵌套循环,那么你需要按照你嵌套的层次,逐步使用br ...

  5. C++ error:Debug Assertion Failed.Expression:_BLOCK_TYPE_IS_VALID(phead->nBlock)

    Debug Assertion Failed.Expression:_BLOCK_TYPE_IS_VALID(phead->nBlockUse) 关于上面这个错误,我在上一篇文章中的程序遇到过了 ...

  6. JavaScript中三种字符串连接方式及其性能比较

    参考地址: https://www.cnblogs.com/programs/p/5554742.html 工作中经常会碰到要把2个或多个字符串连接成一个字符串的问题,在JS中处理这类问题一般有三种方 ...

  7. cocos2d-x入门学习篇;切换场景

    手机游戏开发最近很火爆,鉴于一直在学习c++,看起来上手就比较快了.这篇文章来自皂荚花 cocos2d-x技术,我把我的想法分享给大家. 首先来看一段代码: CCScene* HelloWorld:: ...

  8. SharePoint Online和SharePoint 2016 导出到Excel 表错误

    导出到Excel是一个有用的SharePoint功能.偶尔您可能会遇到该功能无法正常工作的情况.有几个原因可能导致此功能无法正常工作. Problem #1 使用非32位Internet Explor ...

  9. JSON 序列化格式

    一.C#处理简单json数据json数据: 复制代码代码如下: {"result":"0","res_info":"ok" ...

  10. HDU 5451 Best Solver(fibonacci)

    感谢这道题让我复习了一遍线代,还学习了一些奇奇怪怪的数论. 令 二项展开以后根号部分抵消了 显然有 所以要求的答案是 如果n比较小的话,可以直接对二项式快速幂,但是这题n很大 这个问题和矩阵的特征值以 ...