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:
- The input string only contains
'0'
to'9'
,'/'
,'+'
and'-'
. So does the output. - Each fraction (input and output) has format
±numerator/denominator
. If the first input fraction or the output is positive, then'+'
will be omitted. - 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.
- The number of given fractions will be in the range [1,10].
- The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int.
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);
}
};
LC 592. Fraction Addition and Subtraction的更多相关文章
- 【LeetCode】592. Fraction Addition and Subtraction 解题报告(Python)
[LeetCode]592. Fraction Addition and Subtraction 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuem ...
- [LeetCode] 592. Fraction Addition and Subtraction 分数加减法
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- 592. Fraction Addition and Subtraction
Problem statement: Given a string representing an expression of fraction addition and subtraction, y ...
- 【leetcode】592. Fraction Addition and Subtraction
题目如下: 解题思路:本题考察的是分数的加减法.小学时候就学过,分数的加减法是先求两个分母的最小公倍数,然后分子分别乘以最小公倍数与自己分母的商,相加后约分即可.所以,本题只要按+,-两个符号分割输入 ...
- [LeetCode] Fraction Addition and Subtraction 分数加减法
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- [Swift]LeetCode592. 分数加减运算 | Fraction Addition and Subtraction
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- [leetcode-592-Fraction Addition and Subtraction]
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- 大数据加减(Big data addition and subtraction)
题目描述 Description 加减法是计算中的基础运算,虽然规则简单,但是位数太多了,也难免会出错.现在的问题是:给定任意位数(不超过1000位)的加减法算式,请给出正确结果.为提高速度,保证给定 ...
- Arc066_E Addition and Subtraction Hard
传送门 题目大意 给定一个加减法的表达式,让你任意的添加合法的括号对,使的表达式最大. 题解 考虑到任意左括号一定加在减号右边,那么对于第一个左括号,与该左括号相邻的只含有加号的子序列的贡献一定为负, ...
随机推荐
- Hibernate入门第一讲——Hibernate框架的快速入门
Hibernate框架的概述 什么是框架? 框架指的是软件的半成品,已经完成了部分功能. JavaEE开发的三层架构 了解框架的基本概念之后,我们就来看看Hibernate框架处于JavaEE开发的经 ...
- 取出List<Map<String,Object>>里面Map的key:value值
1.取出Map其中一个属性的值 Map map = new HashMap(); map.put("key1", "value1"); map.put(&quo ...
- nginx__的简单搭建和 wsgi
4 部署 1 配置 uwsig(配置文件) pip3 install uwsgi 1 mkdir uwsgi 2 cd uwsgi 3 touch test_uwsig.ini [uwsgi] # 指 ...
- pycharm2017.3版本永久激活
1.下载破解文件 链接:https://pan.baidu.com/s/1nwI278l 密码:j3gt 2.修改检测文件 ,在文件后缀是vmoptions的 文件中加入(注意是文件中,不是文件后缀上 ...
- AppMain
@Controller@ComponentScan@Configuration@EnableScheduling@EnableAutoConfiguration(exclude={DataSource ...
- webpack 和 code splitting
Code Splitting指的是代码分割,那么什么是代码分割,webpack和code splitting又有什么样的联系呢? 使用npm run dev:"webpack-dev-ser ...
- 手写KMeans算法
KMeans算法是一种无监督学习,它会将相似的对象归到同一类中. 其基本思想是: 1.随机计算k个类中心作为起始点. 将数据点分配到理其最近的类中心. 3.移动类中心. 4.重复2,3直至类中心不再改 ...
- mysql总复习
目录 数据库操作 库操作 表操作 数据行操作 表关系操作 单表操作 外键创建 多表联查 pymysql模块 索引 主键索引 唯一索引 普通索引 数据库操作 库操作 create database 库名 ...
- Sql中的主键和外键
SQL的主键和外键的作用: 外键取值规则:空值或参照的主键值. (1)插入非空值时,如果主键表中没有这个值,则不能插入. (2)更新时,不能改为主键表中没有的值. (3)删除主键表记录时,你可以在建外 ...
- error: RPC failed; curl 18 transfer closed with outstanding read data remaining
报错: error: RPC failed; curl 18 transfer closed with outstanding read data remaining fatal: The remot ...