zoukankan      html  css  js  c++  java
  • 166. Fraction to Recurring Decimal

    package LeetCode_166
    
    /**
     * 166. Fraction to Recurring Decimal
     * https://leetcode.com/problems/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.
    
    Example 1:
    Input: numerator = 1, denominator = 2
    Output: "0.5"
    
    Example 2:
    Input: numerator = 2, denominator = 1
    Output: "2"
    
    Example 3:
    Input: numerator = 2, denominator = 3
    Output: "0.(6)"
     * */
    class Solution {
        fun fractionToDecimal(numerator: Int, denominator: Int): String {
            if (numerator == 0) {
                return "0"
            }
            if (denominator == 0) {
                return ""
            }
            val fractionSb = StringBuilder()
            //check if need add '-' in first
            if (numerator < 0 && denominator > 0 || numerator > 0 && denominator < 0) {
                fractionSb.append("-")
            }
            val num = Math.abs(numerator.toLong())
            val den = Math.abs(denominator.toLong())
            var result = num / den
            fractionSb.append(result.toString())
            //get the remainder
            var remainder = num % den
            if (remainder == 0L) {
                //if divisible
                return fractionSb.toString()
            }
            fractionSb.append(".")
            //use map to help, key is remainder, value is result length
            val map = HashMap<Long, Int>()
            while (remainder != 0L) {
                val curString = fractionSb.toString()
                println(map)
                if (map.containsKey(remainder)) {
                    val begin = map.get(remainder) ?: 0
                    val part1 = curString.substring(0, begin)
                    val part2 = curString.substring(begin, curString.length)
                    return part1 + "(" + part2 + ")"
                }
                map.put(remainder, curString.length)
                //because we need to get every decimal, so *10 and do again
                remainder *= 10
                result = remainder / den
                fractionSb.append(result.toString())
                //continue to get the new remainder
                remainder %= den
            }
            return fractionSb.toString()
        }
    }
  • 相关阅读:
    危机下,你还敢提加薪吗?
    大白兔奶糖三聚氰胺事件后21日起重新上架
    15个nosql数据库
    向网页设计师推荐15个很棒的网站
    腾讯新浪通过IP地址获取当前地理位置(省份)的接口
    5个最顶级jQuery图表类库插件Charting plugin
    12种JavaScript MVC框架之比较
    企业网站设计的启示
    游戏引擎大全
    推荐几份能够帮助你学习 CSS3 的实用帮助手册
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13296677.html
Copyright © 2011-2022 走看看