题目

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. shell下批量除去文件名中的空格

    rename 's/ /_/g' * 上述命令可以将当前文件夹内所有文件的名字中得所有空格替换为_.其中g代表所有,如果不加g,如果文件名字中有多个空格,仅替换第一个.

  2. 1550: Simple String 最大流解法

    http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1550 很久以前做的一题,当时队友用最大流做,现在我也是 这个转化为二分图多重匹配,就是一样的意 ...

  3. java中两个map比较

    一 /** * 用map的keySet()的迭代器(性能效率较低) * */ public void compareMap1 (){ Map<String, String> m1 = ne ...

  4. Json 后台转对象的方式或者获取属性值方式。

    类似这类的 json字符串 后台转成 model 或者取其中一个属性值. 需要去掉前后引号 以及将转义字符去掉.空格代替 resoult = resoult.Substring(0, resoult. ...

  5. Java中的switch语句——通过示例学习Java编程(8)

    作者:CHAITANYA SINGH 来源:https://www.koofun.com//pro/kfpostsdetail?kfpostsid=19 当我们在代码逻辑中有多个选项,而且需要为每个选 ...

  6. <context:property-placeholder>标签实现参数剥离

    <context:property-placeholder>标签提供了一种优雅的外在化参数配置的方式(可以是键值对的形式保存在.properties文件中),不过该标签在spring配置文 ...

  7. ABAP跳转屏幕

    1.call transaction语句跳转屏幕 '. CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN. . 2.调用函数 CALL FUNCTION 'M ...

  8. MapReduce的编程思想(1)

    MapReduce的编程思想(1) MapReduce的过程(2) 1. MapReduce采用分而治之的思想,将数据处理拆分为主要的Map(映射)与Reduce(化简)两步,MapReduce操作数 ...

  9. Linux下使用crontab命令配置定时任务

    一.语法结构 crontab [-e [UserName]|-l [UserName]|-r [UserName]|-v [UserName]|File ] 说明 : crontab 是用来让使用者在 ...

  10. Windows7(x86) xampp php5.5 imagick install

    I hate windows. 1. 下载安装 ImageMagick, 选择合适您电脑的版本,我下载的是: ImageMagick-6.8.9-1-Q16-x86-dll.exe http://ww ...