当前位置:网站首页>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);
}
}
};
边栏推荐
猜你喜欢
随机推荐
JNDI and RMI, LDAP
LeetCode-1. Two Sum
华为-坐标移动
训练一个神经网络要多久,神经网络训练时间过长
shopee API 接入说明
「企业架构」企业架构师,解决方案架构师和软件架构师有何不同
招聘分析2020.6.1
一颗完整意义的LPWAN SOC无线通信芯片——ASR6601
64位 RT-Thread 移植到 Cortex-A53 系统 bug 修复笔记
shell获取前n天的日期
奥迪的极致高端属于一个大写的H?重塑时空,谁会是这个夜晚的主角?
植物肉,为何在中国没法“真香”?
fastjson chain analysis (1.2.22-47)
v-for指令:根据数据生成列表结构
还在用 Xshell?你 out 了,推荐一个更现代的终端连接工具,好用到爆!
unr #6day1 T2题解
初始网络原理
Error creating bean with name ‘sqlSessionFactory‘ defined in class path reso「建议收藏」
如何构建一个自己的代理ip池
Redis下载安装教程 (windows)