当前位置:网站首页>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);
}
};
边栏推荐
- 'function VTable for error: undefined reference to... 'cause and solution of the problem
- 阿里一面,给了几条SQL,问需要执行几次树搜索操作?
- Ali gave several SQL messages and asked how many tree search operations need to be performed?
- 数据库使用psql及jdbc进行远程连接,不定时自动断开的解决办法
- 【冷冻电镜入门】加州理工公开课课程笔记 Part 3: Image Formation
- 数据库多表查询 联合查询 增删改查
- 模拟卷Leetcode【普通】093. 复原 IP 地址
- MySql基础知识(高频面试题)
- 模拟卷Leetcode【普通】150. 逆波兰表达式求值
- CVPR2022Oral专题系列(一):低光增强
猜你喜欢

MySQL: what happens in the bufferpool when you crud? Ten pictures can make it clear

【论文阅读 | 冷冻电镜】RELION 4.0 中新的 subtomogram averaging 方法解读

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

SQL developer graphical window to create database (tablespace and user)

Teacher wangshuyao's notes on operations research course 10 linear programming and simplex method (discussion on detection number and degradation)

【论文阅读 | cryoET】Gum-Net:快速准确的3D Subtomo图像对齐和平均的无监督几何匹配

Unity探索地块通路设计分析 & 流程+代码具体实现

STP spanning tree principle and example of election rules

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

Actual combat! Talk about how to solve the deep paging problem of MySQL
随机推荐
The difference between pairs and ipairs
Not so simple singleton mode
阿里一面,给了几条SQL,问需要执行几次树搜索操作?
API for using the new date class of instant
How to write controller layer code gracefully?
吴恩达老师机器学习课程笔记 03 线性代数回顾
【冷冻电镜|论文阅读】子断层平均 M 软件解读:Multi-particle cryo-EM refinement with M
模拟卷Leetcode【普通】172. 阶乘后的零
CNN convolutional neural network
Hongke share | let you have a comprehensive understanding of "can bus errors" (IV) -- producing and recording can errors in practice
王树尧老师运筹学课程笔记 10 线性规划与单纯形法(关于检测数与退化的讨论)
Teacher wangshuyao's notes on operations research 01 guidance and introduction
会话推荐中的价格偏好和兴趣偏好共同建模-论文泛读
Idea cannot find a database solution
NeuralCF-神经协同过滤网络
【冷冻电镜入门】加州理工公开课课程笔记 Part 3: Image Formation
Computer right mouse click always turn around what's going on
吴恩达老师机器学习课程笔记 05 Octave教程
Mutual conversion between Base64 and file
NLP word segmentation
