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 ...
随机推荐
- armel、armhf和arm64
1 这些名词是什么的缩写 1.1 armel 是arm eabi little endian的缩写.eabi是软浮点二进制接口,这里的e是embeded,是对于嵌入式设备而言. 1.2 armhf 是 ...
- 微信小程序引入外部js 方法
步骤: 1.首先将外部js放在你指定的文件夹里(这都是废话...) 2.接下来 将该js文件中你要使用的方法给暴露出来 3.在您要使用的js中引入该js 4.使用暴露出来的方法 例子:使用md5加密 ...
- 恢复MySQL数据库删除的数据
在日常运维工作中,对于数据库的备份是至关重要的!数据库对于网站的重要性使得我们对 MySQL 数据库的管理不容有失!然而是人总难免会犯错误,说不定哪天大脑短路了,误操作把数据库给删除了,怎么办? 下面 ...
- python大法好 vijos1375 大整数
一个k(1<=k<=80)位的十进制正整数N,就是所谓的大整数.请你设计程序,对于给出的某一个大整数N,找到满足p^3+p^2+3p<=n的p的最大值. 输入格式 输入数据只有一行, ...
- linux 一个超简单的makefile
makefile 自动化变量: $@ : 规则的目标文件名 例如:main:main.o test.o g++ -Wall -g main.o test. ...
- 关于LAMP配置Let’s Encrypt SSL证书
昨天建站,买VPS,先装了LAMP,部署wordpress,测试OK了,然后才买的域名,申请SSL证书. 结果Let’s Encrypt cerbot申请证书遇到了麻烦,--apache参数怎么也识别 ...
- POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19895 ...
- iOS NSString拼接字符串
NSString* str_C; // 结果字符串NSString* str_A, str_B; //已存在的字符串,需要将str_A和str_B连接起来 //方法1 str_C = [NSStrin ...
- solr安装-tomcat+solrCloud构建稳健solr集群
solrCloud的搭建可以有两种方式:使用solr内嵌的jetty来搭建:使用外部web容器tomcat来搭建.对于使用jett来搭建参考solr官方的手册照着做肯定ok,下面我主要讲的是如何使用t ...
- iOS bounds、frame之间的关系
这几个都是在ios程序中,经常会注意到的一些小细节,能否真正了解这些,对写ios程序也有很大的好处. frame 是UIView中表示此view的一个矩形面积,包括了view在它的superview中 ...