当前位置:网站首页>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();
}
边栏推荐
- 使用密钥对的形式连接阿里云服务器
- LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
- Yocto Technology Sharing Phase 4: Custom add package support
- Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
- 01 business structure of imitation station B project
- . DLL and Differences between lib files
- 2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
- 20220531数学:快乐数
- Leetcode-100:相同的树
- Opencv notes 17 template matching
猜你喜欢

CV learning notes - clustering

My notes on intelligent charging pile development (II): overview of system hardware circuit design

yocto 技术分享第四期:自定义增加软件包支持

LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)

LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)

Serial communication based on 51 single chip microcomputer

LeetCode - 5 最长回文子串

Development of intelligent charging pile (I): overview of the overall design of the system

LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)

2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
随机推荐
3.1 Monte Carlo Methods & case study: Blackjack of on-Policy Evaluation
LeetCode - 900. RLE 迭代器
Swing transformer details-2
Yocto technology sharing phase IV: customize and add software package support
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
Basic knowledge of communication interface
On the problem of reference assignment to reference
Deep learning by Pytorch
QT is a method of batch modifying the style of a certain type of control after naming the control
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
3.3 Monte Carlo Methods: case study: Blackjack of Policy Improvement of on- & off-policy Evaluation
Dictionary tree prefix tree trie
The data read by pandas is saved to the MySQL database
What can I do to exit the current operation and confirm it twice?
Retinaface: single stage dense face localization in the wild
2021-10-27
Deep Reinforcement learning with PyTorch
Octave instructions
CV learning notes - deep learning
Stm32 NVIC interrupt priority management