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 ...
随机推荐
- 设计模式学习笔记——Mediator中介者模式
将众多对象之间的网状关系转为全部通过一个中间对象间接发生关系,此中间对象为中介者. 看图最直观: 作用不言而喻,就是降低对象之间的耦合度,乃至降低了整个系统的复杂度. 有点象代理模式,更象外观模式:
- Web 监听器
什么事web 监听器? Servlet规范中定义的一种特殊类 用于监听ServletContext.HttpSession和ServletRequest等象的创建与销毁的事件 用监听域对象的属性发生修 ...
- 图像物体检測识别中的LBP特征
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xinzhangyanxiang/article/details/37317863 图像物体检測识别中 ...
- MYSQL进阶学习笔记十:MySQL慢查询!(视频序号:进阶_23-24)
知识点十一:MySQL 慢查询的应用(23) 一.慢查询定义 MySQL记录下查询超过指定时间的语句,我们将超过指定时间的SQL语句查询称为慢查询. 查看时间限制 show variables lik ...
- linux防火墙过滤规则
一.linux防火墙基础 防火墙分为硬件防火墙和软件防火墙. 1.概述 linux 防火墙体系主要工作在网络层,针对TCP/IP数据包实施过滤和限制,属于典型的包过滤防火墙. 包过滤机制:netfil ...
- codeforces 669C C. Little Artem and Matrix(水题)
题目链接: C. Little Artem and Matrix time limit per test 2 seconds memory limit per test 256 megabytes i ...
- 贪吃蛇小游戏—C++、Opencv编写实现
贪吃蛇游戏,C++.Opencv实现 设计思路: 1.显示初始画面,蛇头box初始位置为中心,食物box位置随机 2.按随机方向移动蛇头,按a.s.d.w键控制移动方向,分别为向左,向下,向右,向上 ...
- Linux下安装oracle数据库提示DISPLAY not set. Please set the DISPLAY and try again。
错误如下: Ignoring required pre-requisite failures. Continuing... Preparing to launch Oracle Universal I ...
- 解决IIS中部署WCF时,访问.svc文件的404错误问题
如果你直接在IIS 7中配置WCF,访问.svc文件时会出现404错误.解决方法,以管理员身份进入命令行模式,运行:"%windir%\Microsoft.NET\Framework\v3. ...
- Java:String之间通过==比较的情况
大家都知道在String之间的内容比较的时候,是通过equals函数比较的. 但是在在许多的面试题中,总是出现一堆的判断两个String对象通过==比较的结果,实际上是考的Java内存分配机制. Ja ...