当前位置:网站首页>640. 求解方程
640. 求解方程
2022-08-10 17:09:00 【anieoo】
原题链接:640. 求解方程
solution:
class Solution {
public:
pair<int, int> work(string str) {
if(str[0] != '+' && str[0] != '-') str = '+' + str;
int a = 0, b = 0;
for(int i = 0;i < str.size();i++) {
int j = i + 1;
while(j < str.size() && isdigit(str[j])) j++;
int c = 1;
if(i + 1 <= j - 1) c = stoi(str.substr(i + 1, j - i - 1));
if(str[i] == '-') c = -c;
if(j < str.size() && str[j] == 'x') {
a += c;
i = j;
} else {
b += c;
i = j - 1;
}
}
return {a, b};
}
string solveEquation(string equation) {
int k = equation.find('=');
auto left = work(equation.substr(0, k)), right = work(equation.substr(k + 1));
int a = left.first - right.first, b = right.second - left.second;
if(a == 0) {
if(b == 0) return "Infinite solutions";
return "No solution";
} else {
return "x=" + to_string(b / a);
}
}
};边栏推荐
猜你喜欢

需求骤降,成本激增,PC行业再次入冬

什么是开源中的 “胖虎效应”

注解和反射、持续

v-on补充:自定义参数传递和事件修饰符

一文带你拿下信号卷积—常见信号卷积

How to realize full backup and incremental backup of MySQL database

ahx文件转mav文件 工具分享及说明

电力系统潮流计算(牛顿-拉夫逊法、高斯-赛德尔法、快速解耦法)【6节点 9节点 14节点 26节点 30节点 57节点】(Matlab代码实现)

《安富莱嵌入式周报》第277期:业界首款Cortex-M55+Ethos-U55 NPU套件发布,20个墨水屏菊花链玩法,氙气灯镇流器设计

C语言各种符号如何使用
随机推荐
软件工程基础知识--需求分析
雷达人体存在感应器,人体感知控制应用,为客户提供真实的感知方案
ROBOTSTXT_OBEY[通俗易懂]
长markdown文档的拆分与合并
在 Istio 服务网格内连接外部 MySQL 数据库
JWT 实现登录认证 + Token 自动续期方案
电力系统潮流计算与PowerWorld仿真(牛顿拉夫逊法和高斯赛德尔法)(Matlab实现)
神经网络如何提高准确率,神经网络的求解方式
数据库注入提权总结(二)
shell中判断文件目录是否存在
win11安装deepin20.6双系统(双硬盘)
聊聊云原生数据平台
How to realize full backup and incremental backup of MySQL database
fastjson链分析(1.2.22-47)
mysql定义存储过程
「软件架构」10种常见的软件架构模式
还在用 Xshell?你 out 了,推荐一个更现代的终端连接工具,好用到爆!
未来5年的9大技术趋势
v-show指令:切换元素的显示与隐藏
The DGIOT platform displays the whole process code analysis of OPC reporting data in real time