当前位置:网站首页>力扣解法汇总592-分数加减运算
力扣解法汇总592-分数加减运算
2022-07-27 18:11:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:
描述:
给定一个表示分数加减运算的字符串 expression ,你需要返回一个字符串形式的计算结果。
这个结果应该是不可约分的分数,即最简分数。 如果最终结果是一个整数,例如 2,你需要将它转换成分数形式,其分母为 1。所以在上述例子中, 2 应该被转换为 2/1。
示例 1:
输入: expression = "-1/2+1/2"
输出: "0/1"
示例 2:
输入: expression = "-1/2+1/2+1/3"
输出: "1/3"
示例 3:
输入: expression = "1/3-1/2"
输出: "-1/6"
提示:
输入和输出字符串只包含 '0' 到 '9' 的数字,以及 '/', '+' 和 '-'。
输入和输出分数格式均为 ±分子/分母。如果输入的第一个分数或者输出的分数是正数,则 '+' 会被省略掉。
输入只包含合法的最简分数,每个分数的分子与分母的范围是 [1,10]。 如果分母是1,意味着这个分数实际上是一个整数。
输入的分数个数范围是 [1,10]。
最终结果的分子与分母保证是 32 位整数范围内的有效整数。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/fraction-addition-and-subtraction
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 把字符串拆分为一个一个的节点,每个节点包含分子,分母,以及运算符。 * 第一个节点的话如果负数开头那运算符就是负,否则为正。 * 把这些节点从头开始一个一个计算,分母相等直接计算,否则分母相乘。最后求出来的那个节点的值进行约分处理就是最后的结果。
代码:
public class Solution592 {
public String fractionAddition(String expression) {
int lastIndex = 0;
List<Node> list = new ArrayList<>();
char[] chars = expression.toCharArray();
for (int i = 0; i <= chars.length; i++) {
if (i == expression.length()) {
Node paser = paser(expression.substring(lastIndex, i));
list.add(paser);
continue;
}
int value = chars[i];
if (value == '-' || value == '+') {
Node paser = paser(expression.substring(lastIndex, i));
if (paser != null) {
list.add(paser);
}
lastIndex = i;
continue;
}
}
Node lastNode = new Node();
for (int i = 0; i < list.size(); i++) {
Node node = list.get(i);
lastNode = calculation(lastNode, node);
}
if (lastNode.current == 0) {
lastNode.current2 = 1;
} else {
//约分计算
int i = 2;
while (i <= Math.abs(lastNode.current)) {
if (lastNode.current % i == 0 && lastNode.current2 % i == 0) {
lastNode.current /= i;
lastNode.current2 /= i;
i = 2;
continue;
}
i++;
}
}
return lastNode.current + "/" + lastNode.current2;
}
private Node paser(String str) {
Node node = new Node();
if (str.length() == 0) {
return null;
}
int index = 0;
int lastIndex = 0;
char[] chars = str.toCharArray();
while (index <= str.length()) {
if (index == str.length()) {
node.current = Integer.parseInt(str.substring(lastIndex));
break;
}
int value = chars[index++];
if (value == '-') {
node.isAdd = false;
lastIndex++;
continue;
}
if (value == '/') {
node.current = Integer.parseInt(str.substring(lastIndex, index - 1));
node.current2 = Integer.parseInt(str.substring(index));
break;
}
}
return node;
}
private Node calculation(Node node1, Node node2) {
Node node = new Node();
if (node1.current2 == node2.current2) {
node.current = node2.isAdd ? node1.current + node2.current : node1.current - node2.current;
node.current2 = node1.current2;
return node;
}
int i1 = node1.current * node2.current2;
int i2 = node2.current * node1.current2;
node.current = node2.isAdd ? i1 + i2 : i1 - i2;
node.current2 = node1.current2 * node2.current2;
return node;
}
public static class Node {
boolean isAdd = true;
int current = 0;//分子
int current2 = 1;//分母
}
}边栏推荐
- Check the internship salary of Internet companies: with it, you can also enter the factory
- Why does Alibaba prohibit more than three forms from joining?
- MySQL string function
- 用户和权限撤销用户权限
- JVS基础框架功能列表
- Analysis on the optimization of login request in IM development of instant messaging mobile terminal
- 一个程序员的水平能差到什么程度?
- Arduino开发(二)_基于Arduino UNO开发板的RGB灯光控制方法
- 【深度学习】Pytorch torch.autograd 自动差分引擎
- RK3399平台开发系列讲解(进程篇)15.36、理解进程和协程
猜你喜欢

【程序人生】“阶段总结“-不甘平凡
![[efficiency] abandon notepad++, this open source substitute is more awesome!](/img/41/495bbe4d1e6d953ba5c4d8984f81e7.jpg)
[efficiency] abandon notepad++, this open source substitute is more awesome!

【深度学习】视频分类技术整理

JVS公众号登陆配置

面了个腾讯拿38K跳槽出来的,见识到了真正的测试天花板

antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key

Injection attack

Lennix Lai, OKx financial market director: Web3 is a revolution

【效率】弃用 Notepad++,这款开源替代品更牛逼!

EasyCVR平台添加RTSP设备时,出现均以TCP方式连接的现象是什么原因?
随机推荐
Mlx90640 infrared thermal imager temperature sensor module development notes (VII)
金仓数据库 KingbaseES异构数据库移植指南 (2. 概述)
Idea: solve the problem of code without prompt
Tencent jumped out with 38K and saw the real test ceiling
用户和权限修改用户密码
Software test interview question: given a queue, such as: [1, 3, 5, 7], how to put the first number into the third position to get: [3, 5, 1, 7]
一个程序员的水平能差到什么程度?
MySQL basic queries and operators
【深度学习】Pytorch Tensor 张量
How bad can a programmer be?
Linked list~~~
How to optimize the open source community experience through developer metrics
2022.07.11
My approval of OA project (Query & meeting signature)
软件测试面试题:字符串 “axbyczdj“,如果得到结果“abcd
openresty lua-resty-core 使用
软件测试面试题:已知一个数字为1,如何输出“0001
Koin simple to use
Jetpack Compose 性能优化指南——编译指标
Users and permissions create ordinary users