当前位置:网站首页>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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
边栏推荐
- Does face attendance choose face comparison 1:1 or face search 1:N?
- 48MySQL数据库基础
- Digicert EV证书签名后出现“证书对于请求用法无效”的解决方案
- Deploy the project halfway through the follow-up
- 47Haproxy集群
- What are the five common data types of Redis?What is the corresponding data storage space?Take you to learn from scratch
- Detailed explanation of es6-promise object
- 漏洞管理计划的未来趋势
- 「网络架构」网络代理第一部分: 代理概述
- 阿里架构师整理一份企业级SSM架构实战文档,让你熟悉底层原理
猜你喜欢
LeetCode中等题之颠倒字符串中的单词
LeetCode中等题之搜索二维矩阵
Chapter 5 virtual memory
ASP.NET Core依赖注入系统学习教程:ServiceDescriptor(服务注册描述类型)
Jenkins修改端口号, jenkins容器修改默认端口号
Merge similar items in LeetCode simple questions
How to cultivate the design thinking of ui designers?
LeetCode中等题之比较版本号
十八、一起学习Lua 调试(Debug)
Is there a problem with the CURRENT_TIMESTAMP(6) function?
随机推荐
数字藏品,“赌”字当头
The god-level Alibaba "high concurrency" tutorial - basic + actual combat + source code + interview + architecture is all-inclusive
10 款更先进的开源命令行工具
A detailed explanation of implementation api embed
来看Prada大秀吗?在元宇宙里那种!
毕业总结
22年BATJ大厂必问面试题(复盘):JVM+微服务+多线程+锁+高并发
国外媒体宣发怎样做才可以把握重点
Chapter9 : De Novo Molecular Design with Chemical Language Models
关于flask中static_folder 和 static_url_path参数理解
海外媒体宣发.国内媒体发稿要注意哪些问题?
娄底妆品实验室建设规划构思
培训机构学习费用是多少呢?
Custom filters and interceptors implement ThreadLocal thread closure
odps sql 不支持 unsupported feature CREATE TEMPORARY
AtCoder Beginner Contest 077 D - Small Multiple
Merge similar items in LeetCode simple questions
Loudi Cosmetics Laboratory Construction Planning Concept
camshift achieves target tracking
爱可可AI前沿推介(8.10)