首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LeetCode 0166 - Fraction to Recurring Decimal

LeetCode 0166 - Fraction to Recurring Decimal

作者头像
Reck Zhang
发布于 2021-08-11 06:51:55
发布于 2021-08-11 06:51:55
47700
代码可运行
举报
文章被收录于专栏:Reck ZhangReck Zhang
运行总次数:0
代码可运行

Fraction to Recurring Decimal

Desicription

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.

Example 1:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input: numerator = 1, denominator = 2
Output: "0.5"

Example 2:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input: numerator = 2, denominator = 1
Output: "2"

Example 3:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input: numerator = 2, denominator = 3
Output: "0.(6)"

Solution

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution {
public:
    string fractionToDecimal(long long numerator, long long denominator) {
        if(numerator == 0)
            return "0";
        string res;
        if(numerator < 0 ^ denominator < 0)
            res += "-";
        numerator = abs(numerator);
        denominator = abs(denominator);
        res += to_string(numerator / denominator);
        if(numerator % denominator == 0)
            return res;
        res += ".";
        unordered_map<long long, long long> mp;
        for(long long r = numerator % denominator; r; r %= denominator) {
            if(mp.count(r)) {
                res.insert(mp[r], 1, '(');
                res += ")";
                return res;
            }
            mp[r] = res.size();
            r *= 10;
            res += to_string(r / denominator);
        }
        return res;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-06-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Leetcode 166 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
triplebee
2018/01/12
6660
Leetcode: Fraction to Recurring Decimal
题目: Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.
卡尔曼和玻尔兹曼谁曼
2019/01/22
5980
Leetcode 题目解析之 Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.
ruochen
2022/01/10
1.3K0
LeetCode 166. Fraction to Recurring Decimal(模拟)
题意:给出一个分数的分子和分母,给出这个分数的小数形式的字符串模式。循环的部分用( 括上。
ShenduCC
2020/02/14
3590
LeetCode 166. 分数到小数(小数除法)
给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数。
Michael阿明
2020/07/13
1.5K0
LeetCode 166. 分数到小数(小数除法)
Leetcode 166 Fraction to Recurring Decimal
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/115703.html原文链接:https://javaforall.cn
全栈程序员站长
2022/07/10
6800
leetcode-166-分数到小数(用余数判断有没有出现小数的循环体)
给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数。
chenjx85
2018/09/29
3.2K0
2021-10-20:分数到小数。给定两个整数,分别表示分数的
2021-10-20:分数到小数。给定两个整数,分别表示分数的分子numerator和分母denominator,以字符串形式返回小数。如果小数部分为循环小数,则将循环的部分括在括号内。输入: numerator = 1, denominator = 2,输出: "0.5"。输入: numerator = 2, denominator = 3,输出: "0.(6)"。力扣166。
福大大架构师每日一题
2021/10/20
3050
LeetCode 0140 - Word Break II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences.
Reck Zhang
2021/08/11
2140
循环小数(Repeating Decimals)
The decimal expansion of the fraction 1/33 is 0.03, where the 03 is used to indicate that the cycle 03repeats indefinitely with no intervening digits. In fact, the decimal expansion of every rational number(fraction) has a repeating cycle as opposed to decimal expansions of irrational numbers, which have nosuch repeating cycles.
Vincent-yuan
2020/05/29
6850
循环小数(Repeating Decimals)
LeetCode 题目解答——155~226 题
[Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复杂度上根本就不是最优解,有的写的太啰嗦,有的则用了一些过于 tricky 的方法。我没有为了这个再更新,就让它们去吧。
四火
2022/07/19
7140
LeetCode 题目解答——155~226 题
算法细节系列(32):有趣的数学
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/72934759
用户1147447
2019/05/26
4310
LeetCode Weekly Contest 33解题思路
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/72597789
用户1147447
2019/05/26
4220
166. 分数到小数
给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数。
张伦聪zhangluncong
2022/10/26
4290
CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)
ACM思维题训练集合 To confuse the opponents, the Galactic Empire represents fractions in an unusual format. The fractions are represented as two sets of integers. The product of numbers from the first set gives the fraction numerator, the product of numbers from the second set gives the fraction denominator. However, it turned out that the programs that work with fractions in this representations aren’t complete, they lack supporting the operation of reducing fractions. Implement this operation and the Empire won’t forget you.
风骨散人Chiam
2020/11/03
4270
CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)
☆打卡算法☆LeetCode 166. 分数到小数 算法解析
给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以 字符串形式返回小数 。
恬静的小魔龙
2022/08/07
5200
☆打卡算法☆LeetCode 166. 分数到小数 算法解析
移除元素、分数到小数、整数转罗马数字
给你一个数组 nums_ 和一个值 val,你需要 原地 移除所有数值等于 val _的元素,并返回移除后数组的新长度。 不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地修改输入数组。 元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
共饮一杯无
2022/12/07
6560
LeetCode 0179 - Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.
Reck Zhang
2021/08/11
2140
LeetCode 0149 - Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
Reck Zhang
2021/08/11
2090
LeetCode 0022 - Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Reck Zhang
2021/08/11
2740
相关推荐
Leetcode 166 Fraction to Recurring Decimal
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验