LeetCode 166. Fraction to Recurring Decimal(循环小数)
2016-05-25 01:39
543 查看
原题网址: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.
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)".
Hint:
No scary math, just apply elementary math knowledge. Still remember how to perform a long division?
Try a long division on 4/9, the repeating part is obvious. Now try 4/333. Do you see a pattern?
Be wary of edge cases! List out as many test cases as you can think of and test your code thoroughly.
方法:使用哈希映射记录余数和位置。
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)".
Hint:
No scary math, just apply elementary math knowledge. Still remember how to perform a long division?
Try a long division on 4/9, the repeating part is obvious. Now try 4/333. Do you see a pattern?
Be wary of edge cases! List out as many test cases as you can think of and test your code thoroughly.
方法:使用哈希映射记录余数和位置。
public class Solution { public String fractionToDecimal(int numerator, int denominator) { StringBuilder sb = new StringBuilder(); if ((numerator < 0 && denominator > 0) || (numerator > 0 && denominator < 0)) sb.append("-"); long quotient = (long)numerator / denominator; sb.append(Math.abs(quotient)); long remainder = (long)numerator % denominator * 10; if (remainder != 0) { sb.append("."); StringBuilder fraction = new StringBuilder(); Map<Long, Integer> nums = new HashMap<>(); // nums.add(numerator); int pos = 0; nums.put(remainder, pos++); do { int q = (int)Math.abs(remainder / denominator); fraction.append(q); remainder = remainder % denominator * 10; if (nums.containsKey(remainder)) break; nums.put(remainder, pos++); } while (remainder != 0); if (remainder != 0) { fraction.insert(nums.get(remainder), "("); fraction.append(")"); sb.append(fraction.toString()); } else { sb.append(fraction.toString()); } } return sb.toString(); } }
相关文章推荐
- LeetCode 169. Majority Element(众数)
- 3dsMax之CoCreateInstence() failed报错
- LeetCode 186. Reverse Words in a String II(反转单词)
- LeetCode 179. Largest Number(最大数)
- LeetCode 173. Binary Search Tree Iterator(二叉搜索树迭代器)
- LeetCode 172. Factorial Trailing Zeroes(0结尾)
- LeetCode 171. Excel Sheet Column Number(Excel列号)
- LeetCode 170. Two Sum III - Data structure design(两数和)
- 完全移除TFS2013的版本控制
- Rest API 设计最佳实践(1)
- LeetCode 350. Intersection of Two Arrays II(数组交集)
- LeetCode 174. Dungeon Game(游戏)
- LeetCode 187. Repeated DNA Sequences(重复DNA序列)
- LeetCode 351. Android Unlock Patterns(安卓解锁)
- C#匹配中文字符串的4种正则表达式分享
- Redis中基本数据类型的操作
- Android 简单的条形颜色选择器
- 独立看门狗
- 匹配中文字符的正则表达式: [/u4e00-/u9fa5]
- xml解析之SAX