• 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)) ? "-" : "
  • 相关阅读:
    Android 之 进度条样式
    最佳下载实例
    最佳下载实例
    使用git将项目上传到github(最简单方法)
    使用git将项目上传到github(最简单方法)
    打开URL启动android默认浏览器,启动指定浏览器
    打开URL启动android默认浏览器,启动指定浏览器
    android指定浏览器打开特定网页
    【POJ】[3026]Borg Maze
    【POJ】[3026]Borg Maze
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4371962.html
Copyright © 2020-2023  润新知