Description:

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

Solution:

  long division: 长除法

  Trick: Determining whether two nums have different sign(+ or  -) can use follow codes:

if((n<)^(d<))  //异号
or
if((n>)^(d>)) //异号

  Above code avoiding product's value overflow.

Code:

class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
if(!numerator) return "";
long long n = numerator, d = denominator;
string ret = "";
if((n<)^(d<)) ret += '-';
if(n < ) n = -n;
if(d < ) d = -d;
ret += to_string(n/d);
if(n % d == ){
return ret;
}ret += '.';
map<long long, int>hash;
for(long long r = n % d; r; r %= d){
if(hash.find(r) != hash.end()){
ret.insert(hash[r],"(");
ret += ")";
return ret;
}
hash[r] = ret.size();
r *= ;
ret += to_string(r / d);
}
}
};

【Leetcode 166】 Fraction to Recurring Decimal的更多相关文章

  1. 【leetcode】Fraction to Recurring Decimal

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

  2. LeetCode(166) Fraction to Recurring Decimal

    题目 Given two integers representing the numerator and denominator of a fraction, return the fraction ...

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

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

  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 166. Fraction to Recurring Decimal 弗洛伊德判环

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

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

  8. 【LeetCode 229】Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  9. 【LeetCode练习题】Permutation Sequence

    Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...

随机推荐

  1. List lambda 排序

    Comparator<PromotionRule> comparator = Comparator.comparing(PromotionRule::getCreatedTime); pr ...

  2. 一、ECharts简介

    ECharts,缩写来自Enterprise Charts,商业级数据图表,一个纯Javascript的图表库,可以流畅的运行在PC和移动设备上,兼容当前绝大部分浏览器(IE6/7/8/9/10/11 ...

  3. node-sass 安装失败

    安装 npm install 时偶尔遇到报错:没有安装python或node-sass 安装失败的问题,百度之后发现是被墙了,但根据百度的方法换了淘宝镜像和用了vpn都安装失败, 原因可能是没有卸载之 ...

  4. 使用JdbcTemplate和JdbcDaoSupport

    [Spring对JDBC的支持] [JDBCTemplate简介] 1.为了是JDBC更加易于使用,Spring在JDBC API上定义了一个抽象层,以此建立一个JDBC存取框架. 2.作为Sprin ...

  5. [51Nod1089] 最长回文子串 V2(Manacher算法)

    1089 最长回文子串 V2(Manacher算法) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题   回文串是指aba.abba.cccbccc.aaaa这种左右对称 ...

  6. android Fragment用法

    Fragment常用的三个类:android.app.Fragment 主要用于定义Fragmentandroid.app.FragmentManager 主要用于在Activity中操作Fragme ...

  7. kendo grid 报错:length

    其实是:events中的{}Onsave的问题,把events整个注释掉就好了

  8. [bzoj3289]Mato的文件管理_莫队_树状数组

    Mato的文件管理 bzoj-3289 题目大意:给定一个n个数的序列.m次询问:一段区间中的逆序对个数. 注释:$1\le n\,mle 5\cdot 10^4$. 想法: 开始想这个题的大佬们,给 ...

  9. Eclipse错误出现:Unable to install breakpoint in... (未能解决)

    Unable to install breakpoint in... Eclipse Unable to install breakpoint in  的问题还是没解决 1.重装eclipse无效 2 ...

  10. postgresql 创建函数

    One of the most powerful features of PostgreSQL is its support for user-defined functions written in ...