当前位置:网站首页>[592. Fraction addition and subtraction]
[592. Fraction addition and subtraction]
2022-07-28 09:12:00 【[email protected]】
source : Power button (LeetCode)
describe :
Given a string representing the addition and subtraction of fractions expression , You need to return a string calculation .
This result should be an irreducible score , namely Simplest fraction . If the final result is an integer , for example 2, You need to convert it into fractions , Its denominator is 1. So in the above example , 2 Should be converted to 2/1.
Example 1:
Input : expression = "-1/2+1/2"
Output : "0/1"
Example 2:
Input : expression = "-1/2+1/2+1/3"
Output : "1/3"
Example 3:
Input : expression = "1/3-1/2"
Output : "-1/6"
Tips :
- Input and output strings contain only
'0'To'9'The number of , as well as'/','+'and'-'. - The input and output fractional formats are
± molecular / The denominator. If the first score entered or the score output is a positive number , be'+'Will be omitted . - The input only contains legal Simplest Division Count , Of each score molecular And The denominator The range is
[1,10]. If the denominator is1, Which means that the score is actually an integer . - The range of scores entered is
[1,10]. - final result The numerator and denominator of are guaranteed to be 32 Valid integers in the range of bit integers .
Method : simulation

Code :
class Solution {
public:
string fractionAddition(string expression) {
long long denominator = 0, numerator = 1; // molecular , The denominator
int index = 0, n = expression.size();
while (index < n) {
// Read molecules
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++;
// Read denominator
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); // Get the greatest common divisor
return to_string(denominator / g) + "/" + to_string(numerator / g);
}
};
Execution time :0 ms, In all C++ Defeated in submission 100.00% Users of
Memory consumption :5.8 MB, In all C++ Defeated in submission 90.06% Users of
Complexity analysis
Time complexity : O(n + logC), among n Is string expression The length of , C Is the maximum value of the numerator denominator of the result before simplification . Finding the greatest common divisor requires O(logC).
Spatial complexity : O(1).
author:LeetCode-Solution
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/209/202207280824142785.html
边栏推荐
- Basic syntax of jquey
- v-bind指令的详细介绍
- 蓝牙技术|2025年北京充电桩总规模达70万个,聊聊蓝牙与充电桩的不解之缘
- NDK series (6): let's talk about the way and time to register JNI functions
- mysql5.7.38容器里启动keepalived
- A perfect cross compilation environment records the shell scripts generated by PETA
- [cloud computing] several mistakes that enterprises need to avoid after going to the cloud
- Explain cache consistency and memory barrier
- Map of China province > City > level > District > Town > village 5-level linkage download [2019 and 2021]
- Distributed system architecture theory and components
猜你喜欢

【592. 分数加减运算】

训练一个自己的分类 | 【包教包会,数据都准备好了】

There is a bug in installing CONDA environment

VS2015使用dumpbin 查看库的导出函数符号

1299_ Task status and switching test in FreeRTOS

Overview of head pose estimation

推荐一个摆脱变量名纠结的神器和批量修改文件名方法

Top all major platforms, 22 versions of interview core knowledge analysis notes, strong on the list

opencv4.60版本安装和配置

golang 协程的实现原理
随机推荐
Business digitalization is running rapidly, and management digitalization urgently needs to start
MDM数据质量应用说明
Code management platform SVN deployment practice
linux初始化mysql时报错 FATAL ERROR: Could not find my-default.cnf
ES6 let and Const
A new method of exposing services in kubernetes clusters
Go interface Foundation
ES查询索引字段的分词结果
Setting of parameter configuration tool for wireless vibrating wire collector
leetcode 452. Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球(中等)
Dapp安全总结与典型安全事件分析
ES6 变量的解构赋值
CSV file storage
Two dimensional array and operation
Vrrp+mstp configuration details [Huawei ENSP experiment]
Chapter 2-14 sum integer segments
公众号简介
A perfect cross compilation environment records the shell scripts generated by PETA
一款入门神器TensorFlowPlayground
Argocd Web UI loading is slow? A trick to teach you to solve