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. 24款WordPress网站AI插件大盘点

    ------------恢复内容开始------------ 你想把AI(人工智能)技术和机器学习技术添加到自己的WordPress网站吗?本文中,我会分享24个利用AI技术和机器学习技术的WordP ...

  2. idea 2020 配置本地 Maven 仓库

    问题: 默认Maven 仓库地址在C盘,C盘是系统盘能少放东西尽量少放. 只需要简单的两步 1.File~Settings 然后搜索 maven 如下图绿框 修改成你自己的 Maven 仓库 2.Fi ...

  3. JDBC12 ORM01 Object[]存放一条记录

    ORM(Object Relationship Mapping)的基本思想 -表结构跟类对应:表中的字段和类的属性对应:表中记录和对象对应 让JavaBean的属性名和类型尽量和数据库保持一致 一条记 ...

  4. ASP.NET Core Blazor 初探之 Blazor Server

    上周初步对Blazor WebAssembly进行了初步的探索(ASP.NET Core Blazor 初探之 Blazor WebAssembly).这次来看看Blazor Server该怎么玩. ...

  5. struts2 redirectaction

    <result type="redirectAction"> <param name="actionName">dashboard< ...

  6. 关于 Blob

    博客地址:https://ainyi.com/88 对于 Blob,前端开发中可能比较少遇到:数据库中可使用 Blob 概念,例如 Mysql 存储二进制数据的类型就是 Blob,也就是说图片可存储于 ...

  7. 【雕爷学编程】零基础Python(01)---“投机取巧”的三条途径

    从3月13日报名尝试上网课学习(4天课8.9元),开始接触Python(中文发音“派森”),到今天有一星期了.这两天广泛搜索了一下相关的学习途径,本着“投机取巧”的出发点,居然小有心得,这里一并分享出 ...

  8. day05:数组与字典常识(20170217)

    #1:数组功能的使用:print ("a4A".isdecimal()) #print ("18".isdigit()) #判断是否是数字print (&quo ...

  9. asp.net MVC3.0 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction

    asp.net MVC3.0 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction 1.带有Render的方法返回值是v ...

  10. elment新增el-select的全选功能

    不废话,效果如图 代码实现 平生不爱啰嗦,功能如上已实现.