当前位置:网站首页>LeetCode·每日一题·640.求解方程·模拟构造
LeetCode·每日一题·640.求解方程·模拟构造
2022-08-10 12:12:00 【小迅想变强】
链接:https://leetcode.cn/problems/solve-the-equation/solution/mo-ni-by-xun-ge-v-5m9w/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目
示例
思路
解题思路
根据题意直接模拟计算过程即可
具体实现
为了方便定义两个变量,一个factor保存x的系数,一个val保存常量的值,然后遍历枚举字符串所有元素,将对应元素转换,最后判断factor和val的值
代码
#define MAX_EXPRESSION_LEN 32
char * solveEquation(char * equation) {
int factor = 0, val = 0;
int index = 0, n = strlen(equation), sign1 = 1; // 等式左边默认系数为正
while (index < n) {
//判断是等号左边还是右边
if (equation[index] == '=') {
sign1 = -1; // 等式右边默认系数为负
index++;
continue;
}
int sign2 = sign1, number = 0;
bool valid = false; // 记录 number 是否有效,为了后面判断x的系数方便
if (equation[index] == '-' || equation[index] == '+') { // 去掉前面的符号
sign2 = (equation[index] == '-') ? -sign1 : sign1;
index++;
}
//将数值转换一下,但是还不能确定是不是x的系数还是常量
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";
}
char *ans = (char *)malloc(sizeof(char) * MAX_EXPRESSION_LEN);
sprintf(ans, "x=%d", - val / factor);
return ans;
}
作者:xun-ge-v
链接:https://leetcode.cn/problems/solve-the-equation/solution/mo-ni-by-xun-ge-v-5m9w/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。边栏推荐
- Overview of Loudi Petrochemical Experiment Design and Construction Planning
- Custom filters and interceptors implement ThreadLocal thread closure
- mSystems | 中农汪杰组揭示影响土壤“塑料际”微生物群落的机制
- 实践为主,理论为辅!腾讯大佬MySQL高阶宝典震撼来袭!
- Detailed explanation of es6-promise object
- Polygon zkEVM工具——PIL和CIRCOM
- LT8911EXB MIPI CSI/DSI to EDP signal conversion
- 九宫格抽奖动效
- 浙大、阿里提出DictBERT,字典描述知识增强的预训练语言模型
- Behind IDC's No. 1 position, what kind of "video cloud" is Alibaba Cloud building?
猜你喜欢

专有云ABC Stack,真正的实力派!

2022年8月中国数据库排行榜:openGauss重夺榜眼,PolarDB反超人大金仓

Guidelines for Sending Overseas Mail (2)

中科院深圳先进技术院合成所赵国屏院士组2022年招聘启事

没有接班人,格力只剩“明珠精选”

【黑马早报】雷军称低谷期曾想转行开酒吧;拜登正式签署芯片法案;软银二季度巨亏230亿美元;北京市消协约谈每日优鲜...

Solution for "Certificate not valid for requested usage" after Digicert EV certificate signing

10 款更先进的开源命令行工具

“68道 Redis+168道 MySQL”精品面试题(带解析)

Chapter9 : De Novo Molecular Design with Chemical Language Models
随机推荐
Polygon zkEVM工具——PIL和CIRCOM
Inventory of Loudi Agricultural Products Inspection Laboratory Construction Guidelines
这三个 Go 水平自测题,你手写不出来还是先老实上班吧,过来看看
Loudi Sewage Treatment Plant Laboratory Construction Management
Codeforces Round #276 (Div. 1) D. Kindergarten
瑞幸「翻身」?恐言之尚早
教育Codeforces轮41(额定Div。2)大肠Tufurama
MYSQL误删数据恢复
娄底妆品实验室建设规划构思
燃炸!字节跳动成功上岸,只因刷爆LeetCode算法面试题
Guidelines for Sending Overseas Mail (2)
Reversing words in a string in LeetCode
11 + chrome advanced debugging skills, learn to direct efficiency increases by 666%
48MySQL数据库基础
面试美团被问到了Redis,搞懂这几个问题,让你轻松吊打面试官
神经网络学习-正则化
专有云ABC Stack,真正的实力派!
mSystems | 中农汪杰组揭示影响土壤“塑料际”微生物群落的机制
IP地址分类以及网络地址的计算(子网划分、超网划分)[通俗易懂]
关于flask中static_folder 和 static_url_path参数理解