166. 分数到小数

给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数。

如果小数部分为循环小数,则将循环的部分括在括号内。

示例 1:

输入: numerator = 1, denominator = 2

输出: “0.5”

示例 2:

输入: numerator = 2, denominator = 1

输出: “2”

示例 3:

输入: numerator = 2, denominator = 3

输出: “0.(6)”

class Solution {
public String fractionToDecimal(int numerator, int denominator) {
if (numerator == 0 || denominator == 0) return "0";
int sign = 1;
if (numerator > 0 && denominator < 0) sign = -1;
long big = (long) numerator / (long) denominator;
long small = numerator % denominator;
StringBuilder result = new StringBuilder(String.valueOf(big));
if (sign == -1) result.insert(0, "-");
if (small != 0) {
result.append(".");
StringBuilder smallStr = new StringBuilder();
Map<String, Integer> smallIndexs = new HashMap<String, Integer>();
while (small != 0) {
small *= 10;
big = small / denominator;
small = small % denominator;
String str = small + "_" + big;
if (smallIndexs.containsKey(str)) {
smallStr.append(")");
smallStr.insert(smallIndexs.get(str), "(");
break;
} else {
smallIndexs.put(str, smallStr.length());
smallStr.append(Math.abs(big));
}
}
result.append(smallStr);
}
return result.toString();
}
}

Java实现 LeetCode 166 分数到小数的更多相关文章

  1. Leetcode 166.分数到小数

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

  2. leetcode 166分数到小数

    手动排除特殊情况: 对于一般情况,使用位运算和加减法来计算除法,使用sign记录结果符号:(这部分为leetcode 29题的答案) 使用hashmap来记录循环体出现的开始位置(如果有的话),使用f ...

  3. Java for LeetCode 166 Fraction to Recurring Decimal

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

  4. Java实现 LeetCode 592 分数加减运算(纯体力活)

    592. 分数加减运算 给定一个表示分数加减运算表达式的字符串,你需要返回一个字符串形式的计算结果. 这个结果应该是不可约分的分数,即最简分数. 如果最终结果是一个整数,例如 2,你需要将它转换成分数 ...

  5. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  9. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

随机推荐

  1. python语法学习第九天--else和with语句

    else: while/for else:正常执行完循环(非break)执行else中代码 try else:未捕捉到异常,执行else中代码 with: 语法格式: with open('666.t ...

  2. 写ssm项目的注意点

    注意事项: 输出台乱码 a链接以post提交 表单提交前验证 onsubmit 属性在提交表单时触发. onsubmit 属性只在 中使用. <form action="/demo/d ...

  3. xilinx VDMA IP核使用

    VDMA实用配置说明 VDMA是通过AXI Stream协议对视频数据在PS与PL端进行搬运,开发者无需关注AXI Stream协议,在BlockDesign设计中只需要把相应信号进行连接即可. VD ...

  4. box-sizing 可以使border padding不影响设置的盒子尺寸

  5. 前端面试题-几个很实用的BOM属性对象方法?

    什么是Bom? Bom是浏览器对象.有哪些常用的Bom属性呢? (1)location对象 location.href-- 返回或设置当前文档的URL location.search -- 返回URL ...

  6. ABAP基础4:模块化

    子程序定义 以form开始,以endform结束,用perform语句调用,from语句可以在程序内部/外部,perform一定要写在前面 perform. from. 子程序模块 endform. ...

  7. MVC4.0 上传文件

    Views/Import/ImportExcel.cshtml @{ Layout = null;} <!DOCTYPE html> <html><head> &l ...

  8. 基于elementUI使用v-model实现经纬度输入的vue组件

    绑定一个 [12.34,-45.67] (东经西经,南纬北纬 正负表示) 形式的经纬度数组,能够按度分秒进行编辑,效果如下所示,点击东经,北纬可切换. 经纬度的 度转度分秒 能够获取度分秒格式数据 C ...

  9. 笔记二(JavaWeb)

    上一个笔记写的好累,这次换Markdown试试 缺省适配器设计模式:父类不实现该方法,让子类去实现(抽象方法) 模板方法设计模式:定义一个操作中的方法骨架,而将一些步骤延迟到子类中.模板方法使得子类可 ...

  10. SecureCRT 简单运用

    1.想要从本地电脑将文件通过secureCRT传输到远程服务器如下目录 2.在SecureCRT界面中使用快捷键[Alt+P] 打开了SFTP的界面 3.找到你要传的文件的地址[E:\a.zip] 4 ...