力扣解法汇总592-分数加减运算

 目录链接:

力扣编程题-解法汇总_分享+记录-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;//分母

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
题目描述 柠檬水找零:在柠檬水摊上,每一杯柠檬水的售价为 5 美元。 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一个。 每位顾客只买一杯柠檬水,然后向你支付 5 美元、10 美元或 20 美元。你必须给每个顾客正确找零,也就是说净交易是每位顾客向你支付 5 美元。 注意,一开始你手头没有任何零钱。 如果你能给每位顾客正确找零,返回 true ,否则返回 false 示例 1: 输入:[5,5,5,10,20] 输出:true 解释: 前 3 位顾客那里分别支付了 5 美元。 第 4 位顾客那里支付了 10 美元,接下来是两个 5 美元。 第 5 位顾客那里支付了 20 美元,接下来是 15 美元, 无法提供且返回false。 示例 2: 输入:[5,5,10] 输出:true 示例 3: 输入:[10,10] 输出:false 示例 4: 输入:[5,5,10,10,20] 输出:false 解题思路 使用两个变量 five 和 ten 分别表示手头上的 5 美元钞票和 10 美元钞票的数量。从前往后遍历数组,根据顾客支付的钞票进行分类讨论: 如果顾客支付 5 美元,收入 5 美元钞票一个。 如果顾客支付 10 美元,需要找回一张 5 美元钞票,收入 10 美元钞票一张和减去一张 5 美元钞票。 如果顾客支付 20 美元,优先找回一张 10 美元和一张 5 美元,如果没有再找回三张 5 美元,否则收入不够减,返回 false。 算法流程 遍历 bills,记当前手上拥有的 5 美元张数 five 和 10 美元张数 ten 的数量,初始值为 0。 判断 bills[i] 的大小+0、+5 还是+15,并更新 five 和 ten 的数量。 代码实现 class Solution(object): def lemonadeChange(self, bills): """ :type bills: List[int] :rtype: bool """ five, ten = 0, 0 for bill in bills: if bill == 5: five += 1 elif bill == 10: if not five: return False five -= 1 ten += 1 else: if ten and five: ten -= 1 five -= 1 elif five >= 3: five -= 3 else: return False return True

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

失落夏天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值