当前位置:网站首页>【LeetCode】640. 求解方程
【LeetCode】640. 求解方程
2022-08-10 11:04:00 【pass night】
题目
求解一个给定的方程,将x以字符串 "x=#value" 的形式返回。该方程仅包含 '+' , '-' 操作,变量 x 和其对应系数。
如果方程没有解,请返回 "No solution" 。如果方程有无限解,则返回 “Infinite solutions” 。
如果方程中只有一个解,要保证返回值 ‘x’ 是一个整数。
示例 1:
输入: equation = "x+5-3+x=6+x-2"
输出: "x=2"
示例 2:
输入: equation = "x=x"
输出: "Infinite solutions"
示例 3:
输入: equation = "2x=x"
输出: "x=0"
提示:
3 <= equation.length <= 1000equation只有一个'='.equation方程由整数组成,其绝对值在[0, 100]范围内,不含前导零和变量'x'。
思路
- 因为方程只包含
+-两种运算符,所以可以直接从左往右解析 - 若方程开头不是符号,则归一化添加
+,若为-则无需添加 - 将方程归一化为 a x + b = 0 ax+b=0 ax+b=0的形式,然后使用数学法求解
- 为了简化代码“Clean Code”,将右式正负颠倒后复用左式的解析函数
代码
class Solution:
def solveEquation(self, equation: str) -> str:
a, b = 0,0
def resolve(expression: str):
nonlocal a,b
i = 0
while i < len(expression):
operator = expression[i]
i += 1
start = i
while i < len(expression) and expression[i] not in "+-":
i += 1
if expression[i-1] == "x":
if operator == "+":
if i-start == 1:
a += 1
else:
a += int(expression[start:i-1])
else:
if i-start == 1:
a -= 1
else:
a -= int(expression[start:i-1])
else:
if operator == "+":
b += int(expression[start:i])
else:
b -= int(expression[start:i])
left, right = equation.split("=")
if left[0] != "-":
left = "+"+left
if right[0] != "-":
right = "+"+right
right = right.translate(str.maketrans("+-", "-+"))
resolve(left)
resolve(right)
if a == 0 and b == 0:
return "Infinite solutions"
elif a == 0 and b != 0:
return "No solution"
else:
return F"x={
-b//a}"
class Solution2:
def solveEquation(self, equation: str) -> str:
left, right = equation.split("=")
if left[0] != "-":
left = "+"+left
if right[0] != "-":
right = "+"+right
# formate ax+b = 0
a, b, i = 0, 0, 0
while i < len(left):
operator = left[i]
i += 1
start = i
while i < len(left) and left[i] not in "+-":
i += 1
if left[i-1] == "x":
if operator == "+":
if i-start == 1:
a += 1
else:
a += int(left[start:i-1])
else:
if i-start == 1:
a -= 1
else:
a -= int(left[start:i-1])
else:
if operator == "+":
b += int(left[start:i])
else:
b -= int(left[start:i])
i = 0
while i < len(right):
operator = right[i]
i += 1
start = i
while i < len(right) and right[i] not in "+-":
i += 1
if right[i-1] == "x":
if operator == "+":
if i-start == 1:
a -= 1
else:
a -= int(right[start:i-1])
else:
if i-start == 1:
a += 1
else:
a += int(right[start:i-1])
else:
if operator == "+":
b -= int(right[start:i])
else:
b += int(right[start:i])
if a == 0 and b == 0:
return "Infinite solutions"
elif a == 0 and b != 0:
return "No solution"
else:
return F"x={
-b//a}"
复杂度
- 时间复杂度: O ( n ) O(n) O(n)
- 空间复杂度: O ( 1 ) O(1) O(1)python 字符串是Immutable的,因此替换需要声明一个新的字符串,为此可以分开写,见Solution2
边栏推荐
- Where can I view the version record of WeChat applet submission review history?
- Stroke Practice - 62 Valid Sudokus
- 力扣练习——60 二叉搜索子树的最大键值和
- 今天面了个腾讯拿38K出来的大佬,让我见识到了基础的天花板
- 常量及数据类型你还记得多少?
- 软件架构简介
- 力扣练习——59 从二叉搜索树到更大和树
- 力扣练习——61 根据字符出现频率排序
- 第5章相似矩阵及二次型(4)
- Hangdian Multi-School-Loop-(uncertainty greedy + line segment tree)
猜你喜欢
随机推荐
The brave rice rice, does not fear the brush list of 】 list has a ring
基于UiAutomator2+PageObject模式开展APP自动化测试实战
【无标题】
基于UiAutomator2+PageObject模式开展APP自动化测试实战
不止跑路,拯救误操作rm -rf /*的小伙儿
Codeforces 814 C. An impassioned circulation of affection (dp)
Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了
使用JMeter进行MySQL的压力测试
【Untitled】
【电商运营】你真的了解社交媒体营销(SMM)吗?
std::move()
模块九 - 设计电商秒杀系统
即时零售业态下如何实现自动做账?
3款不同类型的自媒体免费工具,有效提高创作、运营效率
越折腾越好用的 3 款开源 APP
mysql appears: ERROR 1524 (HY000): Plugin '123' is not loaded
程序员追求技术夯实基础学习路线建议
什么是幂等性?四种接口幂等性方案详解!
推荐6个自媒体领域,轻松易上手
Introduction to Software Architecture








