当前位置:网站首页>Leetcode-592: fraction addition and subtraction
Leetcode-592: fraction addition and subtraction
2022-07-29 06:57:00 【Chrysanthemum headed bat】
leetcode-592: Fractional addition and subtraction
subject
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 , The 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"

Problem solving
Method 1 : simulation
class Solution {
public:
string fractionAddition(string expression) {
long long a=0,b=1;// molecular , The denominator
int index=0,n=expression.size();
while(index<n){
// Read the sign
int sign=1;// If no symbols are encountered , Default positive
if(expression[index]=='+'||expression[index]=='-'){
sign=expression[index]=='+'?1:-1;
index++;
}
// Read molecules
long long a1=0;
while(isdigit(expression[index])){
a1=a1*10+expression[index]-'0';
index++;
}
index++;// skip '/'
// Read denominator
long long b1=0;
while(isdigit(expression[index])){
b1=b1*10+expression[index]-'0';
index++;
}
a1=a1*sign;
a=a*b1+a1*b;
b=b*b1;
}
if(a==0) return "0/1";
long long g=gcd(abs(a),b);// Get the greatest common divisor
return to_string(a/g)+"/"+to_string(b/g);
}
};
边栏推荐
- 【冷冻电镜|论文阅读】A feature-guided, focused 3D signal permutation method for subtomogram averaging
- 王树尧老师运筹学课程笔记 09 线性规划与单纯形法(单纯形表的应用)
- 游戏资产的革命
- 循环神经网络RNN
- Teacher wangshuyao's notes on operations research 06 linear programming and simplex method (geometric significance)
- SQL developer graphical window to create database (tablespace and user)
- 二次元卡通渲染——进阶技巧
- Pytorch多GPU条件下DDP集群分布训练实现(简述-从无到有)
- 【干货备忘】50种Matplotlib科研论文绘图合集,含代码实现
- 吴恩达老师机器学习课程笔记 00 写在前面
猜你喜欢

How to write controller layer code gracefully?

Ali gave several SQL messages and asked how many tree search operations need to be performed?

【CryoEM】FSC, Fourier Shell Correlation简介

C语言数据类型

Windows 上 php 7.4 连接 oracle 配置

Teacher Wu Enda's machine learning course notes 02 univariate linear regression

线程 - 线程安全 - 线程优化

Jetpack Compose 中的键盘处理

5g service interface and reference point

猜数字//第一次使用生成随机数
随机推荐
Embedding理解+代码
Teacher Wu Enda's machine learning course notes 00 are written in the front
LDAP brief description and unified authentication description
王树尧老师运筹学课程笔记 04 线性代数基础
Hongke share | let you have a comprehensive understanding of "can bus errors" (IV) -- producing and recording can errors in practice
模拟卷Leetcode【普通】061. 旋转链表
王树尧老师运筹学课程笔记 08 线性规划与单纯形法(单纯形法)
【笔记】The art of research - (讲好故事和论点)
模拟卷Leetcode【普通】150. 逆波兰表达式求值
Introduction to OSPF theory
线程 - 线程安全 - 线程优化
SQL developer graphical window to create database (tablespace and user)
Unity免费元素特效推荐
STP spanning tree principle and example of election rules
ECCV 2022丨轻量级模型架ParC-Net 力压苹果MobileViT代码和论文下载
模拟卷Leetcode【普通】172. 阶乘后的零
Phantom reference virtual reference code demonstration
Shallow reading of shared lock source code of abstractqueuedsynchronizer (AQS)
会话推荐中的价格偏好和兴趣偏好共同建模-论文泛读
Teacher Wang Shuyao's notes on operations research 09 linear programming and simplex method (Application of simplex table)
