无聊之作-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)
        

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值