当前位置:网站首页>【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 <= 1000
equation
只有一个'='
.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
边栏推荐
- 力扣练习——56 寻找右区间
- 负载均衡原理分析与源码解读
- 快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
- [Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists
- The brave rice rice, does not fear the brush list of 】 list has a ring
- POJ 2891 Strange Way to Express Integers (Extended Euclidean)
- 4 of huawei offer levels, incredibly side is easing the bit in the interview ali?
- Introduction to Software Architecture
- AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
- 基于UiAutomator2+PageObject模式开展APP自动化测试实战
猜你喜欢
How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize
Nocalhost - Making development more efficient in the cloud-native era
推荐6个自媒体领域,轻松易上手
微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。
WeChat applet, global variables change in one place and the state in other places also changes.
ENVI 5.3软件安装包和安装教程
基于UiAutomator2+PageObject模式开展APP自动化测试实战
单目操作符(含原码反码补码转换)
一文带你搞懂中断按键驱动程序之poll机制
Short video software development - how to break the platform homogenization
随机推荐
Spss-多元回归案例实操
Introduction to Software Architecture
一文带你搞懂中断按键驱动程序之poll机制
[Go WebSocket] 多房间的聊天室(一)思考篇
POJ 2891 Strange Way to Express Integers (Extended Euclidean)
Nocalhost - 让云原生时代的开发更高效
使用哈工大LTP测试分词并且增加自定义字典
POJ 3101 Astronomy (Mathematics)
Break through the dimensional barriers and let the dolls around you move on the screen!
不止跑路,拯救误操作rm -rf /*的小伙儿
blocking non-blocking poll mechanism asynchronous
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
4 of huawei offer levels, incredibly side is easing the bit in the interview ali?
AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
Licking Exercise - 63 Find all anagrams in a string
AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)
HDU 1520 Anniversary party (tree dp)
AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
【勇敢饭饭,不怕刷题之链表】有序链表的合并
Programmers pursue technology to consolidate basic learning route suggestions