c语言sscanf函数的用法是什么
286
2022-08-26
[leetcode] 640. Solve the Equation
Description
Solve a given equation and return the value of x in the form of string “x=#value”. The equation contains only ‘+’, ‘-’ operation, the variable x and its coefficient.
If there is no solution for the equation, return “No solution”.
If there are infinite solutions for the equation, return “Infinite solutions”.
If there is exactly one solution for the equation, we ensure that the value of x is an integer.
Example 1: Input:
"x+5-3+x=6+x-2"
Output:
"x=2"
Example 2: Input:
"x=x"
Output:
"Infinite solutions"
Example 3: Input:
"2x=x"
Output:
"x=0"
Example 4: Input:
"2x+3x-6x=x+2"
Output:
"x=-1"
Example 5: Input:
"x=x+2"
Output:
"No solution"
分析
题目的意思是:给定一个字符串等式,求解x。
等式左边sign=1,等式右边sign=-1;b用来累加数,a用来累加符号x上的数。然后-b/a就是结果了哈,注意无解和无穷解的情况。无解a=0&&b!=0,无穷解a=0&&b=0。
代码
class Solution {public: string solveEquation(string equation) { int n=equation.size(); int j=0; int sign=1; int b=0,a=0; for(int i=0;i 参考文献 [LeetCode] Solve the Equation 解方程
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~