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)”.
分析
由上描述,本题要求得整数相除的结果,对循环小数用括号扩之;
首先,对于除数和被除数的特殊情况需分类处理;
然后,得到整数部分;
再次,分析小数部分,若有循环小数得到正确下标增加括号;
注意:整数的溢出问题;
先将int类型保存至long long类型;
AC代码
class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
string str = "";
//除数为0,为异常情况
if (denominator == 0)
return str;
//被除数为0,结果为0
if (numerator == 0)
return "0";
//异或,numerator<0和denominator<0仅有一个为真
if (numerator < 0 ^ denominator < 0)
str += '-';
//转化为正数,INT_MIN转化为正数会溢出,故用long long;long long int n=abs(INT_MIN)得到的n仍然是负的,所以写成下面的形式
long long r = numerator; r = abs(r);
long long d = denominator; d = abs(d);
//得到整数部分并保存
str += to_string(r / d);
r = r % d;
//可以整除,直接返回
if (r == 0)
return str;
//添加小数点
str += ".";
//下面处理小数部分,用哈希表
unordered_map<int, int> map;
while (r){
//检查余数r是否在哈希表中,是的话则开始循环了
if (map.find(r) != map.end()){
str.insert(map[r], 1, '(');
str += ')';
break;
}
map[r] = str.size(); //这个余数对应于result的哪个位置
//正常运算
r *= 10;
str += to_string(r / d);
r = r % d;
}
return str;
}
//整数到字符串的转换函数
string intToStr(long long num)
{
string str = "";
if (num < 10)
{
char c = num + '0';
return str + c;
}
else
{
while (num)
{
int d = num % 10;
char c = d + '0';
str += c;
num /= 10;
}//while
reverse(str.begin(), str.end());
return str;
}//else
}
};
LeetCode(166) Fraction to Recurring Decimal的更多相关文章
- 【Leetcode 166】 Fraction to Recurring Decimal
Description: Given two integers representing the numerator and denominator of a fraction, return the ...
- 【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解题报告—— 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】Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
随机推荐
- MDX分页查询
WITH SET [e16a30d0-2174-4874-8dae-a5085a75a3e2] as NONEMPTY({[Measures].[终端销售数量], [Measures].[终端销售吊牌 ...
- Exception sending context destroyed event to listener instance of class
五月 29, 2019 6:29:39 下午 org.apache.catalina.core.StandardContext listenerStop严重: Exception sending co ...
- noip搜索模拟题 骰子
骰子 dice.cpp/c/pas 1s/128M [题目描述] 桌面上有两个特别的骰子.骰子的每一个面,都写了一个不同的数字.设第一个骰子上下左右前后分别为a1, a2, a3, a4, a5, a ...
- Echarts的重点
官网中,主要看文档的”教程“和”配置项手册“这两部分 1 下载 引入js 页面放一个容器,一定要设宽高 创建对象:var myChart = echarts.init(document.getElem ...
- vue2.0:(九)、外卖App弹窗部分星星评分
本篇是星星评分部分,先上代码: 1.header.vue: <template> <transition name="fade"> & ...
- -oN ,-oX,-oG
-oN ,正常输出 -oX, xml输出 nmap 192.168.9.12 -oX TEST.xml -oG grep输出 html文件可读性比xml文件要好,将xml转换成html xs ...
- [转载]—Health Check Reports Problem: Dependency$ p_timestamp mismatch for VALID objects (文档 ID 781959.1)
Health Check Reports Problem: Dependency$ p_timestamp mismatch for VALID objects (文档 ID 781959.1) AP ...
- C# 向服务器上传文件(客服端winform、服务端web)
转载 首先写客服端,winform模拟一个post提交: /// <summary> /// 将本地文件上传到指定的服务器(HttpWebRequest方法) /// </summa ...
- npm在linux即mac下更新时报错
nam在linux即mac下需要更新到新版本:
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...