Fraction to Recurring Decimal leetcode
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)".
Credits:
Special thanks to @Shangrila for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
string fractionToDecimal(int numerator, int denominator) {
string ret;
int a = numerator;
int b = denominator;
ret = to_string(a / b);
a = a % b;
if (a)
ret.push_back('.');
else
return ret;
unordered_map<int, int> m;
int i = ret.length() - ;
while (a)
{
i++;
if (m.find(a) != m.end()) {
ret.insert(ret.begin() + m[a], '(');
ret.push_back(')');
break;
}
m[a] = i;
a *= ;
if (a < b) {
ret.push_back('');
continue;
}
ret.push_back('' + a / b);
a = a % b;
}
return ret;
}
提交后发现,有特殊情况没有考虑到。-2147483648 / -1 这种情况会溢出,所以需要使用long类型替换int。
参考修改后代码如下:
string fractionToDecimal(int numerator, int denominator) {
if (!numerator) return "";
string ret;
if (numerator < ^ denominator < ) ret += '-';
long a = numerator < ? (long)numerator * (-) : (long)numerator;
long b = denominator < ? (long)denominator * (-) : (long)denominator;
long c = a / b;
ret += to_string(c);
a = a % b;
if (!a) return ret;
ret.push_back('.');
unordered_map<long, long> m;
int i = ret.length() - ;
while (a)
{
i++;
if (m.find(a) != m.end()) {
ret.insert(ret.begin() + m[a], '(');
ret.push_back(')');
break;
}
m[a] = i;
a *= ;
if (a < b) {
ret.push_back('');
continue;
}
ret.push_back('' + a / b);
a = a % b;
}
return ret;
}
虽然在leetcode上提交成功了,但是在vs2015上运行是错误的,
long a = numerator < 0 ? (long)numerator * (-1) : (long)numerator;
这一句如果是numerator是-2147483648,a会是-2147483648,这非常诡异。不知道vs2015编译器对于这个问题是怎么解决的。
Fraction to Recurring Decimal leetcode的更多相关文章
- 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 Fraction to Recurring Decimal
原题链接在这里:https://leetcode.com/problems/fraction-to-recurring-decimal/ 题目: Given two integers represen ...
- [LeetCode] Fraction to Recurring Decimal 分数转循环小数
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- [LeetCode#116]Fraction to Recurring Decimal
Problem: Given two integers representing the numerator and denominator of a fraction, return the fra ...
随机推荐
- Intent的属性及Intent-filter配置——Data、Type属性与intent-filter配置
Data属性通常用于向Action属性提供操作的数据,Data属性接受一个Uri对象,一个Uri对象通常通过如下形式的字符串来表示: content://com.android.contacts/co ...
- HTML5 简介、HTML5 浏览器支持
HTML5是HTML最新的修订版本,2014年10月由万维网联盟(W3C)完成标准制定. HTML5的设计目的是为了在移动设备上支持多媒体. HTML5 简单易学. 什么是 HTML5? HTML5 ...
- 在Express中安装XTemplate
上一节讲了安装Express,并且生成了一个"microblog"的工程,我们的目标是在工程下安装XTemplate: 1.安装xtpl npm install xtpl xtem ...
- 【Unity3d游戏开发】游戏中的贝塞尔曲线以及其在Unity中的实现
RT,马三最近在参与一款足球游戏的开发,其中涉及到足球的各种运动轨迹和路径,比如射门的轨迹,高吊球,香蕉球的轨迹.最早的版本中马三是使用物理引擎加力的方式实现的足球各种运动,后来的版本中使用了根据物理 ...
- iOS动画案例(1)
受人所托,做一个类似于qq账号信息里的一个动画,感觉挺有意思,也没感觉有多难,就开始做了,结果才发现学的数学知识都还给体育老师了,研究了大半天才做出来. 先看一下动画效果: 用到的知识 ...
- KB奇遇记(3):IT现状
2015年8月3号,终于告别了过去来到了KB. 公司给安排的住房是一间套房里的小房间,小的简直连坐的地方都没有了,中间一个大床将房间隔了两边,显得特别狭小.由于是刚来,我也不好要求太多.但就这个小房间 ...
- C# 连接 SQLServer 及操作
随笔:连接: // 将tb_User表数据添加到DataGridView中 string sqlconn = "Data Source=localhost;Initial Catalog=d ...
- 蓝桥网试题 java 基础练习 矩形面积交
------------------------------------------------------------------------------------------- 思路见锦囊2 - ...
- Raspberry树莓派学习笔记1—基本介绍
树莓派的简单介绍 一个名片大小的迷你个人电脑主机,还有wifi/蓝牙... 运行完整的Linux操作系统(注意关键字:完整,不是精简过的嵌入式Linux) 开源的硬件平台.与普通主机不同的是,它带有简 ...
- linux下安装Mysql 以及导入数据库
1.下载mysql的rpm包,创建一个文件夹例如software来放置下面文件 可以通过wget下载具体的地址 (1)MySQL-server-5.6.10-1.rhel5.x86_64.rpm:My ...