LeetCode 640 : Solve the Equation(python)
2017-07-11 11:08
501 查看
原题:
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的一些操作和求解方程组的数学知识,测试的时候要充分考虑各种情况。
代码:
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))
相关文章推荐
- [LeetCode] Solve the Equation 解方程
- 640. Solve the Equation
- 640. Solve the Equation
- 640. Solve the Equation
- Math-640-Solve the Equation
- leetcode 640. Solve the Equation
- 640. Solve the Equation
- LeetCode Weekly Contest 40(2) - Solve the Equation
- [leetcode]640. Solve the Equation
- Leetcode 5. Longest Palindromic Substring The Solution of Python
- 【python】【leetcode】【算法题目389—Find the Difference】
- Leetcode 6. ZigZag Conversion The Solution of Python
- Leetcode--Find the Difference[python]
- Solve the Equation问题及解法
- [leetcode: Python]389. Find the Difference
- LeetCode--Find the Duplicate Number(查找重复数字)Python
- Use the BFS to solve the equation problems(Case : hdu 4403 )
- Solve the Equation
- Leetcode 4. Median of Two Sorted Arrays The Solution of Python
- Can you solve the equation?(二分求方程解)