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. shrine

    0x01 import flask import os app = flask.Flask(__name__) app.config['FLAG'] = os.environ.pop('FLAG') ...

  2. 用项目强化你的webpack

    用你的webpack实现vue-cli 本文围绕前端工程化,用webpack从零搭建一个完整项目的过程 本文核心知识点: webpack的使用 vue组件化思想 Element-UI的使用 别走别走, ...

  3. 2018-06-17 js数组

    数组的定义:① var arr=new Array(xx,xx,xx); ②var arr=[yy,yy,,yy]; 数组的查看:arr[x]; 数组的修改:arr[x]=xx; 数组的遍历:①for ...

  4. 萌新带你开车上p站(终极番外)

    本文由“合天智汇”公众号首发,作者:萌新 0x01前言 这关其实和pwn关系不大,主要考察的都是linux下一些函数的操作,考察linux的基本功.涉及到的知识点包括一些经典的函数原型.IO重定向.文 ...

  5. angular2 + bootstrap +jquery 实例

    一.Create a project process: 1.use Angular CLI to create an Angular Project "demo": need th ...

  6. 9.5 Go 依赖管理

    9.5 Go 依赖管理 godep是解决包依赖的管理工具,目前最主流的一种,原理是扫描记录版本控制的信息. A. 所有的第三方包都放在$GOPATH的src目录下. B. 如果不同程序依赖的版本不一样 ...

  7. linux常用命令---文件软硬链接

    文件链接

  8. MyCat水平分库

    一.什么是水平分库 将一张表水平切分到多个库中 1.1分片原则 1.需要分片的表是少数的 2.能不切分尽量不要切分 3.日志表可以采取归档方式 4.选择合适的切分规则和分片建,确保数据分片均匀,否则依 ...

  9. poj3635 优先队列+打标记+广搜

    After going through the receipts from your car trip through Europe this summer, you realised that th ...

  10. 第几天?(hdu2005)

    第几天那个代码模板可以保存起来. #include<stdio.h> #include<math.h> #define PI 3.1415927 using namespace ...