无聊之作-LeetCode Solve the Equation

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_coef一个num_coef分别用于存x的系数和数字的系数,最后如果这两个都等于0的话就是无限个解,如果x系数为0数字不为0就返回没有解决方案,其他的就返回num_coef/x_coef。

过程如下,在等式的左侧,

遍历到x的时候,将x_coef加上x的系数,

遍历到数字的时候,将num_coef减去该数字,相当于将该数字移动到了等式右边。

在等式的右侧时,

遍历到x的时候,将x_coef减去x的系数,相当于将该x移动到了等式的左边,

遍历到数字的时候,将num_coef加上该数字

其他的就涉及到一些储存符号,边界判断,等式左右判断的了

class Solution:
    def solveEquation(self, equation: str) -> str:
        #x的系数
        x_coef=0
        #数字
        num_coef=0
        #储存符号的flag
        add_flag=True
        #储存上一个数字
        last_str=''
        #等式左右两边的flag,False为等式左侧,True为右侧
        reverse_flag=False
        #遍历每个字符
        for i in range(len(equation)):
            if equation[i]=='+' or equation[i]=='-' or equation[i]=='=':
                #当上一个数字中含有x的时候,说明是x的系数
                if 'x' in last_str:
                    #当last_str="x",即x的系数等于1时
                    if len(last_str)==1:
                        x_num=1
                    else:
                        #x的系数不等于1时,获取到x的系数
                        x_num=int(last_str[0:len(last_str)-1])
                    #等式左侧
                    if reverse_flag:
                        #符号为-
                        if add_flag:
                            x_coef-=x_num
                        else:
                        #符号为+
                            x_coef+=x_num
                    else:
                        if add_flag:
                            x_coef+=x_num
                        else:
                            x_coef-=x_num
                #上一个数字是纯数字,不含有x
                else:
                    #用于判断=后边的直接出现数字的情况
                    if len(last_str)==0:
                        if equation[i]=='+':
                            add_flag=True
                        elif equation[i]=='-':
                            add_flag=False
                        continue
                    #获取到上一个数字
                    num_num=int(last_str)
                    #在等式左侧
                    if reverse_flag:
                        #符号为-
                        if add_flag:
                            num_coef+=num_num
                        else:
                            num_coef-=num_num
                    else:
                        if add_flag:
                            num_coef-=num_num
                        else:
                            num_coef+=num_num
                last_str=""
                if equation[i]=='+':
                    add_flag=True
                elif equation[i]=='-':
                    add_flag=False
            else:
                last_str+=equation[i]
            #当遇到=时,对等式左右两侧的flag进行操作
            if  equation[i]=='=':
                reverse_flag=True
                add_flag=True
            #print(x_coef,num_coef)
        #最后结束的再进行一次操作
        if 'x' in last_str:
            if len(last_str)==1:
                x_num=1
            else:
                x_num=int(last_str[0:len(last_str)-1])
            if reverse_flag:
                if add_flag:
                    x_coef-=x_num
                else:
                    x_coef+=x_num
            else:
                if add_flag:
                    x_coef+=x_num
                else:
                    x_coef-=x_num
        else:
            num_num=int(last_str)
            if reverse_flag:
                if add_flag:
                    num_coef+=num_num
                else:
                    num_coef-=num_num
            else:
                if add_flag:
                    num_coef-=num_num
                else:
                    num_coef+=num_num
        #print(x_coef,num_coef)
        
        
        if x_coef==0 and num_coef==0:
            return "Infinite solutions"
        elif x_coef==0:
            return "No solution"
        else:
            return "x="+str(num_coef//x_coef)
        
发布了164 篇原创文章 · 获赞 36 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/py184473894/article/details/103705806