当前位置:网站首页>365天挑战LeetCode1000题——Day 053 求解方程 解析 模拟
365天挑战LeetCode1000题——Day 053 求解方程 解析 模拟
2022-08-10 18:29:00 【ShowM3TheCode】
640. 求解方程
代码实现(自解)
class Solution {
private:
void parse(string str, int& x, int& co) {
int pstr = 0;
int n = str.size();
bool flag = false;
string tmp = "";
int val = 0;
x = 0, co = 0;
while (pstr != n) {
if (str[pstr] == '-') {
flag = true;
pstr++;
}
else if (str[pstr] == '+') {
pstr++;
}
while (str[pstr] >= '0' && str[pstr] <= '9') {
tmp += str[pstr++];
}
if (str[pstr] == 'x') {
if (tmp == "") val = 1;
else val = stoi(tmp);
if (flag) x -= val;
else x += val;
}
else {
if (flag) co -= stoi(tmp);
else co += stoi(tmp);
pstr--;
}
pstr++;
flag = false;
tmp = "";
}
}
public:
string solveEquation(string equation) {
int equal = equation.find('=', 0);
string left = equation.substr(0, equal);
string right = equation.substr(equal + 1);
int x1, co1, x2, co2;
parse(left, x1, co1);
parse(right, x2, co2);
// cout << "x1 : " << x1 << ", co1 : " << co1 << endl;
// cout << "x2 : " << x2 << ", co2 : " << co2 << endl;
if (x1 == x2 && co1 == co2) return "Infinite solutions";
if (x1 == x2 && co1 != co2) return "No solution";
return "x=" + to_string((co2 - co1) / (x1 - x2));
}
};
边栏推荐
- FlexSim仿真软件入门笔记:基本操作、快捷键
- FPGA:生成固化文件(将代码固化到板子上面)
- RS-485多主机通信的组网方式评估
- pip3升级到22.2.2
- 多种深度模型实现手写字母MNIST的识别(CNN,RNN,DNN,逻辑回归,CRNN,LSTM/Bi-LSTM,GRU/Bi-GRU)
- 破解校园数字安全难点,联想推出智慧教育安全体系
- redis.exceptions.DataError: Invalid input of type: ‘dict‘. Convert to a byte, string or number first
- Keil5退出仿真调试卡死的解决办法
- 去除富文本标签样式
- Consul Introduction and Installation
猜你喜欢
随机推荐
MySQL 原理与优化:Update 优化
容器化 | 在 S3 实现定时备份
请问下在datastream中用flinkcdc怎么设置jdbc的参数useSSL=false呀
set和map使用讲解
【2015】【论文笔记】等离子光混合器THz辐射的光谱——
Flexsim 发生器和暂存区设定临时实体流颜色和端口
弘玑Cyclone与风变科技达成战略合作:优势互补聚焦数字化人才培养
Three schemes of SQL query across the table
shell运算详解,看这一篇就够了!
Active users of mobile banking grew rapidly in June, hitting a half-year high
[Image segmentation] Image segmentation based on cellular automata with matlab code
常量
API 网关的功能
MSE 治理中心重磅升级-流量治理、数据库治理、同 AZ 优先
类型和id对应的两个数组
MySQL数据高级查询之连接查询、联合查询、子查询[通俗易懂]
剑指 Offer II 042. 最近请求次数-队列法
开发模式对测试的影响
C#/VB.NET 将PDF转为PDF/X-1a:2001
flex&bison系列第一章:flex Hello World