public class Solution {
public String fractionToDecimal(int numerator, int denominator) {
HashMap<Long,Integer> maps = new HashMap<>();//store divid number
List<Long> number = new ArrayList<>();
int index = 0;
int beginIndex = -1; StringBuilder builder = new StringBuilder();
if(denominator==0)
return "";
long num = numerator;
long denum = denominator;
if((num<0 && denum>0) || (num>0 && denum<0))
builder.append('-'); num = Math.abs(num);
denum = Math.abs(denum); long val = num/denum;
builder.append(String.valueOf(val));
num = (num%denum)*10; while(num!=0){
if(maps.containsKey(num)){//开始重复
beginIndex = maps.get(num);
break;
}else{ maps.put(num, index++);
val = num/denum;
num = (num%denum)*10;
number.add(val);
}
}
for(int i = 0;i<index;i++){
if(i==0)
builder.append('.');
if(i==beginIndex){
builder.append('(');
}
builder.append(number.get(i));
}
if(beginIndex!=-1)
builder.append(')'); return builder.toString();
}
}

leetcode166的更多相关文章

  1. [Swift]LeetCode166. 分数到小数 | Fraction to Recurring Decimal

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  2. leetcode166 Fraction to Recurring Decimal

    思路: 模拟. 实现: class Solution { public: string fractionToDecimal(int numerator, int denominator) { long ...

  3. leetcode-166周赛-5282-转化为全0矩阵的最小反转次数

    题目描述: 方法一:暴力BFS class Solution: def minFlips(self, mat) -> int: R, C = len(mat), len(mat[0]) def ...

  4. leetcode-166周赛-5280-用户分组

    题目描述: 自己的提交: class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: ...

  5. Leetcode166. Fraction to Recurring Decimal分数到小数

    给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数. 如果小数部分为循环小数,则将循环的部分括在括号内. 示例 1: 输入: numerator ...

随机推荐

  1. git代码回退

    情况1.还没有push可能 git add ,commit以后发现代码有点问题,想取消提交,用: reset git reset [--soft | --mixed | --hard] eg:  gi ...

  2. MySQL--DROP TABLE与MySQL版本

    ======================================================================== DROP TABLE与MySQL版本 MySQL在5. ...

  3. .NET基金会成立

    作者 Jonathan Allen ,译者 臧秀涛 发布于 2014年4月8日 随着.NET基金会的成立,微软在成为主要的开源参与者的道路上又前进了一步.该基金会的宗旨是“成为越来越多的开源.NET项 ...

  4. Generator 知识点

    Generator 函数的执行过程,其实是将同一个回调函数,反复传入 next 方法的 value 属性. Iterator 接口的 next 方法必须是同步的,只要调用就必须立刻返回值.也就是说,一 ...

  5. golang fmt用法举例

    下标与参数的对应 例子如下: package main import ( "fmt" ) func main() { num := 10 fmt.Printf("num: ...

  6. 打印时报emSize必须大于0

    Value of '0' is not valid for 'emSize','emSize' should be greater than 0 and less than or equal to S ...

  7. springMVC学习(11)-json数据交互和RESTful支持

    一.json数据交互: json数据格式在接口调用中.html页面中较常用,json格式比较简单,解析还比较方便. 比如:webservice接口,传输json数据. springMVC进行json交 ...

  8. Bootstrap:百科

    ylbtech-Bootstrap:百科 Bootstrap (Web框架) Bootstrap,来自 Twitter,是目前很受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.Java ...

  9. 杂项:Mantis

    ylbtech-杂项:Mantis 缺陷管理平台Mantis,也做MantisBT,全称Mantis Bug Tracker.Mantis是一个基于PHP技术的轻量级的开源缺陷跟踪系统,以Web操作的 ...

  10. [C#]泛型约束在窗体定义上的使用

    查相关资料查的一团乱,自己瞎写了几次以后误打误撞成功了: namespace Test1 { public partial class Form1<T> : Form { // ... F ...