题目

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. NET Core+MySql+Nginx

    NET Core+MySql+Nginx 容器化部署 .NET Core容器化@Docker.NET Core容器化之多容器应用部署@Docker-Compose.NET Core+MySql+Ngi ...

  2. Codeforces Round #377 (Div. 2) 被坑了

    http://codeforces.com/contest/732/problem/B 题目要求任意两个连续的日子都要 >= k 那么如果a[1] + a[2] < k,就要把a[2]加上 ...

  3. [干货分享]AXURE整套高保真UI框架和元件组(白色风格)

      写在前面 强烈建议开始之前阅读以下第一篇高保真UI框架的前面部分,以了解设计思想,这篇文章不再重复介绍: AXURE-整套可复用的高保真元件和框架之暗黑风格 本次共享模板的UI规范 注:由于篇幅问 ...

  4. Windows2

    windows如何打开dvd, iso镜像文件 .iso后缀的文件是一个压缩文件, 使用Winrar等压缩工具即可打开 windows7如何下载Visual Studio 2010(2010是流行的开 ...

  5. UI2_异步下载

    // AppDelegate.m // UI2_异步下载 // // Created by zhangxueming on 15/7/17. // Copyright (c) 2015年 zhangx ...

  6. css3动画:animation

    例: -webkit-animation: myfirst 5s linear 2s infinite alternate; animation: myfirst 5s linear 2s infin ...

  7. 《超实用的Node.js代码段》连载三:Node.js深受欢迎的六大原因

    <超实用的Node.js代码段>连载一:获取Buffer对象字节长度 <超实用的Node.js代码段>连载二:正确拼接Buffer Node.js是一种后起的优秀服务器编程语言 ...

  8. Kendo 单页面应用(一)概述

    Kendo 单页面应用(一)概述 Kendo 单页面应用(Single-Page Application,缩写为 SPA)定义了一组类用于简化 Web 应用(Rich Client)开发,最常见的单页 ...

  9. UIButton 图片文字位置

    在实际开发过程中经常在按钮上添加文字和图片,位置和图片的位置根据需求放置也是不一样的.下面实现了各种显示方式,如下图: UIButton+LSAdditions.h // // UIButton+LS ...

  10. Elasticsearch-分片原理2

    Elasticsearch版本:6.0 一.Elasticsearch计算分片位置的公式 shard = hash(routing) % number_of_primary_shards 解释:rou ...