题目如下:

解题思路:本题的思路就是解析字符串,然后是小学时候学的解方程的思想,以"2x+3x-6x+1=x+2",先把左右两边的x项和非x项进行合并,得到"-x+1=x+2",接下来就是移项,把x项移到左边,常数项移到右边,得到"2x=-1",最后的解就是x=-1/2。对于任意一个表达式ax+b = cx+d来说,最终都能得到解x=(d-b)/(a-c),这里要对(a-c)是否为0做判断,同时根据(d-b)是否为0等到Infinite solutions,No solution,常规解三种结果。

代码如下:

class Solution(object):
def parse(self,expression):
x,v = 0,0
if expression[0] == '-':
expression = '' + expression
item = ''
operators = ['+', '-']
operator = ''
for i in (expression + '#'):
if i in operators or i == '#':
if item == 'x':
item = '1x'
if operator == '':
operator = i
if item[-1] == 'x':
x = int(item[:-1])
else:
v = int(item)
item = ''
else:
if operator == '+' and item[-1] == 'x':
x += int(item[:-1])
elif operator == '-' and item[-1] == 'x':
x -= int(item[:-1])
elif operator == '+' and item[-1] != 'x':
v += int(item)
else:
v -= int(item)
item = ''
operator = i
else:
item += i
return x,v
def solveEquation(self, equation):
"""
:type equation: str
:rtype: str
"""
left,right = equation.split('=')
lx,lv = self.parse(left)
rx,rv = self.parse(right) if lx - rx == 0 and rv - lv == 0:
return "Infinite solutions"
elif lx - rx == 0 and rv - lv != 0:
return "No solution"
else:
return "x=" + str((rv - lv)/(lx - rx))

【leetcode】640. Solve the Equation的更多相关文章

  1. 【LeetCode】640. Solve the Equation 解题报告(Python)

    [LeetCode]640. Solve the Equation 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

  2. 【LeetCode】9、Palindrome Number(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

  3. 【Leetcode】Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  4. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  5. 【LeetCode】692. Top K Frequent Words 解题报告(Python)

    [LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【Leetcode】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. Cytoscape基础教程笔记

    昨天开始学用Cytoscape,其tutorial分为两个部分,基础的和高级 的.基础教程又分成了四课:Getting Started.Filters & Editor.Fetching Ex ...

  2. 【Idea 】插件

    FindBugs-IDEA Maven Helper VisualVM Launcher GenerateAllSetter Rainbow Brackets Translation P3c(Alib ...

  3. win 10配置安装iis

    站长喜欢本地配置iss调试网站后发布到网上,但是前提是系统得配置好iis.随着Win10的出现,越来越多的人装上了Win10, 但是小编最近发现很多旧版本windows系统用户在升级到windows ...

  4. 从头开始开发一个vue幻灯片组件

    首先新建项目vue init webpack projectName 安装依赖包npm i这些就不说了 接下来就是构建我们的swiper组件 因为我写的代码不规范, 通不过eslint的检测, 会频繁 ...

  5. SQLserver查询作业、视图、函数、存储过程中的关键字

    一.查询视图.函数.存储过程中的关键字 SELECT a.name,a.[type],b.[definition] FROM sys.all_objects a,sys.sql_modules b W ...

  6. UOJ 418 【集训队作业2018】三角形——思路+线段树合并

    题目:http://uoj.ac/problem/418 看了题解才会…… 很好的想法是把整个过程看成若干 “取一点 i ,值+=w[ i ],值-=\(\sum w[j]\)”(其中 j 是 i 的 ...

  7. AcWing 252. 树 (点分治)打卡

    题目:https://www.acwing.com/problem/content/254/ 题意:求一棵树上,路径<=k的有多少条 思路:点分治,我们用两个指针算solve函数,首先对算出来的 ...

  8. 23-25 October in 614

    Practice sort 给定一系列形如 \(A<B\) 的不等关系,判断前 \(k\) 个不等关系是否即可确定 \(n\) 个元素之间的大小顺序:如果不可确定,判断前 \(k\) 个不等关系 ...

  9. 【Java架构:持续交付】一篇文章搞掂:Jenkins

    一.安装 1.使用yum本地安装 1.1.使用yum安装JDK a.检查系统是否有安装open-jdk rpm -qa |grep java rpm -qa |grep jdk rpm -qa |gr ...

  10. (转)PAL制式和NTSC制式的区别

    转:https://www.cnblogs.com/nx520zj/articles/6061777.html 常见的电视信号制式是PAL和NTSC,另外还有SECAM等. NTSC即正交平衡调幅制. ...