【LeetCode】640. Solve the Equation 解题报告(Python)
【LeetCode】640. Solve the Equation 解题报告(Python)
标签(空格分隔): LeetCode
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.me/
题目地址:https://leetcode.com/problems/solve-the-equation/description/
题目描述:
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"
题目大意
求解一元线性方程,求得时候注意是否有解或者无穷解等条件。
解题方法
这种题本质都是用python实现一个数学操作,本身并不难,但是很烦!
这个题我的做法是先把式子分为左右式,如果式子以负号开头,给它替换成’0-‘,然后把所有的-换成+,这样的目的是可以直接按照+去split字符串得到每个数字。然后判断数字是否包含x呀,来求出左边的x的数目,左边数字的和,右边x的数目,右边数字的和。底下实现所谓的移动分式,把x都移到左边,把数字都移动到右边。最后再根据题目的要求求解或者返回特殊情况即可。
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 日 ———— 今天学科三在路上跑的飞快~
【LeetCode】640. Solve the Equation 解题报告(Python)的更多相关文章
- ACM:HDU 2199 Can you solve this equation? 解题报告 -二分、三分
Can you solve this equation? Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
随机推荐
- A Child's History of England.50
'Knave [man without honor]!' said King Richard. 'What have I done to thee [you] that thou [you] shou ...
- day02 Linux基础
day02 Linux基础 1.什么是服务器 服务器,也称伺服器,是提供计算服务的设备.由于服务器需要响应服务请求,并进行处理,因 此一般来说服务器应具备承担服务并且保障服务的能力. windows: ...
- git pull、git fetch、git merge、git rebase的区别
一.git pull与git fetch区别 1.两者的区别 两者都是更新远程仓库代码到本地. git fetch相当于是从远程获取最新版本到本地,不会自动merge. 只是将远程仓库最新 ...
- ORACLE 按逗号拆分字符串为多行
with t as (select '1,2,3,10,11,12' a from dual) select substr(a, decode(level - 1, 0, 0, instr(a, ', ...
- java网站架构设计
涉及到的技术及工具:java,springmvc,ibatis,freemarker,mysql,mongdb,memcached,ehcache,maven. 一个网站不可能说一开始就是要设计一个能 ...
- Linux基础命令---slabtop
slabtop slabtop实时显示详细的内核板条缓存信息.它显示按所列排序条件之一排序的顶级缓存的列表.它还会显示一个统计信息头,其中填充了板坯层信息. 此命令的适用范围:RedHat.RHEL. ...
- 3.3 rust HashMap
The type HashMap<K, V> stores a mapping of keys of type K to values of type V. It does this vi ...
- NoSQL之Redis学习笔记
一.NoSQL与Redis 1.什么是NoSQL? NoSQL=Not Only SQL ,泛指非关系型数据库.随着互联网的兴起,传统的关系型数据库已经暴露了很多问题,NoSQL数据库的产生就是为了解 ...
- java配置文件的使用 —— 设置一个类为单例模式
阅读本文章前建议先阅读:java通过JDBC访问sqlserver数据库 一.使用原因:通过JDBC连接数据库时有时会需要连接不同的数据库,而jar包.连接url.用户名和密码等都是写定在程序中,不便 ...
- 【Keras】神经网络的搭建
Dense层的使用方法 参考:https://blog.csdn.net/qq_34840129/article/details/86319446 keras.layers.core.Dense( u ...