Java for LeetCode 166 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.
For example,
Given numerator = 1, denominator = 2, return "0.5".
Given numerator = 2, denominator = 1, return "2".
Given numerator = 2, denominator = 3, return "0.(6)".
解题思路:
测试中会出现各种临界值,因此,需要先将 numerator 和 denominator转为long,然后计算出整数部分,对于小数部分,将numerator*10后处理,一方面将计算除后的值放到一个list中,另一方面将新的分子放到map里,由于新分子肯定小于denominator,所以肯定会出现0或者重复(因此分数要么有限,要么是无限循环小数)一旦新分母重复出现,意味着结果会出现循环值,将其按照规则写出来即可,JAVA实现如下:
public String fractionToDecimal(int numerator, int denominator) {
StringBuilder sb = new StringBuilder();
if (numerator < 0 && denominator > 0)
sb.append("-");
else if (numerator > 0 && denominator < 0)
sb.append("-");
Long Lnumerator = (long) numerator;
if (Lnumerator < 0)
Lnumerator = -Lnumerator;
Long Ldenominator = (long) denominator;
if (Ldenominator < 0)
Ldenominator = -Ldenominator;
if (Lnumerator % Ldenominator == 0){
sb.append(Lnumerator / Ldenominator);
return sb.toString();
}
sb.append(Lnumerator / Ldenominator + ".");
System.out.println(Lnumerator);
Lnumerator %= Ldenominator;
System.out.println(Lnumerator);
HashMap<Long, Integer> map = new HashMap<Long, Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
map.put(Lnumerator, 0);
while (true) {
Lnumerator *= 10;
while (Lnumerator < Ldenominator) {
list.add(0);
map.put(Lnumerator, list.size());
Lnumerator *= 10;
}
list.add((int) (Lnumerator / Ldenominator));
Lnumerator %= Ldenominator;
if (Lnumerator == 0) {
for (int i = 0; i < list.size(); i++)
sb.append(list.get(i));
return sb.toString();
} else if (map.containsKey(Lnumerator)) {
for (int i = 0; i < map.get(Lnumerator); i++)
sb.append(list.get(i));
sb.append("(");
for (int i = map.get(Lnumerator); i < list.size(); i++)
sb.append(list.get(i));
sb.append(")");
return sb.toString();
}
map.put(Lnumerator, list.size());
}
}
Java for LeetCode 166 Fraction to Recurring Decimal的更多相关文章
- Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...
- ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Leetcode#166 Fraction to Recurring Decimal
原题地址 计算循环小数 先把负数转化成正数,然后计算,最后添加符号 当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数 需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧 ...
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- 【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
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 166. Fraction to Recurring Decimal
题目: Given two integers representing the numerator and denominator of a fraction, return the fraction ...
- [LeetCode] 167. Fraction to Recurring Decimal 分数转循环小数
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
随机推荐
- html+Ajax和JSP的比较
1.有人说JSP会泄露源码(可能会有一些代码痕迹,但肯定没啥大事)2.又说,Ajax是为了分离前后台,让控制部分在前台处理,降低代码耦合度,后台只相当于服务. 3.能够让前台移植,降低后期维护成本.纯 ...
- Cocos2d-X3.0 刨根问底(五)----- Node类及显示对象列表源码分析
上一章 我们分析了Cocos2d-x的内存管理,主要解剖了 Ref.PoolManager.AutoreleasePool这三个类,了解了对象是如何自动释放的机制.之前有一个类 Node经常出现在各种 ...
- 淘宝中的UV,PV,IPV
1. UV & PV UV: 店铺各页面的访问人数,一个用户在一天内多次访问店铺被记为一个访客(去重) ; Unique visitors PV: 店铺内所有页面的浏览总量(次数累加); p ...
- 【poj1962】 Corporative Network
http://poj.org/problem?id=1962 (题目链接) 时隔多年又一次写带权并查集. 题意 n个节点,若干次询问,I x y表示从x连一条边到y,权值为|x-y|%1000:E x ...
- 了解 Nginx 基本概念
前言 本篇是我学习 Nginx 的一些笔记,主要内容讲述了一些了解 Nginx 需要的基本概念.然后探讨一下 Nginx 的模块化的组织架构,以及各个模块的分类.工作方式.职责和提供的相关指令. 主要 ...
- 《驾驭Core Data》 第二章 Core Data入门
本文由海水的味道编译整理,请勿转载,请勿用于商业用途. 当前版本号:0.4.0 第二章 Core Data入门 本章将讲解Core Data框架中涉及的基本概念,以及一个简单的Core Data ...
- 使用Navicat V8.0创建数据库,外键出现错误ERROR 1005: Can’t create table (errno: 121)
ERROR 1005: Can't create table (errno: 121) errno 121 means a duplicate key error. Probably the tabl ...
- vagrant 启动错误
Stderr: VBoxManage.EXE: error: Failed to create the VirtualBox object!VBoxManage.EXE: error: Code E_ ...
- Matlab中的括号()[]{}
Matlab中的括号()[]{} Matlab中经常会用到括号去引用某Array或者是cell的内容,但三者有什么具体区别呢?[ ] 中括号用来构建向量(Vectors)或者是矩阵(Matrices) ...
- 菲涅尔反射(Fresnel Reflection)
离线渲染中,通常可以用kd,ks,kt(分别代表物体的漫反射系数,镜面反射系数,透射系数)来简单地描述一个物体的基本材质,例如,我们将一个物体设置为:kd=0,ks=0.1,kt=0.9,即代表一束光 ...