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. 《DSP using MATLAB》Problem 4.5

    1. 2. 3. 5.不会

  2. test20181016 B君的第二题

    题意 分析 考场暴力50分. 考虑bfs序,一个点的儿子节点的bfs序一定连续,所以对bfs序建线段树,努力打一下就行了. 时间复杂度\(O(n \log n + m \log n)\) #inclu ...

  3. 红黑树-算法大神的博客-以及java多线程酷炫的知识

    http://www.cnblogs.com/skywang12345/p/3245399.html 解释第5条:从 ->根节点(或者任意个结点)到->所有的末端节点的路径中 ->黑 ...

  4. dbt 基本试用

    dbt 是一个很不错的进行etl 中的t 处理的工具,灵活简单,我们需要写的就是select 语句 dbt 帮助我们进行处理 测试集成了graphql 以及使用docker 运行 安装 pip ins ...

  5. nyoj 数独

    数独 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 数独是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一 ...

  6. mac上安装nginx

    终端执行: brew install nginx nginx 默认安装在 /usr/local/Cellar/nginx/1.12.2 conf 文件默认安装在 /usr/local/etc/ngin ...

  7. django 使用Ajax方式POST JSON数据包

    示例1: js: function SaveAction(){ //点击 保存按键 var senddata = {"type":"A", "host ...

  8. button使用注意

    <button type="button" class="btn btn-primary" onclick="ChangePassword(); ...

  9. postman环境变量的设置

    相同的api接口因为部署环境不同,分为test和fromal 不仅重复,还容易出错 下面来介绍一下Postman的一个小技巧来解决这种问题: 设置环境变量 Tips: 不是在OS中设置环境变量哦   ...

  10. php函数的实现

    1.函数     汇编中函数对应的是一组独立的汇编指令,然后通过call指令实现函数的调用.PHP编译的opcode数组,与汇编指令对应. PHP用户自定义函数的实现就是将函数编译为独立的opcode ...