# # 【LeetCode】640. Solve the Equation 解题报告（Python）

## # 题目描述：

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"
``````

## # 解题方法

``````class Solution(object):
def solveEquation(self, equation):
"""
:type equation: str
:rtype: str
"""
left, right = equation.split('=')
if left[0] == '-':
left = '0' + left
if right[0] == '-':
right = '0' + right
left = left.replace('-', '+-')
right = right.replace('-', '+-')
left_x, left_val, right_x, right_val = 0, 0, 0, 0
for num in left.split('+'):
if 'x' in num:
if num == 'x':
left_x += 1
elif num == '-x':
left_x -= 1
else:
left_x += int(num[:-1])
else:
left_val += int(num)
for num in right.split('+'):
if 'x' in num:
if num == 'x':
right_x += 1
elif num == '-x':
right_x -= 1
else:
right_x += int(num[:-1])
else:
right_val += int(num)
left_x -= right_x
right_val -= left_val
if left_x != 0 and right_val == 0:
return "x=0"
elif left_x != 0 and right_val != 0:
return 'x=' + str(right_val / left_x)
elif left_x == 0 and right_val == 0:
return "Infinite solutions"
elif left_x == 0 and right_val != 0:
return "No solution"
``````

## # 日期

2018 年 6 月 11 日 ———— 今天学科三在路上跑的飞快～