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

力扣题-12.16

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

力扣题1:640. 求解方程

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

力扣题:数字与字符串间转换-12.16_第1张图片

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;
            }
        }
    }
};

你可能感兴趣的:(leetcode,算法,职场和发展)