forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
66 lines (47 loc) · 1.64 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/// Source : https://leetcode.com/problems/fraction-addition-and-subtraction/
/// Author : liuyubobobo
/// Time : 2022-07-26
#include <iostream>
using namespace std;
/// String Parse
/// Time Complexity: O(|exp|)
/// Space Complexity: O(1)
class Solution {
public:
string fractionAddition(string expression) {
if(expression[0] != '-') expression = "+" + expression;
long long upper = 0, lower = 1;
for(int start = 0, i = 1; i <= expression.size(); i ++)
if(i == expression.size() || (expression[i] == '+' || expression[i] == '-')){
string f = expression.substr(start, i - start);
char op = f[0];
f = f.substr(1);
int div = f.find('/');
long long a = atoll(f.substr(0, div).c_str());
long long b = atoll(f.substr(div + 1).c_str());
if(op == '+'){
long long new_a = upper * b + a * lower;
long long new_b = b * lower;
upper = new_a, lower = new_b;
}
else{
long long new_a = upper * b - a * lower;
long long new_b = b * lower;
upper = new_a, lower = new_b;
}
long long g = gcd(abs(upper), abs(lower));
upper /= g, lower /= g;
start = i;
}
return to_string(upper) + "/" + to_string(lower);
}
private:
int gcd(long long a, long long b){
if(a > b) swap(a, b);
if (a == 0) return b;
return gcd(b % a, a);
}
};
int main() {
return 0;
}