当前位置:网站首页>【592. 分数加减运算】
【592. 分数加减运算】
2022-07-28 08:24:00 【千北@】
来源:力扣(LeetCode)
描述:
给定一个表示分数加减运算的字符串 expression ,你需要返回一个字符串形式的计算结果。
这个结果应该是不可约分的分数,即最简分数。 如果最终结果是一个整数,例如 2,你需要将它转换成分数形式,其分母为 1。所以在上述例子中, 2 应该被转换为 2/1。
示例 1:
输入: expression = "-1/2+1/2"
输出: "0/1"
示例 2:
输入: expression = "-1/2+1/2+1/3"
输出: "1/3"
示例 3:
输入: expression = "1/3-1/2"
输出: "-1/6"
提示:
- 输入和输出字符串只包含
'0'到'9'的数字,以及'/','+'和'-'。 - 输入和输出分数格式均为
±分子/分母。如果输入的第一个分数或者输出的分数是正数,则'+'会被省略掉。 - 输入只包含合法的最简分数,每个分数的分子与分母的范围是
[1,10]。 如果分母是1,意味着这个分数实际上是一个整数。 - 输入的分数个数范围是
[1,10]。 - 最终结果的分子与分母保证是 32 位整数范围内的有效整数。
方法:模拟

代码:
class Solution {
public:
string fractionAddition(string expression) {
long long denominator = 0, numerator = 1; // 分子,分母
int index = 0, n = expression.size();
while (index < n) {
// 读取分子
long long denominator1 = 0, sign = 1;
if (expression[index] == '-' || expression[index] == '+') {
sign = expression[index] == '-' ? -1 : 1;
index++;
}
while (index < n && isdigit(expression[index])) {
denominator1 = denominator1 * 10 + expression[index] - '0';
index++;
}
denominator1 = sign * denominator1;
index++;
// 读取分母
long long numerator1 = 0;
while (index < n && isdigit(expression[index])) {
numerator1 = numerator1 * 10 + expression[index] - '0';
index++;
}
denominator = denominator * numerator1 + denominator1 * numerator;
numerator *= numerator1;
}
if (denominator == 0) {
return "0/1";
}
long long g = gcd(abs(denominator), numerator); // 获取最大公约数
return to_string(denominator / g) + "/" + to_string(numerator / g);
}
};
执行用时:0 ms, 在所有 C++ 提交中击败了100.00%的用户
内存消耗:5.8 MB, 在所有 C++ 提交中击败了90.06%的用户
复杂度分析
时间复杂度: O(n + logC),其中 n 是字符串 expression 的长度, C 为化简前结果分子分母的最大值。求最大公约数需要 O(logC)。
空间复杂度: O(1)。
author:LeetCode-Solution
边栏推荐
- Detailed explanation of MSTP protocol for layer 3 switch configuration [Huawei ENSP experiment]
- Marketing play is changeable, and understanding the rules is the key!
- This flick SQL timestamp_ Can ltz be used in create DDL
- Go panic and recover
- Post it notes -- 45 {packaging of the uniapp component picker, for data transmission and processing -- Based on the from custom packaging that will be released later}
- Alibaba internal interview materials
- Digital signatures and Ca certificates
- PHP Basics - PHP uses PDO
- Data analysis interview question summary
- 训练一个自己的分类 | 【包教包会,数据都准备好了】
猜你喜欢

一年涨薪三次背后的秘密

NPM and yarn use (official website, installation, command line, uploading your own package, detailed explanation of package version number, updating and uninstalling package, viewing all versions, equ

Argocd Web UI loading is slow? A trick to teach you to solve

golang 协程的实现原理

How to obtain the subordinate / annotation information of KEGG channel

Bash shell interaction free
![[opencv] generate transparent PNG image](/img/0a/4afc9bda411634562f4b0f3915a7ba.png)
[opencv] generate transparent PNG image

我来教你如何组装一个注册中心?

实现批量数据增强 | keras ImageDataGenerator使用

Network interface network crystal head RJ45, Poe interface definition line sequence
随机推荐
golang 协程的实现原理
训练一个自己的分类 | 【包教包会,数据都准备好了】
Mobaxtermsession synchronization
象棋机器人夹伤7岁男孩手指,软件测试工程师的锅?我笑了。。。
Chapter 2-2 calculation of piecewise function [1]
[advanced drawing of single cell] 07. Display of KEGG enrichment results
ES6 变量的解构赋值
【单细胞高级绘图】07.KEGG富集结果展示
[activity registration] User Group Xi'an - empowering enterprise growth with modern data architecture
How to obtain the subordinate / annotation information of KEGG channel
Quickly build a gateway service, dynamic routing and authentication process, and watch the second meeting (including the flow chart)
Principle of line of sight tracking and explanation of the paper
Competition: diabetes genetic risk detection challenge (iFLYTEK)
Div tags and span Tags
Business digitalization is running rapidly, and management digitalization urgently needs to start
You're not still using xshell, are you? This open source terminal tool is yyds!
修改虚拟机IP地址
Go interface advanced
Baidu AI Cloud Jiuzhou district and county brain, depicting a new blueprint for urban and rural areas!
Kubernetes cluster configuration DNS Service