zoukankan      html  css  js  c++  java
  • 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.

    思路:

      模拟除法

      判断循环节的时候用hashtable

    我的代码:

    import java.util.Hashtable;
    public class Solution {
        public String fractionToDecimal(int numerator, int denominator) {
            if(denominator == 0 || numerator == 0 )    return "0";
            int cnt = 0;
            if(numerator < 0)
            {
                cnt++;
            }
            if(denominator < 0)
            {
                cnt++;
            }
            long num = Math.abs((long)numerator);
            long den = Math.abs((long)denominator);
            StringBuffer left = new StringBuffer();
            long remain = num%den;
            num /= den;
            left.append(num);
            if(remain == 0) 
            {
                return cnt == 1 ? "-"+left.toString() : left.toString();
            }
            StringBuffer right = new StringBuffer();
            Hashtable<Long,Integer> ht = new Hashtable<Long,Integer>();
            num = remain;
            ht.put(num, 0);
            int index = 1;
            while(num != 0)
            {
                num *= 10; 
                remain = num%den;
                num /= den;
                right.append(num);
                if(ht.containsKey(remain))
                {
                    right.insert(ht.get(remain),"(");
                    right.append(')');
                    break;
                }
                ht.put(remain,index);
                num = remain;
                index++;
            }
            String rst = left.toString() + "." + right.toString();
            return cnt == 1 ? "-"+rst : rst;
        }
    }
    View Code

    他人代码:

    public class Solution {
        public String fractionToDecimal(int numerator, int denominator) {
            if (numerator == 0) {
                return "0";
            }
            StringBuilder res = new StringBuilder();
            // "+" or "-"
            res.append(((numerator > 0) ^ (denominator > 0)) ? "-" : "");
            long num = Math.abs((long)numerator);
            long den = Math.abs((long)denominator);
    
            // integral part
            res.append(num / den);
            num %= den;
            if (num == 0) {
                return res.toString();
            }
    
            // fractional part
            res.append(".");
            HashMap<Long, Integer> map = new HashMap<Long, Integer>();
            map.put(num, res.length());
            while (num != 0) {
                num *= 10;
                res.append(num / den);
                num %= den;
                if (map.containsKey(num)) {
                    int index = map.get(num);
                    res.insert(index, "(");
                    res.append(")");
                    break;
                }
                else {
                    map.put(num, res.length());
                }
            }
            return res.toString();
        }
    }
    View Code

    学习之处:

    • 这道题的思路很简单,看到题的那一刻基本上也知道怎么做了,但是需要考虑的corner case 特别多
      • 分母为0 分子为0
      • 永远记住对于int而言负数转换成正数的时候,需要用long进行存储,因为会溢出啊!!
      • 循环节问题 仔细分析 1/6 和 2/3 这两种情况就可以了
    • 判断乘积或者除法的正负号的时候,用这种方法真简洁 (numerator > 0) ^ (denominator > 0)) ? "-" : "
  • 相关阅读:
    [Java] Hibernate
    python基础(十三):函数(一)公共操作
    python基础(十二):数据结构(五)集合
    python基础(十二):数据结构(四)字典
    python基础(十一):数据结构(三)元组
    python基础(十):数据结构(二)列表
    python基础(八):流程控制(五)循环
    python基础(七):流程控制(一)if
    python基础(六):运算符
    python基础(五):转换数据类型
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4371962.html
Copyright © 2011-2022 走看看