Solve the Equation

本文详细解析了一种解决代数方程的算法,通过将方程拆分为左右两边,分别计算未知数系数和常数项,进而求解未知数的具体值。文章提供了清晰的Java代码实现,并附有具体示例,帮助读者理解算法的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 xis 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"

分析:https://leetcode.com/problems/solve-the-equation/discuss/150021/Clear-Java-Code-with-Detailed-Example

Example
e.g. x+5-3+x=6+x-2

  • Firstly, we split the equation by "=":
    leftPart is x+5-3+x;
    rightPart is 6+x-2;
  • Secondly, we sum up coefficient and the rest numbers separately, i.e.
    leftVals is 2x + 2, i.e., [2, 2];
    rightVals is x + 4, i.e., [1, 4];
  • Thirdly, we solve the simplified equation by moving all elements to the left of "=",
    cntX = leftVals[0] - rightVals[0];, i.e., 2 - 1 = 1,
    cntNum = leftVals[1] - rightVals[1];, i.e., 2 - 4 = -2,
    cntX * x + cntNum = 0, i.e., 1 * x + (-2) = 0,
    x = (-cntNum) / cntX, i.e., x = 2

 

Please note that
exp.split(""); split exp by 0-length string ("a+b-c" to "a", "+", "b", "-", "c")
exp.split("(?=[-+])");split exp by 0-length string only if they are follow by "-" or "+" ("a+b-c" to "a", "+b", "-c")

 1 class Solution {
 2     public String solveEquation(String equation) {
 3         String[] parts = equation.split("=");
 4         String leftPart = parts[0];
 5         String rightPart = parts[1];
 6         int[] leftVals = evaluate(leftPart);
 7         int[] rightVals = evaluate(rightPart);
 8         int cntX = leftVals[0] - rightVals[0];
 9         int cntNum = leftVals[1] - rightVals[1];
10         if (cntX == 0) {
11             if (cntNum != 0) {
12                 return "No solution";
13             }
14             return "Infinite solutions";
15         }
16         int valX = (-cntNum) / cntX;
17         StringBuilder result = new StringBuilder();
18         result.append("x=").append(valX);
19         return result.toString();
20     }
21 
22     private int[] evaluate(String exp) {
23         int[] result = new int[2];
24         String[] expElements = exp.split("(?=[-+])");
25 
26         for (String ele : expElements) {
27             if (ele.equals("+x") || ele.equals("x")) {
28                 result[0]++;
29             } else if (ele.equals("-x")) {
30                 result[0]--;
31             } else if (ele.contains("x")) {
32                 result[0] += Integer.valueOf(ele.substring(0, ele.indexOf("x")));
33             } else {
34                 result[1] += Integer.valueOf(ele);
35             }
36         }
37         return result;
38     }
39 }

 

转载于:https://www.cnblogs.com/beiyeqingteng/p/11145365.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值