[leedcode 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 "0.5".
  • Given numerator = 2, denominator = 1, return "2".
  • Given numerator = 2, denominator = 3, return "0.(6)".

    public class Solution {
        public String fractionToDecimal(int numerator, int denominator) {
            //注意正负
            //注意越界(转为long)
            //使用map记录余数和位置,当发现相同的余数时,则表示此余数对应位置的商位开始循环,此时需要用()包装
            //seq表示商
            long num=Math.abs((long)numerator);
            long deno=Math.abs((long)denominator);
            int flag=1;
            if(numerator>0&&denominator<0||numerator<0&&denominator>0) flag=0;
            long s=num/deno;
            long t=num%deno;
            String res=flag==0?"-"+Long.toString(s):Long.toString(s);
            if(t==0) return res;
            else return res+"."+getRes(t,deno);
    
        }
        public StringBuilder getRes(long t,long deno){
            StringBuilder seq=new StringBuilder();
            HashMap<Long,Integer> map=new HashMap<Long,Integer>();
            int i=0;
            while(t!=0&&!map.containsKey(t)){
                map.put(t,i);
                i++;
                t*=10;
                seq.append(Long.toString(t/deno));
                t%=deno;
    
            }
            if(t!=0){
                seq.insert((int)map.get(t),‘(‘);
                seq.append(‘)‘);
            }
            return seq;
        }
    }
时间: 2024-11-09 06:20:39

[leedcode 166] Fraction to Recurring Decimal的相关文章

Java for 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, retu

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, retu

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, retu

[leetcode] 166. Fraction to Recurring Decimal 解题报告

题目链接: https://leetcode.com/problems/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 p

leetcode[166] Fraction to Recurring Decimal

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)". 按照上例子实现结果.主要就是如何处理循环的数字. 我们发现如果循环了,那么余数也是出现了循环,所以从余数入手,一

166 Fraction to Recurring Decimal 分数到小数

给定两个整数,分别表示分数的分子和分母,返回字符串格式的小数.如果小数部分为循环小数,则将重复部分括在括号内.例如,    给出 分子 = 1, 分母 = 2,返回 "0.5".    给出 分子 = 2, 分母 = 1,返回 "2".    给出 分子 = 2, 分母 = 3,返回 "0.(6)". 详见:https://leetcode.com/problems/fraction-to-recurring-decimal/descriptio

LeetCode 166. Fraction to Recurring Decimal(模拟)

题目 题意:给出一个分数的分子和分母,给出这个分数的小数形式的字符串模式.循环的部分用( 括上. 题解:模拟除法,判断循环体. class Solution { public: map<int,int> m; string fractionToDecimal(int numerator, int denominator) { long long int numerator_ = numerator; long long int denominator_ = denominator; int s

【LeetCode】Fraction to Recurring Decimal

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 numera

【Leetcode 166】 Fraction to Recurring Decimal

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, denomina