当前位置:网站首页>【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
边栏推荐
- 今日睡眠质量记录75分
- camera preview process --- from HAL to OEM
- xshell (sed 命令)
- How to translate financial annual report, why choose a professional translation company?
- TCP连接过程中如果拔掉网线会发生什么?
- Merge k sorted linked lists
- 谁是边缘计算服务的采购者?是这六个关键角色
- Addition of linked lists (2)
- 黑猫带你学Makefile第11篇:当头文件a.h改变时,如何将所有依赖头文件a.h的.c文件都重新编译
- Research on multi-element N-k fault model of power system based on AC power flow (implemented by Matlab code) [Power System Fault]
猜你喜欢
【Maui正式版】创建可跨平台的Maui程序,以及有关依赖注入、MVVM双向绑定的实现和演示
BM7 list entry in central
shell(文本打印工具awk)
基于交流潮流的电力系统多元件N-k故障模型研究(Matlab代码实现)【电力系统故障】
谁是边缘计算服务的采购者?是这六个关键角色
Alibaba and Ant Group launched OceanBase 4.0, a distributed database, with single-machine deployment performance exceeding MySQL
阿里云架构师金云龙:基于云XR平台的视觉计算应用部署
FPGA - Memory Resources of 7 Series FPGA Internal Structure -03- Built-in Error Correction Function
Nodes in the linked list are flipped in groups of k
什么是Jmeter?Jmeter使用的原理步骤是什么?
随机推荐
带着昇腾去旅行:一日看尽金陵城里的AI胜景
HighTec shortcut keys (Keys) setting location
配电网络扩展规划:考虑使用概率性能源生产和消费概况的决策(Matlab代码实现)
String类的常用方法
geemap的详细安装步骤及环境配置
美味的石井饭
STL-deque
FPGA - 7系列 FPGA内部结构之Memory Resources -03- 内置纠错功能
威纶通触摸屏如何在报警的同时,显示出异常数据的当前值?
Shell编程规范与变量
《DevOps围炉夜话》- Pilot - CNCF开源DevOps项目DevStream简介 - feat. PMC成员胡涛
GMT,UTC,CST,DST,RTC,NTP,SNTP,NITZ: 嵌入式的时间
Use Cloudreve to build a private cloud disk
黑猫带你学Makefile第12篇:常见Makefile问题汇总
shell(文本打印工具awk)
H3C S5130 IRF做堆叠
What are the concepts, purposes, processes, and testing methods of interface testing?
[Maui official version] Create a cross-platform Maui program, as well as the implementation and demonstration of dependency injection and MVVM two-way binding
Translating scientific and technological papers, how to translate from Russian to Chinese
从斐波那契 - 谈及动态规划 - 优化