LC 592. Fraction Addition and Subtraction

Posted ethanhong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LC 592. Fraction Addition and Subtraction相关的知识,希望对你有一定的参考价值。

 

Given a string representing an expression of fraction addition and subtraction, you need to return the calculation result in string format. The final result should be irreducible fraction. If your final result is an integer, say 2, you need to change it to the format of fraction that has denominator 1. So in this case, 2 should be converted to 2/1.

Example 1:

Input:"-1/2+1/2"
Output: "0/1"

 

Example 2:

Input:"-1/2+1/2+1/3"
Output: "1/3"

 

Example 3:

Input:"1/3-1/2"
Output: "-1/6"

 

Example 4:

Input:"5/3+1/3"
Output: "2/1"

 

Note:

  1. The input string only contains ‘0‘ to ‘9‘‘/‘‘+‘ and ‘-‘. So does the output.
  2. Each fraction (input and output) has format ±numerator/denominator. If the first input fraction or the output is positive, then ‘+‘ will be omitted.
  3. The input only contains valid irreducible fractions, where the numerator and denominator of each fraction will always be in the range [1,10]. If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above.
  4. The number of given fractions will be in the range [1,10].
  5. The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int.

 

Runtime: 8 ms, faster than 0.00% of C++ online submissions for Fraction Addition and Subtraction.
Memory Usage: 5.1 MB, less than 0.00% of C++ online submissions for Fraction Addition and Subtraction.

 

 

class Solution {
public:
    long long gcd(long long a, long long b) {
        if(a < b) return gcd(b,a);
        if(b == 0) return a;
        return gcd(b, a%b);
    }
    string fractionAddition(string expression) {
        vector<int> numerator;
        vector<int> denominator;
        int j = 0;
        bool positive = false;
        if(expression[0] >= 0 && expression[0] <= 9) positive = true;
        else {
            positive = false;
            j = 1;
        }
        string tmp;
        for(size_t i=j+1; i<expression.size(); i++) {
            if(expression[i] == + || expression[i] == -) {
                tmp = expression.substr(j, i-j);
                size_t k;
                for(k = 0; k < tmp.size(); k++) {
                    if(tmp[k] == /) break;
                }
                numerator.push_back(stoi(tmp.substr(0,k)));
                if(!positive) numerator[numerator.size()-1] *= -1;
                positive = expression[i] == + ? true : false;
                denominator.push_back(stoi(tmp.substr(k+1)));
                j = i+1;
            }
        }
        tmp = expression.substr(j,expression.size()-j);
        size_t k;
        for(k=0; k<tmp.size(); k++)
            if(tmp[k] == /) break;
        numerator.push_back(stoi(tmp.substr(0,k)));
        if(!positive) numerator[numerator.size()-1] *= -1;
        denominator.push_back(stoi(tmp.substr(k+1)));
// test




        long long ret_numer = numerator[0];
        long long ret_denomi = denominator[0];
        long long r = 0;
        for(size_t i= 1; i<denominator.size(); i++) {
            long long tmp_deno = ret_denomi;
            ret_denomi *= denominator[i];
            ret_numer = ret_numer * denominator[i] + tmp_deno * numerator[i];
        }
        if(ret_numer > 0) {
            r = gcd(ret_numer, ret_denomi);
            ret_numer /= r;
            ret_denomi /= r;
        } else if (ret_numer < 0) {
            r = gcd(-ret_numer, ret_denomi);
            ret_numer /= r;
            ret_denomi /= r;
        } else if(ret_numer == 0) {
            ret_denomi = 1;
        }
        string str_numer = ret_numer < 0 ? ("-" + to_string(-ret_numer)) : to_string(ret_numer);
        string str_deno  = to_string(ret_denomi);
        return str_numer + "/" + str_deno;
  }
};

 

 更好的一个思路

 

 

class Solution {
public:
    string fractionAddition(string expression) {
        istringstream iss(expression);
        int num = 0, den = 0, NUM = 0, DEN = 1;
        char c;
        while (iss >> num >> c >> den)
        {
            NUM = NUM * den + num * DEN;
            DEN *= den;
            int g = abs(gcd(NUM, DEN));
            NUM /= g;
            DEN /= g;
        }
        
        return to_string(NUM) + "/" + to_string(DEN);
    }
    
    int gcd(int x, int y)
    {
        return y == 0 ? x : gcd(y, x % y);
    }
};

 

 

以上是关于LC 592. Fraction Addition and Subtraction的主要内容,如果未能解决你的问题,请参考以下文章

java 592. Fraction Addition和Subtraction.java

java 592. Fraction Addition和Subtraction.java

java 592. Fraction Addition和Subtraction.java

java 592. Fraction Addition和Subtraction.java

java 592. Fraction Addition和Subtraction.java

java 592. Fraction Addition和Subtraction.java