• 640.Solve the Equation


    The Description of the Problem

    Solve a given equation and return the value of x in the form of a string x=#value. The equation contains only +,- operation, the variable x and its coefficient. You should return No solution if there is no solution for the equation, or Infinite solutions if there are infinite solutions for the equation.
    If there is exactly one solution for the equation, we ensure that the value of x is an integer.

    The intuition for this

    1. The main idea is megering parts in same ending.
    2. transfer all the elements from right to the left
    3. add all the coefficients and constants
    4. compare the value of summarization of coefficients and constants.

    The code 1

    #include 
    #include 
    #include 
    class Solution {
    public:
        std::string solveEquation(std::string equation) {
            int results[2] = {0, 0}; // first element is the summation of coefficients, second element is the summation of constants
            int sign = 1;
            int i = 0;
            int n = equation.size();
            while (i < n) {
                if (equation[i] == '=') {
                    sign = -1;
                    i++;
                    continue;
                }
                int sign_part = sign;
                if (equation[i] == '+' || equation[i] == '-') {
                    sign_part = (equation[i] == '+' ? 1 : -1) * sign;
                    i++;
                }
                int val = 0;
                bool is_valid = false;
                while (i < n && isdigit(equation[i])) {
                    val = val * 10 + equation[i] - '0';
                    i++;
                    is_valid = true;
                }
                if (i < n && equation[i] == 'x') {
                    results[0] += is_valid ? sign_part * val : sign_part;
                    i++;
                } else {
                    results[1] += sign_part * val;
                }
            }
            if (results[0] == 0) {
                return results[1] == 0 ? "Infinite solutions" : "No solution";
            } else {
                return "x=" + std::to_string(-results[1]/results[0]);
            }
        }
    };
    int main()
    {
        Solution s;
        std::cout << "The resuts of x+5-3+x=6 is as follows:" << std::endl;
        std::cout << s.solveEquation("x+5-3+x=6+x-2") << std::endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    The results

    在这里插入图片描述

    The code 2 (mainly use substr())

    class Solution {
    public:
        string solveEquation(string equation) {
            string s = equation;
            // split s into two parts by '='
            int pos = s.find('=');
            string sLeft = s.substr(0, pos);
            string sRight = s.substr(pos + 1);
            // analyze each part into coefficents and constants by '+' and '-'
            vector<int> coefficients;
            vector<int> constants;
            analyzeString(sLeft, coefficients, constants, 1);
            analyzeString(sRight, coefficients, constants, -1);
            // add all the coefficents and constants
            int sum_coeff = 0, sum_const = 0;
            for (int i = 0; i < coefficients.size(); i++) {
                sum_coeff += coefficients[i];
            }
            for (int i = 0; i < constants.size(); i++) {
                sum_const += constants[i];
            }
            // return the result
            string result = "";
            if (!sum_coeff) {
                result = sum_const ? "No solution" : "Infinite solutions";
            } else {
                result = "x=" + to_string(-sum_const / sum_coeff);
            }
            return result;
        }
        void analyzeString(string s, vector<int>& coefficients, vector<int>& constants, int sign) {
            int sign_ = sign;
            if (s[0] == '+' || s[0] == '-') {
                s = s.substr(1);
                sign_  = sign * (s[0] == '+' ? 1 : -1);
            }
            int index = 0;
            while (index < s.size()) {
                string num = "";
                int sign_next = sign;
                if (s[index] == '+' || s[index] == '-') {
                    sign_next = s[index] == '+' ? 1 : -1;
                    num = s.substr(0, index);
                    if (!num.empty()) {
                        if (num.find('x') != string::npos) {
                            if (num[0] == 'x') {
                                coefficients.push_back(sign_);
                            } else {
                                coefficients.push_back(sign_ * stoi(num));
                            }
                        } else {
                            constants.push_back(sign_ * stoi(num));
                        }
                    }
                    s = s.substr(index + 1);
                    index = 0;
                    sign_ = sign * sign_next;
                } else {
                    index++;
                }
            }
            if (!s.empty()) {
                if (s.find('x') != string::npos) {
                    if (s[0] == 'x') {
                        coefficients.push_back(sign_);
                    } else {
                        coefficients.push_back(sign_ * stoi(s));
                    }
                } else {
                    constants.push_back(sign_ * stoi(s));
                }
            }
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    在这里插入图片描述

  • 相关阅读:
    trino安装及使用
    jvm学习
    A-LEVEL计算机科学得A*难吗?
    【leetcode】【剑指offer Ⅱ】046. 二叉树的右侧视图
    Fastdfs之多机房同步模拟测试
    UE5中实现沿样条线创建网格体2-SplineMesh版本
    神经网络如何识别图像,神经网络和图神经网络
    计算机毕业设计Python+djang基于大数据的应届生求职系统(源码+系统+mysql数据库+Lw文档)
    Open3D 点云法向量重定向(基于相机位置)
    正确的用户拖拽方式
  • 原文地址:https://blog.csdn.net/weixin_38396940/article/details/126311662