LeetCode 640 : Solve the Equation(python)

来源:互联网 发布:开实体店在淘宝上拿货 编辑:程序博客网 时间:2024/05/21 17:30

原题:

Solve a given equation and return the value of x in the form of string “x=#value”. The equation contains only ‘+’, ‘-’ operation, the variable x and its coefficient.

If there is no solution for the equation, return “No solution”.

If there are infinite solutions for the equation, return “Infinite solutions”.

If there is exactly one solution for the equation, we ensure that the value of x is an integer.

Example 1:
Input: “x+5-3+x=6+x-2”
Output: “x=2”
Example 2:
Input: “x=x”
Output: “Infinite solutions”
Example 3:
Input: “2x=x”
Output: “x=0”
Example 4:
Input: “2x+3x-6x=x+2”
Output: “x=-1”
Example 5:
Input: “x=x+2”
Output: “No solution”

思路:
本题要求求解方程组,并且假定方程组只有加和减两种运算,同时只含有x一个未知数。
解题的思路是:(1)先把等式分割为左右两边(2)通过mergeEquation函数分别简化左右两边,即计算出左右两侧x的系数和常数值,记为left_xCoefficient,left_constant,right_xCoefficient,right_constant(3)把含有x的项移到左边,把常数项移到右边,其系数分别简化为merged_xCoefficient,merged_constant,然后通过求解方程组的基本知识分类求出答案。
本题考查字符串、list的一些操作和求解方程组的数学知识,测试的时候要充分考虑各种情况。

代码:

class Solution:    def mergeEquation(self, equation):        #signlist存储每一项的正负,mergelist存储每一项的内容        #signlist和mergelist一一对应        signlist,mergelist=[],[]        #xCoefficient存储x的系数,constant存储常数项        xCoefficient,constant=0,0        #生成signlist        if(equation[0]=='-'):            signlist.append('-')        else:            signlist.append('+')        for i in range(len(equation)):            if(equation[i]=='+'):                signlist.append('+')            elif(equation[i]=='-'):                signlist.append('-')        #通过“+”和“-”分割生成mergelist        add_split=equation.split('+')        for i in range(len(add_split)):            mergelist.extend(add_split[i].split('-'))        for i in range(len(mergelist)):            #若出现“-x=-1”的情况,"-"会把左边分割成“”和“x”两项            if(len(mergelist[i])==0):                continue            #处理含有x的项            if(mergelist[i][-1]=='x'):                if(signlist[i]=='+'):                    #当mergelist[i]=‘x’时,mergelist[i][0:-1]=‘’,int('')会抛出异常                    try:                        xCoefficient+=int(mergelist[i][0:-1])                    except:                        xCoefficient+=1                else:                    try:                        xCoefficient-=int(mergelist[i][0:-1])                    except:                        xCoefficient-=1            #处理不含x的项,即常数项            else:                if(signlist[i]=='+'):                    constant+=int(mergelist[i])                else:                    constant-=int(mergelist[i])        return xCoefficient,constant    def solveEquation(self, equation):        """        :type equation: str        :rtype: str        """        #把等式分割成左右两项        left,right=equation.split('=',2)        #通过计算,合并成只含一个x和一个常数的项        #左右两侧x的系数和常数值,记为left_xCoefficient,left_constant,right_xCoefficient,right_constant        left_xCoefficient,left_constant=self.mergeEquation(left)        right_xCoefficient,right_constant=self.mergeEquation(right)        #方程组计算        merged_xCoefficient,merged_constant=left_xCoefficient-right_xCoefficient,right_constant-left_constant        if((merged_xCoefficient==0)and(merged_constant==0)):            return "Infinite solutions"        elif((merged_xCoefficient==0)and(merged_constant!=0)):            return "No solution"        else:            return "x="+str(int(merged_constant//merged_xCoefficient))
原创粉丝点击