【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)的更多相关文章

  1. 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 ...

  2. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  3. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  4. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  5. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  6. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  7. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  8. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  9. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

随机推荐

  1. 从 ClickHouse 到 ByteHouse:实时数据分析场景下的优化实践

    本文来自火山引擎公众号,原文发布于2021-09-06. 近日,字节跳动旗下的企业级技术服务平台火山引擎正式对外发布「ByteHouse」,作为 ClickHouse 企业版,解决开源技术上手难 &a ...

  2. Spring Security 基于URL的权限判断

    1.  FilterSecurityInterceptor 源码阅读 org.springframework.security.web.access.intercept.FilterSecurityI ...

  3. C语言中不用 + 和 - 求两个数之和

    (二)解题 题目大意:不用+或者-实现两个整数的加法 解题思路:不用+或者-,就自然想到位运算,无非就是与或非来实现二进制的加法 首先,我们来看一位二进制的加法和异或运算 A B A&B A^ ...

  4. SQLyog连接mysql8报2058错误

    连接会话时,报如下错误. 通过网上查解决办法,报这个错误的原因是mysql密码加密方法变了 解决办法: 1.先使用mysql -uroot -p输入密码进去mysql 2.ALTER USER 'ro ...

  5. Hive(五)【DQL数据查询】

    目录 一. 基本查询 1.1 算数运算符 1.2 常用聚合函数 1.3 limit 1.4 where 1.5 比较运算符(between|in|is null) 1.6 LIKE和RLIKE 1.7 ...

  6. 零基础学习java------day6----数组

    0. 内容概览 补充:main方法中的数组 1. 数组的概述 概念: 用来存储一组相同数据类型的集合(或者叫容器) 注意事项: 1. 数组中的元素类型必须一致 2. 数组本身是引用数据类型,但是里面的 ...

  7. RB-Tree深度探索

    关联式容器就是通过key值来寻找value,这个和数据库很相像,为了提升查找效率,因此关联式容器底层大多数用红黑树或哈希表来实现. 红黑树是高度平衡的二叉树,它也被称为平衡二元搜索树. 如上所示,正常 ...

  8. springboot优雅实现异常处理

    前言 在平时的 API 开发过程中,总会遇到一些错误异常没有捕捉到的情况.那有的小伙伴可能会想,这还不简单么,我在 API 最外层加一个 try...catch 不就完事了. 哈哈哈,没错.这种方法简 ...

  9. vue 中使用import导入 script 在线链接

    一般我们在vue中导入另外一个文件或者文件中的方法,我们都是使用import来实现他的,那么问题来了,现在我们要导入的不是另外的一个文件,而是在线链接,这该怎么办?我们也使用了 import * as ...

  10. 应用层协议——DHCP

    常见协议分层 网洛层协议:包括:IP协议.ICMP协议.ARP协议.RARP协议. 传输层协议:TCP协议.UDP协议. 应用层协议:FTP.Telnet.SMTP.HTTP.RIP.NFS.DNS ...