当前位置:网站首页>​Summary of Force Buckle Solution 640 - Solving Equations

​Summary of Force Buckle Solution 640 - Solving Equations

2022-08-10 13:54:00 Lost in the summer

Directory link:

Likou Programming Questions-Solution Summary_Share + Record-CSDN Blog

GitHub synchronization brushing project:

https://github.com/September26/java-algorithms

Original link:

Reel


Description:

Solve a given equation, returning x as the string "x=#value".The equation contains only '+' , '-' operations, the variable x and its corresponding coefficients.

If the equation has no solution, return "No solution" .Returns "Infinite solutions" if the equation has infinite solutions.

The title guarantees that if there is only one solution in the equation, the value of 'x' is an integer.

Example 1:

Input: equation = "x+5-3+x=6+x-2"
Output: "x=2"
Example 2:

Input: equation = "x=x"
Output: "Infinite solutions"
Example 3:

Input: equation = "2x=x"
Output: "x=0"

Hint:

3 <= equation.length <= 1000
equation There is only one '='.
equation The equation consists of integers whose absolute value is in the range [0, 100] without leading zeros and variables'x' .
​​​


Source: LeetCode
Link: https://leetcode.cn/problems/solve-the-equation
The copyright belongs to LeetCode.For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.

Solution ideas:

* Problem-solving ideas:* Decompose the string and use four variables to record, the number of x on the left (xLeftNum), the value of the constant on the left (valueLeftNum), the number of x on the right (xRightNum), the value of the constant on the right (valueRightNum),* If xLeftNum==xRightNum, valueLeftNum=valueRightNum is an infinite solution, otherwise there is no solution.* If xLeftNum!=xRightNum, x=(valueLeftNum - valueRightNum) / (xRightNum - xLeftNum)

Code:

public class Solution640 {public String solveEquation(String equation) {String[] split = equation.split("=");Pair pairLeft = getXAndValueNum(split[0]);Pair pairRight = getXAndValueNum(split[1]);int xLeftNum = pairLeft.getKey();int valueLeftNum = pairLeft.getValue();int xRightNum = pairRight.getKey();int valueRightNum = pairRight.getValue();if (xLeftNum != xRightNum) {return "x=" + (valueLeftNum - valueRightNum) / (xRightNum - xLeftNum);}if (valueLeftNum == valueRightNum) {return "Infinite solutions";}return "No solution";}private Pair getXAndValueNum(String str) {int xNum = 0;int valueNum = 0;boolean isPositive = true;StringBuilder builder = new StringBuilder();int index = 0;char[] chars = str.toCharArray();while (index++ < str.length()) {char aChar = 0;aChar = chars[index - 1];if (aChar != '-' && aChar != '+') {builder.append(aChar);if (index < str.length()) {continue;}}String currentValue = builder.toString();builder.setLength(0);int num;if (!currentValue.endsWith("x")) {if (currentValue.length() == 0) {num = 0;} else {num = Integer.parseInt(currentValue);}if (isPositive) {valueNum += num;} else {valueNum -= num;}isPositive = aChar == '+';continue;}String substring = currentValue.substring(0, currentValue.length() - 1);if (substring.length() == 0) {num = 1;} else {num = Integer.parseInt(substring);}if (isPositive) {xNum += num;} else {xNum -= num;}isPositive = aChar == '+';}return new Pair<>(xNum, valueNum);}}

原网站

版权声明
本文为[Lost in the summer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208101329282966.html