当前位置:网站首页>【640. Solving Equations】
【640. Solving Equations】
2022-08-10 22:36:00 【[email protected]】
来源:力扣(LeetCode)
描述:
求解一个给定的方程,将 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’
方法:解析

代码:
class Solution {
public:
string solveEquation(string equation) {
int factor = 0, val = 0;
int index = 0, n = equation.size(), sign1 = 1; // The default coefficient on the left side of the equation is positive
while (index < n) {
if (equation[index] == '=') {
sign1 = -1; // The default coefficient on the right-hand side of the equation is negative
index++;
continue;
}
int sign2 = sign1, number = 0;
bool valid = false; // 记录 number 是否有效
if (equation[index] == '-' || equation[index] == '+') {
// 去掉前面的符号
sign2 = (equation[index] == '-') ? -sign1 : sign1;
index++;
}
while (index < n && isdigit(equation[index])) {
number = number * 10 + (equation[index] - '0');
index++;
valid = true;
}
if (index < n && equation[index] == 'x') {
// 变量
factor += valid ? sign2 * number : sign2;
index++;
} else {
// 数值
val += sign2 * number;
}
}
if (factor == 0) {
return val == 0 ? "Infinite solutions" : "No solution";
}
return string("x=") + to_string(-val / factor);
}
};
执行用时:0 ms, 在所有 C++ 提交中击败了100.00%的用户
内存消耗:5.9 MB, 在所有 C++ 提交中击败了83.55%的用户
复杂度分析
时间复杂度: O(n),其中 n 是字符串 equation 的长度.
空间复杂度: O(1).
author:LeetCode-Solution
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208102149022968.html
边栏推荐
- 商家招募电商主播要考虑哪些内容
- How to secure users in LDAP directory service?
- camera预览流程 --- 从HAL到OEM
- 新一代网络安全防护体系的五个关键特征
- 电力系统潮流计算(牛顿-拉夫逊法、高斯-赛德尔法、快速解耦法)(Matlab代码实现)
- 阿里云贾朝辉:云XR平台支持彼真科技呈现国风科幻虚拟演唱会
- STL-deque
- Merge k sorted linked lists
- 什么是Jmeter?Jmeter使用的原理步骤是什么?
- Using SylixOS virtual serial port, serial port free implementation system
猜你喜欢

HighTec快捷键(Keys)设置位置

2022年8月10日:使用 ASP.NET Core 为初学者构建 Web 应用程序--使用 ASP.NET Core 创建 Web UI(没看懂需要再看一遍)

Alibaba and Ant Group launched OceanBase 4.0, a distributed database, with single-machine deployment performance exceeding MySQL

c语言之 练习题1 大贤者福尔:魔法数,神奇的等式

unusual understanding

爬虫request.get()出现错误

3598. 二叉树遍历(华中科技大学考研机试题)
![[SQL brush questions] Day3----Special exercises for common functions that SQL must know](/img/b8/05589138441ada5d453297de7d181b.png)
[SQL brush questions] Day3----Special exercises for common functions that SQL must know

《DevOps围炉夜话》- Pilot - CNCF开源DevOps项目DevStream简介 - feat. PMC成员胡涛

【640. 求解方程】
随机推荐
Redis Performance Impact - Asynchronous Mechanisms and Response Latency
黑猫带你学Makefile第13篇:Makefile编译问题合集
解码2022中国网安强星丨正向建、反向查,华为构建数字化时代的网络安全防线
LeetCode每日一题(1573. Number of Ways to Split a String)
这些不可不知的JVM知识,我都用思维导图整理好了
What is Jmeter? What are the principle steps used by Jmeter?
美味的佳肴
RK3399平台开发系列讲解(内核驱动外设篇)6.35、IAM20680陀螺仪介绍
ThreadLocal comprehensive analysis (1)
unusual understanding
Service - DHCP principle and configuration
Common interview questions for APP UI automation testing, maybe useful~
Using SylixOS virtual serial port, serial port free implementation system
FPGA - Memory Resources of 7 Series FPGA Internal Structure -03- Built-in Error Correction Function
String类的常用方法
Shell 编程--Sed
美味的石井饭
Interpretation of the paper (g-U-Nets) "Graph U-Nets"
Why general company will say "go back messages such as" after the end of the interview, rather than just tell the interviewer the result?
Nodes in the linked list are flipped in groups of k