当前位置:网站首页>【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
边栏推荐
- Conditional Statements of Shell Programming (2)
- 财务年报怎样翻译,为什么要选择专业翻译公司?
- shell(文本打印工具awk)
- 阿里云贾朝辉:云XR平台支持彼真科技呈现国风科幻虚拟演唱会
- xshell (sed 命令)
- HighTec shortcut keys (Keys) setting location
- FPGA - Memory Resources of 7 Series FPGA Internal Structure -03- Built-in Error Correction Function
- labelme - block drag and drop events
- H3C S5130 IRF做堆叠
- 美味的石井饭
猜你喜欢
BM13 determines whether a linked list is a palindrome
Conditional Statements of Shell Programming (2)
深度学习之 12 循环神经网络RNN2
【开源教程5】疯壳·开源编队无人机-飞控固件烧写
H3C S5130 IRF做堆叠
What is Jmeter? What are the principle steps used by Jmeter?
一篇文章教你Pytest快速入门和基础讲解,一定要看
Play RT-THREAD of doxygen
An article to teach you a quick start and basic explanation of Pytest, be sure to read
geemap的详细安装步骤及环境配置
随机推荐
port forwarding
黑猫带你学Makefile第12篇:常见Makefile问题汇总
服务——DHCP原理与配置
pytorch手撕CNN
Nodes in the linked list are flipped in groups of k
Intelligent scheme design - intelligent rope skipping scheme
unusual understanding
3D model reconstruction of UAV images based on motion structure restoration method based on Pix4Dmapper
About DataFrame: Processing Time
An article to teach you a quick start and basic explanation of Pytest, be sure to read
如何成为一名正义黑客?你应该学习什么?
CIKM2022 | 基于双向Transformers对比学习的序列推荐
《DevOps围炉夜话》- Pilot - CNCF开源DevOps项目DevStream简介 - feat. PMC成员胡涛
APP UI自动化测试常见面试题,或许有用呢~
RTL8721DM 双频WIFI + 蓝牙5.0 物联网(IoT)应用
The perfect alternative to domestic Gravatar avatars Cravatar
LeetCode-402 - Remove K digits
BM7 链表中环的入口结点
GMT,UTC,CST,DST,RTC,NTP,SNTP,NITZ: 嵌入式的时间
Shell编程之条件语句(二)