题目如下:

解题思路:本题的思路就是解析字符串,然后是小学时候学的解方程的思想,以"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. WIN10无法识别安卓设备,提示Windows 无法验证此设备所需的驱动程序的数字签名

    在设备管理器,显示ANDROID设备是感叹号, 不管更新驱动,还是下载什么手机助手自动安装驱动,均不可解. 从属性中查看提示的是“Windows 无法验证此设备所需的驱动程序的数字签名”, 解决办法: ...

  2. 尽量用类型化的常量替代预处理器的 #DEFINE 方法

    类型化常量 (TYPED CONSTANTS) #define ANIMATION_DURATION 0.3 这是一个预处理器指令,当编译器在代码中发现有 ANIMATION_DURATION 时,就 ...

  3. 汇编指令MOV

    格式:MOV DST,SRC 例如: MOV EAX,#050aH ;将十六进制050a 传送到通用寄存器eax中 MOV DI,BX(寄存器到寄存器之间传数) MOV ES,AX(通用寄存器与段寄存 ...

  4. 测开之路二十八:Flask基础之静态资源

    Flask默认的存放静态资源的目录名为static 在工程下创建一个文件夹(与脚本同级) 如果想命名为其他名字,则在声明app的时候要初始化,如: 准备一张图片放在static下,返回的内容加上img ...

  5. ActiveReports报表数据源:运行时数据源

    ActiveReports 支持在设计时和运行时设置报表数据源,本文将重点介绍如何在运行时为区域报表和RDL/页面报表设置报表数据源. ·第一步: 在工程中创建一个名为 SectionReport_D ...

  6. 搭建邮件服务器 使用Postfix与Dovecot

    首先需要从yum中下载安装三个服务:bind-chroot    postfix     dovecot 配置文件依次: /etc/named.conf 下载安装完后要开启的服务:named     ...

  7. 零基础如何快速学习好Python网络爬虫?

    Python网络爬虫上手很快,能够尽早入门,可是想精通确实是需求些时间,需求达到爬虫工程师的级别更是需求煞费苦心了,接下来共享的学习道路是针对小白或许学习Python网络爬虫不久的同伴们. 学习网络爬 ...

  8. jquery 的几种写法和常见问题

    为了理解页面初始化事件的编写和执行方式,特此记录下页面加载事件的语句方式: //最简单的加载事件语句 $(function(){ alert("这个提示框最先弹出")//这个用的最 ...

  9. v-cloakd的应用场景和使用方法

    使用vue构建前端应用,加载应用后页面可能会出现闪烁: <div id="app" v-cloak> {{message}} </div> 例如上述场景,应 ...

  10. mysql自带压测工具--mysqlslap

    mysqlslap  压测 mysql 5.7.20 目前版本,打印不出内存.cpu使用信息 mysqlslap -h192.168.9.60 -P4406 -uroot -p --create-sc ...