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 ...
随机推荐
- Medical Image Processing Conference and Journal 医学图像处理会议与期刊
会议: Information Processing in Medical Imaging,IPMI IPMI2013 International Conference on Medical Imag ...
- 利用Python的三元表达式解决Odoo中工资条中城镇、农村保险的问题
Python中没有像C#中有三元表达式 A?B:C 但在python中可以通过 A if condition else B 的方式来达到同样的效果. 例如 : 1 if True else 0 输出 ...
- Excel操作类
'引入Excel的COM组件 Imports System Imports System.Data Imports System.Configuration Imports System.Web Im ...
- [ZZ] Adventures with Gamma-Correct Rendering
http://renderwonk.com/blog/index.php/archive/adventures-with-gamma-correct-rendering/ Adventures wit ...
- 匈牙利命名法,骆驼命名法(camel),帕斯卡(Pascal)命名法(转)
一.匈牙利命名法 Windows 编程中用到的变量(还包括宏)的命名规则匈牙利命名法,这种命名技术是由一位能干的 Microsoft 程序员查尔斯·西蒙尼(Charles Simonyi) ...
- Windows2008 R2修改3389端口教程
默认的3389端口有必要修改么,这个端口,我建议是修改一下为好,最好改大一点,这样安全性会高一点,但然面对高手级攻击,就没有用了,通常高手级过NMAP扫描一个你的IP地址,端便全然显现出来了,好了,下 ...
- vbaexcel
Sub WordTest() Dim objwordApp As Word.Application Dim objword As Word.Document Dim objSeheet As Stri ...
- PHP数组常用函数分类整理
一.数组操作的基本函数数组的键名和值array_values($arr); 获得数组的值array_keys($arr); 获得数组的键名array_flip($arr); 数组中的值与键名互换 ...
- bpel 之伙伴
一.伙伴链接类型(Partner Link Types) 1.交互过程 伙伴之间的交互过程共分为两种典型情况: 流程调用伙伴后同步等待返回结果.这种情况通常是伙伴能很快返回结果,流程不需要等待很长时间 ...
- phpexcel 读取数据
最近公司做一个客户导入会员的功能,以前导入都是使用csv格式导入的,但是客户反应问题挺多的,普遍是乱码(由于各种系统各种环境可能引起编码问题).最近想着就把这个导入完全改成excel导入,就研究了下p ...