当前位置:网站首页>20220606 Mathematics: fraction to decimal
20220606 Mathematics: fraction to decimal
2022-07-03 10:11:00 【Seeyouagain】
Title Description : Given two integers , Molecules representing fractions respectively numerator
Denominator denominator
, With Returns a decimal as a string . If the decimal part is a circular decimal , The loop is enclosed in brackets . If there are multiple answers , Just go back Any one . For all given inputs , Guarantee The length of the answer string is less than 10^4
.
coded :
public String fractionToDecimal(int numerator, int denominator) {
if (denominator == 0) {
return "";
}
if (numerator == 0) {
return "0";
}
StringBuilder result = new StringBuilder();
long num = numerator,den = denominator;
if ((num >= 0) ^ (den >= 0)) {
result.append("-");
}
num = Math.abs(num);
den = Math.abs(den);
result.append(num / den);
num %= den;
if (num == 0) {
return result.toString();
}
result.append(".");
int index = result.length() - 1;
Map<Long, Integer> record = new HashMap<>();
while (num != 0 && !record.containsKey(num)) {
record.put(num, ++index);
num *= 10;
result.append(num / den);
num %= den;
}
if (record.containsKey(num)) {
result.insert(record.get(num), "(");
result.append(")");
}
return result.toString();
}
边栏推荐
- Opencv notes 17 template matching
- My 4G smart charging pile gateway design and development related articles
- is_ power_ of_ 2 judge whether it is a multiple of 2
- pycharm 无法引入自定义包
- 3.2 Off-Policy Monte Carlo Methods & case study: Blackjack of off-Policy Evaluation
- 03 fastjason solves circular references
- Screen display of charging pile design -- led driver ta6932
- Flutter 退出当前操作二次确认怎么做才更优雅?
- LeetCode - 5 最长回文子串
- Leetcode-404:左叶子之和
猜你喜欢
Anaconda安装包 报错packagesNotFoundError: The following packages are not available from current channels:
CV learning notes convolutional neural network
RESNET code details
CV learning notes - image filter
Retinaface: single stage dense face localization in the wild
yocto 技术分享第四期:自定义增加软件包支持
2021-10-28
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
随机推荐
LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
Vgg16 migration learning source code
QT setting suspension button
What can I do to exit the current operation and confirm it twice?
Modelcheckpoint auto save model
20220601数学:阶乘后的零
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
CV learning notes alexnet
2021-10-27
LeetCode - 919. Full binary tree inserter (array)
LeetCode - 933 最近的请求次数
2021-10-28
Leetcode-100:相同的树
4G module initialization of charge point design
Screen display of charging pile design -- led driver ta6932
openCV+dlib实现给蒙娜丽莎换脸
(1) What is a lambda expression
Discrete-event system
Markdown latex full quantifier and existential quantifier (for all, existential)
Opencv+dlib to change the face of Mona Lisa