力扣题:数字与字符串间转换-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;
            }
        }
    }
};

相关推荐

  1. 第八——字符串转换整数

    2023-12-16 06:28:02       31 阅读
  2. 面试经典数组/字符串

    2023-12-16 06:28:02       66 阅读
  3. 每日一2744最大字符串配对数目

    2023-12-16 06:28:02       62 阅读
  4. 数据结构算法】 344. 反转字符串

    2023-12-16 06:28:02       44 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-16 06:28:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-16 06:28:02       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-16 06:28:02       87 阅读
  4. Python语言-面向对象

    2023-12-16 06:28:02       96 阅读

热门阅读

  1. x86上运行LoongArch的环境

    2023-12-16 06:28:02       63 阅读
  2. TCP标志位

    2023-12-16 06:28:02       56 阅读
  3. 5G基础设施:2024-2028年市场趋势与预测

    2023-12-16 06:28:02       59 阅读
  4. 华为云CodeArts Pipeline常见问答汇总

    2023-12-16 06:28:02       58 阅读
  5. 华为云CodeArts Check常见问答汇总

    2023-12-16 06:28:02       61 阅读
  6. 新时代商业市场:AR技术的挑战与机遇并存

    2023-12-16 06:28:02       54 阅读