166 Fraction to Recurring Decimal 分数到小数
给定两个整数,分别表示分数的分子和分母,返回字符串格式的小数。
如果小数部分为循环小数,则将重复部分括在括号内。
例如,
给出 分子 = 1, 分母 = 2,返回 "0.5".
给出 分子 = 2, 分母 = 1,返回 "2".
给出 分子 = 2, 分母 = 3,返回 "0.(6)".
详见:https://leetcode.com/problems/fraction-to-recurring-decimal/description/
Java实现:
class Solution {
public String fractionToDecimal(int numerator, int denominator) {
if(numerator == 0){
return "0";
}
if(denominator == 0){
return "";
}
String res = "";
//判断结果是否为负数,加负号
if((numerator<0) ^ (denominator<0)){
res += "-";
}
//保证分子分母都为正数,防止取绝对值溢出,先将int转换为long,再取绝对值
long num = numerator;
long den = denominator;
num = Math.abs(num);
den = Math.abs(den);
//得到结果的整数部分
long numInt = num/den;
res += String.valueOf(numInt);
//判断是否能整除,如果能,则直接返回结果
long number = (num%den)*10;
if(number==0){
return res;
}
//结果的小数部分:使用map记录每次操作之后的余数和对应的下标,HashMap的<key, value>分别对应<当前余数, 对应结果的下标>,当出现重复值的时候,可以通过对应下标寻找到重复部分。
HashMap<Long, Integer> map = new HashMap<Long, Integer>();
res += ".";
while(number!=0){
//判断map中是否出现过该余数,如果出现过则开始循环
if(map.containsKey(number)){
int beg = map.get(number); //循环体开始的位置
String part1 = res.substring(0, beg);
String part2 = res.substring(beg, res.length());
res = part1 + "(" + part2 + ")";
return res;
}
//继续下除
map.put(number, res.length());
numInt = number / den;
res += String.valueOf(numInt);
number = (number%den) * 10;
}
return res;
}
}
详见:https://www.cnblogs.com/grandyang/p/4238577.html
166 Fraction to Recurring Decimal 分数到小数的更多相关文章
- ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Leetcode166. Fraction to Recurring Decimal分数到小数
给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数. 如果小数部分为循环小数,则将循环的部分括在括号内. 示例 1: 输入: numerator ...
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- 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] Fraction to Recurring Decimal 分数转循环小数
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Java for LeetCode 166 Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- [LeetCode] 167. Fraction to Recurring Decimal 分数转循环小数
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
随机推荐
- 基于第三方微信授权登录的iOS代码分析
本文转载至 http://www.cocoachina.com/ios/20140922/9715.html 微信已经深入到每一个APP的缝隙,最常用的莫过分享和登录了,接下来就以代码的形式来展开微信 ...
- Cocos2d-js异步图片加载
这里说的是在需要的使用加载图片,比如游戏中的某个关卡的图片,不用在游戏一开始就加载(万一用户玩不到那关,岂不是很冤,流量费了那么多),否则 载入速度也慢.这种方式加载资源要用到cc.loader官方文 ...
- [Silverlight 2.0 控制物体绕圆弧运行(C#初探篇)]
我自己写的第一个 Silverlight 2.0 程序 [Silverlight 2.0 控制物体绕圆弧运行(C#初探篇)] 程序运行时:小地球将绕着圆形轨迹做圆周运动. ...
- 转:zip版Tomcat(6,7)不能注册成windows服务或者注册后不能启动的问题解决
源地址:http://www.iflym.com/index.php/code/zip-tomcat67-can-not-register-windows-services-resolve.html ...
- Python(1)(安装与基本使用)
1.Python的下载和安装我就不废话了,百度上都有. 我安装的是Python 3.4.3 64bit 安装完之后,打开Cmd,输入Python 显示以上相同,按照百度的意思就是安装成功. 2.配置环 ...
- Python里的一些注释规范
写代码注释是一件很重要的事情,如果你写的一段函数给别人调用那么往往都需要配上一些基本的注释.写好代码可以让别人容易阅读你的代码.试想一 下:如果你在github上面找到一段你想要的代码,这段代码有20 ...
- 「NOIP2014」「LuoguP2296」 寻找道路
Description 在有向图 G 中,每条边的长度均为 1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 路径上的所有点的出边所指向的点都直接或间接与终点连通. 在 ...
- Bug: CCScheduler#scheduleSelector. Selector already scheduled. Updating interval from: 0.0000 to 0.0000
原因是当前的scheduleOnce还没有执行完成, 可以将scheduleOnce方法改写成另外一种形式,把CCDelayTime和CCCallFunc拼接构造延迟事件调用: CCDelayTime ...
- 【旧文章搬运】Windows内核常见数据结构(进程相关)
原文发表于百度空间,2008-7-24========================================================================== 进程的相关结 ...
- pair运用
#include <iostream> #include <string> #include <map> #include <algorithm> us ...