力扣题:数字与字符串间转换-12.16

力扣题-12.16

[力扣刷题攻略] Re:从零开始的力扣刷题生活

力扣题1:640. 求解方程

解题思想:首先将方程按照“=”进行划分,然后分别记录x的因数和常数项,最后进行返回的判断即可

在这里插入图片描述

class Solution(object):
    def solveEquation(self, equation):
        """
        :type equation: str
        :rtype: str
        """
        number = 0
        factor = 0

        temp = equation.split("=")
        left = temp[0]
        right = temp[1]
        left = left.replace('-', '+-')
        left = left.split("+")
        print(left)
        for i in range(len(left)):
            if left[i]!='':
                temp_number = 0
                temp_factor = 0
                if left[i] =='x':
                    temp_factor = 1
                elif left[i][-1] =='x':
                    if left[i][:-1] =='-':
                        temp_factor = -1
                    else:
                        temp_factor = int(left[i][:-1]) 
                else:
                    temp_number = int(left[i])
                factor = factor+temp_factor
                number = number+temp_number

        right = right.replace('-', '+-')
        right = right.split("+")
        for i in range(len(right)):
            if right[i]!='':
                temp_number = 0
                temp_factor = 0
                if right[i] =='x':
                    temp_factor = -1
                elif right[i][-1] =='x':
                    if right[i][:-1] =='-':
                        temp_factor = 1
                    else:
                        temp_factor = int(right[i][:-1]) *-1
                else:
                    temp_number = int(right[i])*-1
                factor = factor+temp_factor
                number = number+temp_number
        if factor ==0 and number==0:
            return "Infinite solutions"
        elif factor ==0:
            return "No solution"
        else:
            return "x="+str(number/factor*-1)
class Solution {
public:
    string solveEquation(string equation) {
        int number = 0;
        int factor = 0;

        std::vector<std::string> temp = splitEquation(equation, '=');
        std::string left = temp[0];
        std::string right = temp[1];

        left = replace_minus_with_plus_dash(left);
        std::vector<std::string> leftTokens = splitExpression(left, '+');
        processTokens(leftTokens, number, factor);

        right = replace_minus_with_plus_dash(right);
        std::vector<std::string> rightTokens = splitExpression(right, '+');
        processTokens(rightTokens, number, factor, true);

        if (factor == 0 && number == 0) {
            return "Infinite solutions";
        } else if (factor == 0) {
            return "No solution";
        } else {
            return "x=" + std::to_string(-number / factor);
        }
    }

private:
    std::vector<std::string> splitEquation(std::string str, char delimiter) {
        std::vector<std::string> result;
        size_t pos = str.find(delimiter);
        result.push_back(str.substr(0, pos));
        result.push_back(str.substr(pos + 1));
        return result;
    }

    std::string replace_minus_with_plus_dash(std::string str) {
        size_t found = str.find("-");
        while (found != std::string::npos) {
            str.replace(found, 1, "+-");
            found = str.find("-", found + 2);
        }
        return str;
    }

    std::vector<std::string> splitExpression(std::string str, char delimiter) {
        std::vector<std::string> result;
        size_t pos = 0;
        while ((pos = str.find(delimiter)) != std::string::npos) {
            result.push_back(str.substr(0, pos));
            str.erase(0, pos + 1);
        }
        result.push_back(str);
        return result;
    }

    void processTokens(const std::vector<std::string>& tokens, int& number, int& factor, bool isRight = false) {
        int multiplier = isRight ? -1 : 1;
        for (size_t i = 0; i < tokens.size(); ++i) {
            if (!tokens[i].empty()) {
                int temp_number = 0;
                int temp_factor = 0;
                if (tokens[i] == "x") {
                    temp_factor = 1 * multiplier;
                } 
                else if(tokens[i] == "-x"){
                    temp_factor = -1 * multiplier;
                }
                else if (tokens[i].back() == 'x') {
                    temp_factor = std::stoi(tokens[i].substr(0, tokens[i].size() - 1)) * multiplier;
                } else {
                    temp_number = std::stoi(tokens[i]) * multiplier;
                }
                factor += temp_factor;
                number += temp_number;
            }
        }
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值