当前位置:网站首页>【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
边栏推荐
- 一篇文章教你Pytest快速入门和基础讲解,一定要看
- 留言有奖|OpenBMB x 清华大学NLP:大模型公开课更新完结!
- Shell编程之条件语句(二)
- Translating scientific and technological papers, how to translate from Russian to Chinese
- ThreadLocal全面解析(一)
- 测试4年感觉和1、2年时没什么不同?这和应届生有什么区别?
- 自组织是管理者和成员的双向奔赴
- 黑猫带你学Makefile第11篇:当头文件a.h改变时,如何将所有依赖头文件a.h的.c文件都重新编译
- What is Jmeter? What are the principle steps used by Jmeter?
- 特别的三杯鸡
猜你喜欢
unusual understanding
Service - DNS forward and reverse domain name resolution service
阿里云贾朝辉:云XR平台支持彼真科技呈现国风科幻虚拟演唱会
c语言之 练习题1 大贤者福尔:魔法数,神奇的等式
企业云存储日常运行维护实践经验分享
What are the concepts, purposes, processes, and testing methods of interface testing?
Exploration and practice of the "zero trust" protection and data security governance system of the ransomware virus of Meichuang Technology
QT笔记——用VS + qt 生成dll 和 调用生成的dll
Black cats take you learn Makefile article 13: a Makefile collection compile problem
测试4年感觉和1、2年时没什么不同?这和应届生有什么区别?
随机推荐
shell脚本
Service - DNS forward and reverse domain name resolution service
阿里云张新涛:支持沉浸式体验应用快速落地,阿里云云XR平台发布
About DataFrame: Processing Time
服务——DHCP原理与配置
Addition of linked lists (2)
JVM classic fifty questions, now the interview is stable
Extended Chinese Remainder Theorem
CIKM2022 | 基于双向Transformers对比学习的序列推荐
shell (text printing tool awk)
Regular expression of shell programming and text processor
RADIUS Authentication Server Deployment Costs That Administrators Must Know
元宇宙社交应用,靠什么吸引用户「为爱发电」?
MySQL高级指令
基于交流潮流的电力系统多元件N-k故障模型研究(Matlab代码实现)【电力系统故障】
HighTec shortcut keys (Keys) setting location
使用 Cloudreve 搭建私有云盘
Use Cloudreve to build a private cloud disk
接口测试的概念、目的、流程、测试方法有哪些?
2022年8月10日:使用 ASP.NET Core 为初学者构建 Web 应用程序--使用 ASP.NET Core 创建 Web UI(没看懂需要再看一遍)