【leetcode】640. Solve the Equation
题目如下:

解题思路:本题的思路就是解析字符串,然后是小学时候学的解方程的思想,以"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的更多相关文章
- 【LeetCode】640. Solve the Equation 解题报告(Python)
[LeetCode]640. Solve the Equation 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】692. Top K Frequent Words 解题报告(Python)
[LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【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 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- Xcode7.1环境下上架iOS App到AppStore 流程①
前言部分 之前App要上架遇到些问题到网上搜上架教程发现都是一些老的版本的教程 ,目前iTunesConnect 都已经迭代好几个版本了和之前的 界面风格还是有很大的差别的,后面自己折腾了好久才终于把 ...
- python3用pygame实现播放音乐文件
import pygameimport time #导入音乐文件file = r'C:\1.wav'pygame.mixer.init()track = pygame.mixer.music.load ...
- Halo(十三)
Spring Boot Actuator 请求跟踪 Spring Boot Actuator 的关键特性是在应用程序里提供众多 Web 接口, 通过它们了解应用程序运行时的内部状况,且能监控和度量 S ...
- [转]解决win10下localhost打不开的问题
博主刚开始玩Tornado,结果localhost都打不开,各种找寻解决方案,结论都是IIS服务器问题.然而win10下的解决方法居然没人写过...那就我来配图详解下. 打开控制面板--添加或删除程序 ...
- php ord()函数 语法
php ord()函数 语法 作用:返回字符串的首个字符的 ASCII 值.直线电机生产厂家 语法:ord(string) 参数: 参数 描述 string 必须,要从中获得ASCII值的字符串 说明 ...
- 安装windows10和fedora23双系统的一些注意事项
在安装双系统windows10和fedora的过程中遇到了很多的问题,博主也是在慢慢的摸索中最后莫名其妙的成功的安装双系统. 当然,幸亏博主机智的记住了中间的一些细节,所以大致上的有一些注意事项希望能 ...
- linux0.11源码内核——系统调用,int80的实现细节
linux0.11添加系统调用的步骤 假设添加一个系统调用foo() 1.修改include/linux/sys.h 添加声明 extern int foo(); 同时在sys_call_table数 ...
- [CSP-S模拟测试]:Lighthouse(哈密顿回路+容斥)
题目背景 $Billions\ of\ lighthouses...stuck\ at\ the\ far\ end\ of\ the\ sky.$ 题目描述 平面有$n$个灯塔,初始时两两之间可以相 ...
- js中打地鼠游戏
<!DOCTYPE html><html lang=""><head> <mata charset = "utf-8" ...
- Linux操作系统(五)_部署Zentao
一.部署Zentao 1.检查服务器信息 uname -a 2.下载相应的部署包(一键安装包) http://dl.cnezsoft.com/zentao/9.8.1/ZenTaoPMS.9.8.1. ...