当前位置:网站首页>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);
}
};
边栏推荐
- 【冷冻电镜|论文阅读】子断层平均 M 软件解读:Multi-particle cryo-EM refinement with M
- Teacher wangshuyao's notes on operations research 01 guidance and introduction
- NeuralCF-神经协同过滤网络
- 分享一些你代码更好的小建议,流畅编码提搞效率
- Embedding understanding + code
- Pytorch多GPU条件下DDP集群分布训练实现(简述-从无到有)
- DM数据守护集群搭建
- 如何优雅的写 Controller 层代码?
- Teacher wangshuyao's notes on operations research 06 linear programming and simplex method (geometric significance)
- Actual combat! Talk about how to solve the deep paging problem of MySQL
猜你喜欢

DM数据守护集群搭建

【冷冻电镜|论文阅读】子断层平均 M 软件解读:Multi-particle cryo-EM refinement with M

Actual combat! Talk about how to solve the deep paging problem of MySQL

NeuralCF-神经协同过滤网络

线程同步—— 生产者与消费者、龟兔赛跑、双线程打印

Why does 5g N2 interface control plane use SCTP protocol?

【冷冻电镜】RELION4.0之subtomogram对位功能源码分析(自用)

分享一些你代码更好的小建议,流畅编码提搞效率

NLP-分词

Software definition boundary SDP
随机推荐
Teacher Wu Enda machine learning course notes 01 introduction
JMM memory model concept
模拟卷Leetcode【普通】081. 搜索旋转排序数组 II
王树尧老师运筹学课程笔记 07 线性规划与单纯形法(标准型、基、基解、基可行解、可行基)
Why does 5g N2 interface control plane use SCTP protocol?
IDEA找不到Database解决方法
Neuralcf neural collaborative filtering network
【笔记】The art of research - (讲好故事和论点)
Unity探索地块通路设计分析 & 流程+代码具体实现
Shallow reading of reentrantlock source code of abstractqueuedsynchronizer (AQS)
【技能积累】presentation实用技巧积累,常用句式
Summary of 2022 SQL classic interview questions (with analysis)
吴恩达老师机器学习课程笔记 02 单变量线性回归
Loss function -- cross entropy loss function
Pytorch多GPU条件下DDP集群分布训练实现(简述-从无到有)
Teacher Cui Xueting's course notes on optimization theory and methods 00 are written in the front
API for using the new date class of instant
JVM之垃圾回收机制(GC)
模拟卷Leetcode【普通】093. 复原 IP 地址
【技能积累】写邮件时的常用表达
