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 == ) return a;
return gcd(b, a%b);
}
string fractionAddition(string expression) {
vector<int> numerator;
vector<int> denominator;
int j = ;
bool positive = false;
if(expression[] >= '' && expression[] <= '') positive = true;
else {
positive = false;
j = ;
}
string tmp;
for(size_t i=j+; i<expression.size(); i++) {
if(expression[i] == '+' || expression[i] == '-') {
tmp = expression.substr(j, i-j);
size_t k;
for(k = ; k < tmp.size(); k++) {
if(tmp[k] == '/') break;
}
numerator.push_back(stoi(tmp.substr(,k)));
if(!positive) numerator[numerator.size()-] *= -;
positive = expression[i] == '+' ? true : false;
denominator.push_back(stoi(tmp.substr(k+)));
j = i+;
}
}
tmp = expression.substr(j,expression.size()-j);
size_t k;
for(k=; k<tmp.size(); k++)
if(tmp[k] == '/') break;
numerator.push_back(stoi(tmp.substr(,k)));
if(!positive) numerator[numerator.size()-] *= -;
denominator.push_back(stoi(tmp.substr(k+)));
// test long long ret_numer = numerator[];
long long ret_denomi = denominator[];
long long r = ;
for(size_t i= ; 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 > ) {
r = gcd(ret_numer, ret_denomi);
ret_numer /= r;
ret_denomi /= r;
} else if (ret_numer < ) {
r = gcd(-ret_numer, ret_denomi);
ret_numer /= r;
ret_denomi /= r;
} else if(ret_numer == ) {
ret_denomi = ;
}
string str_numer = ret_numer < ? ("-" + 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 = , den = , NUM = , DEN = ;
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 == ? x : gcd(y, x % y);
}
};

最新文章

  1. 【IT】公司FTP服务器使用说明
  2. Angularjs学习笔记(四)----与后端服务器通信
  3. 【Visual Lisp】人机交互与数据处理(表除外)-lisp
  4. Java中的super与this解析
  5. python leetcode 日记--231. Power of Two
  6. PIVOT 用于将列值旋转为列名
  7. 解决dede搜索页面只能显示10条信息解决方案
  8. mybatis 入门二
  9. 【制作镜像】virsh
  10. 使用游标循环进行SQL更新插入的SQL语句
  11. 初识KMP
  12. 【设计模式:单例模式】使用单例模式载入properties文件
  13. C#字符串常见操作总结
  14. MapReduce深度分析(二)
  15. 谈谈对Spring IOC的理解(转载)
  16. Window Server 布署 WCF 服务 , 权限配置问题
  17. koa-router 后台路由管理框架
  18. 使用POI读写Word doc文件
  19. lesson 8:小程序
  20. PyQt5--CloseWindow

热门文章

  1. vagrant 搭建开发环境
  2. vue v-cloak 指令 处理页面显示源码
  3. insert buffer/change buffer double write buffer,双写 adaptive hash index(AHI) innodb的crash recovery innodb重要参数 innodb监控
  4. Error creating bean with name &#39;objectMapperConfigurer&#39; defined in class path resource
  5. Exams(二分
  6. python3 多线程和多进程
  7. JS 禁用按钮10秒方法
  8. python panda读写内存溢出:MemoryError
  9. Java8-Executors-No.02
  10. C# IFormattable 接口重写