Leetcode 166. Fraction to Recurring Decimal
来源:互联网 发布:移动光纤网络机房在哪 编辑:程序博客网 时间:2024/06/13 20:28
public class Solution { public String fractionToDecimal(int numerator, int denominator) { // determin the sign first // careful of the case n=0, d>0, so return 0 when numerator equals to 0 if (numerator == 0) return "0"; StringBuilder ret = new StringBuilder(); if (numerator>0 ^ denominator>0) ret.append("-"); // calculate the remainder long n = new Long(numerator); long d = new Long(denominator); n = Math.abs(n); d = Math.abs(d); ret.append(n / d); long remainder = n % d; if (remainder == 0) return ret.toString(); // calculate the decimal part // need a hashset to save decimal, if meets a duplicate number return (...) HashMap<Long, Integer> map = new HashMap<Long, Integer>(); ret.append("."); while (remainder != 0) { if (map.containsKey(remainder)) { ret.insert(map.get(remainder), "("); ret.append(")"); return ret.toString(); } map.put(remainder, ret.length()); n = remainder * 10; long quotient = n / d; remainder = n % d; ret.append(quotient); } return ret.toString(); }}
0 0
- 166. Fraction to Recurring Decimal Leetcode Python
- Leetcode-166.Fraction to Recurring Decimal
- [Leetcode]166. Fraction to Recurring Decimal @python
- [leetcode] 166. Fraction to Recurring Decimal
- leetcode 166. Fraction to Recurring Decimal
- [LeetCode]166. Fraction to Recurring Decimal
- LeetCode *** 166. Fraction to Recurring Decimal
- LeetCode 166. Fraction to Recurring Decimal
- [LeetCode]problem 166. Fraction to Recurring Decimal
- leetcode-java-166. Fraction to Recurring Decimal
- LeetCode 166. Fraction to Recurring Decimal
- [LeetCode] 166. Fraction to Recurring Decimal
- Leetcode-166. Fraction to Recurring Decimal
- leetcode 166. Fraction to Recurring Decimal
- Leetcode 166. Fraction to Recurring Decimal
- LeetCode 166. Fraction to Recurring Decimal
- Leetcode 166. Fraction to Recurring Decimal
- leetCode 166. Fraction to Recurring Decimal
- Git和Github的基本使用
- sql server 2008 r2中用alter修改表结构---给属性加上unique约束
- 常用Linux操作
- 15My3.0版本聊天系统(mybatis集成)
- php的Generator生成器及yield
- Leetcode 166. Fraction to Recurring Decimal
- CSS基础入门3
- 1. Two Sum
- Managed Server Independence
- centos升级openssh的两种方式
- c语言中数组名代表数组首地址,它的值在运行期间可以改变吗?
- 关于jQuery的$.getJSON乱码问题
- Easyui笔记2:实现datagrid多行删除
- RXjava的简单介绍 请求网络的Demo menglong0329