当前位置:网站首页>592. Fraction Addition and Subtraction
592. Fraction Addition and Subtraction
2022-07-30 17:00:00 【soO_007】
题目:
Given a string expression
representing an expression of fraction addition and subtraction, return the calculation result in string format.
The final result should be an irreducible fraction. If your final result is an integer, change it to the format of a fraction that has a denominator 1
. So in this case, 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"
Constraints:
- The input string only contains
'0'
to'9'
,'/'
,'+'
and'-'
. So does the output. - Each fraction (input and output) has the 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 is1
, 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.
思路:
单纯模拟题,需要掌握两点,一个是最大公约数,用辗转相除法即可,但是要注意-1,比较的时候需要用abs;另一个是。首先需要四个数,up表示当前数分子,down表示当前数分母,当前数即up / down,初始化肯定是0,但是避免分母为0,我们把up初始化为0,down初始化为1,不影响结果;同理使用nextup和nextdown记录下一个数,用sign记录下一个数的正负,初始化为1即可。另外需要一个数为cur,记录当前数字,因为可能是111,则需要每次进位,即每次cur * 10 + 当前expression[i]的int即可。之后遍历expression,每次遇到加号或者减号就进行一轮计算,首先如果下个数字的分母是0,我们赋为1,防止分母为0。之后为nextup计算符号,套用公式获得新的up和down即可。这里记得重置cur和sign,因为sign是代表下一个数字的正负,因此当前expression[i]的加减号是当计算完新的up和down以后才赋值给sign。最后在遍历完成后加上最后一个数,再得出gcd,对up和down都除以最大公约数,再转成string就是答案了。
代码:
class Solution {
public:
string fractionAddition(string expression) {
int up = 0, down = 1;
int i = 0, sign = 1;
int nextup = 0, nextdown = 1;
int cur = 0;
while (i < expression.size()) {
if (expression[i] == '-') {
nextdown = cur == 0 ? 1 : cur;
nextup *= sign;
up = up * nextdown + down * nextup;
down = down * nextdown;
sign = -1;
cur = 0;
} else if (expression[i] == '+') {
nextdown = cur == 0 ? 1 : cur;
nextup *= sign;
up = up * nextdown + down * nextup;
down = down * nextdown;
sign = 1;
cur = 0;
} else if (expression[i] == '/') {
nextup = cur;
cur = 0;
} else {
cur = cur * 10 + (expression[i] - '0');
}
i++;
}
nextdown = cur == 0 ? 1 : cur;
nextup *= sign;
up = up * nextdown + down * nextup;
down = down * nextdown;
sign = up >= 0 ? 1 : -1;
int g = abs(gcd(up, down));
up /= g;
down /= g;
string ans = to_string(up) + '/' + to_string(down);
return ans;
}
private:
int gcd(int a, int b) {
if (abs(a) < abs(b))
return gcd(b, a);
if (b == 0)
return a;
return gcd(a % b, b);
}
};
边栏推荐
- [NCTF2019]Fake XML cookbook-1|XXE漏洞|XXE信息介绍
- CMake库搜索函数居然不搜索LD_LIBRARY_PATH
- data storage
- MySQL索引常见面试题(2022版)
- LeetCode167:有序数组两数之和
- SLIM: Sparse Linear Methods (TopN推荐)
- Scheduling_Channel_Access_Based_on_Target_Wake_Time_Mechanism_in_802.11ax_WLANs
- Moonbeam创始人解读多链新概念Connected Contract
- 云厂商做生态需要“真连接、真赋能”,用“技术+真金实银”发展伙伴
- SwiftUI SQLite教程之带有历史的搜索栏List App (教程含完整代码)
猜你喜欢
Wanhua chemical fine chemical industry innovation product assembly
Public Key Retrieval is not allowed error solution
Paper reading (63): Get To The Point: Summarization with Pointer-Generator Networks
MySQL超详细安装教程 手把手教你安装MySQL到使用MySQL 最简单的MySQL安装方式,这种方式装,卸载也简单
SQLServer下载与安装
有没有并发系统设计的经验,我该怎么说?
[Geek Challenge 2020] Roamphp1-Welcome
华为云数据治理生产线DataArts,让“数据'慧'说话”
(17)[系统调用]追踪系统调用(0环)
查询表中开始日期与结束日期
随机推荐
PHP message feedback management system source code
【综合类型第 34 篇】喜讯!喜讯!!喜讯!!!,我在 CSDN 的第一个实体铭牌
全职做自媒体靠谱吗?
向量检索基础方法总结
Public Key Retrieval is not allowed error solution
What does a good resume look like in the eyes of a big factory interviewer?
一篇文 带你搞懂,虚拟内存、内存分页、分段、段页式内存管理(超详细)
Leetcode 118. Yanghui Triangle
获得抖音商品详情 API
Various meanings of SQL's PARTITION BY syntax (with examples)
说几个大厂分库分表的那点破事。
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
有没有并发系统设计的经验,我该怎么说?
onenote使用
No qualifying bean of type问题解决
FP6600QSO SOP-8 USB专用充电端口控制器 用于快充电协议和QC2.0/3.0
真正懂经营管理的CIO具备哪些特质
实现web实时消息推送的7种方案
Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)
如何在 UE4 中用代码去控制角色移动