LeetCode Fraction to Recurring Decimal
原题链接在这里:https://leetcode.com/problems/fraction-to-recurring-decimal/
题目:
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.
If the fractional part is repeating, enclose the repeating part in parentheses.
Example 1:
Input: numerator = 1, denominator = 2
Output: "0.5"
Example 2:
Input: numerator = 2, denominator = 1
Output: "2"
Example 3:
Input: numerator = 2, denominator = 3
Output: "0.(6)"
题解:
加入整数部分后,在reminder 不为0的时候添加小数部分,用HashMap来维护reminder 和 对应的小数长度. 若是出现无限循环可以找到加左括号的起始点.
AC Java:
public class Solution {
public String fractionToDecimal(int numerator, int denominator) {
if(numerator == 0){
return "0";
}
if(denominator == 0){
return "";
} StringBuilder res = new StringBuilder();
long num = Math.abs((long)numerator);
long deno = Math.abs((long)denominator); //如果是负数
if((numerator < 0) ^ (denominator < 0)){
res.append("-");
} res.append(num/deno);
long reminder = num%deno;
if(reminder == 0){
return res.toString();
} //有小数
res.append(".");
//HashMap中计入reminder 和 对应的小数部分长度,用来找到循环开始的位置
HashMap<Long, Integer> hm = new HashMap<Long, Integer>();
StringBuilder desRes = new StringBuilder();
while(reminder != 0){
if(hm.containsKey(reminder)){ //出现了循环,找到循环开始的index添加左括号
int beginIndex = hm.get(reminder);
desRes.insert(beginIndex, "(");
desRes.append(")");
res.append(desRes);
return res.toString();
}
hm.put(reminder, desRes.length());
desRes.append(reminder*10/deno);
reminder = (reminder*10)%deno;
}
res.append(desRes);
return res.toString();
}
}
与Decimal To Fraction 小数转换成分数类似.
LeetCode Fraction to Recurring Decimal的更多相关文章
- [LeetCode] Fraction to Recurring Decimal 分数转循环小数
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- [LeetCode] Fraction to Recurring Decimal 哈希表
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal
1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- 【leetcode】Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...
- 【LeetCode】166. Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 【刷题-LeetCode】166 Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- [LeetCode#116]Fraction to Recurring Decimal
Problem: Given two integers representing the numerator and denominator of a fraction, return the fra ...
随机推荐
- 解决EasyUI-Datagrid和LinqToEntity结合应用时排序问题
我们在做WEB页面时,时常会选择JQuery框架的Datagrid,如Ext.EasyUI.Flexigrid,数据访问则采用LinqToSQL或LinqToEntity.UI用Jquery框架的目的 ...
- SQL中distinct的用法(转自博主:Rain Man)
在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值.关键词 distinct用于返回唯一不同的值. 表A: 示例1 select distinct nam ...
- 用wampserver 装的集成环境,命令行进不去提示mysql
命令行进不去提示mysql 不是内部命令或外部命令. 解决办法,就是将mysql/bin路径加到path中去
- Linux环境下实现生产者消费者问题
#include <stdio.h> #include <semaphore.h> #include <stdlib.h> #include <pthread ...
- Twemproxy 介绍与使用
Twemproxy是一种代理分片机制,由Twitter开源.Twemproxy作为代理,可接受来自多个程序的访问,按照路由规则,转发给后台的各个Redis服务器,再原路返回.该方案很好的解决了单个Re ...
- fastjson 常用api
一.json字符串的数据解析 1.json字符串 ---> JSONObject或者JSONArray[好处就是当你没有bean的model类时,可以直接获取相关数据] ...
- 持续集成篇_05_SonarQube代码质量管理平台的介绍与安装
1.SonarQube的介绍 SonarQube是一个管理代码质量的开放平台. 可以从七个维度检测代码质量(为什么要用SonarQube): (1)复杂度分布(complexity):代码复杂度过高将 ...
- 项目部署到Tomat报错:jar not loaded.See Servlet Spec 2.3, section 9.7.2. Offending
项目部署到Tomcat报这样的异常: Java代码 jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: ja ...
- StringUtils cannot be resolved
I am using Java server pages and for using String Manipulations and i am Using StringUtils which i a ...
- UVALive 7297 bfs
题意 一个小偷偷到了项链 他想知道自己是否可以逃出去 地图中有一个小偷 一个警察 警察有一条狗 一开始 小偷和警察的移动速度都是1 当警察走到小偷经过过的地方时 警察会有一条狗嗅到小偷的气味并且以2的 ...