当前位置:网站首页>力扣解法汇总640-求解方程
力扣解法汇总640-求解方程
2022-08-10 13:30:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:
描述:
求解一个给定的方程,将x以字符串 "x=#value" 的形式返回。该方程仅包含 '+' , '-' 操作,变量 x 和其对应系数。
如果方程没有解,请返回 "No solution" 。如果方程有无限解,则返回 “Infinite solutions” 。
题目保证,如果方程中只有一个解,则 'x' 的值是一个整数。
示例 1:
输入: equation = "x+5-3+x=6+x-2"
输出: "x=2"
示例 2:
输入: equation = "x=x"
输出: "Infinite solutions"
示例 3:
输入: equation = "2x=x"
输出: "x=0"
提示:
3 <= equation.length <= 1000
equation 只有一个 '='.
equation 方程由整数组成,其绝对值在 [0, 100] 范围内,不含前导零和变量 'x' 。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/solve-the-equation
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 分解字符串,用四个变量来记录,左边x的数量(xLeftNum),左边常数的值(valueLeftNum),右边x的数量(xRightNum),右边常数的值(valueRightNum), * 如果xLeftNum==xRightNum时,valueLeftNum=valueRightNum就是无限解,否则就是没有解。 * 如果xLeftNum!=xRightNum时,x=(valueLeftNum - valueRightNum) / (xRightNum - xLeftNum)
代码:
public class Solution640 {
public String solveEquation(String equation) {
String[] split = equation.split("=");
Pair<Integer, Integer> pairLeft = getXAndValueNum(split[0]);
Pair<Integer, Integer> 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<Integer, Integer> 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);
}
}
边栏推荐
猜你喜欢
C#实现访问OPC UA服务器
BEVDet4D: Exploit Temporal Cues in Multi-camera 3D Object Detection Paper Notes
Short read or OOM loading DB. Unrecoverable error, aborting now
3DS MAX batch export file script MAXScript with interface
“Oracle 封禁了我的账户”
ABAP file operations involved in the Chinese character set of problems and solutions for trying to read
MySQL面试题整理
Code Casual Recording Notes_Dynamic Programming_70 Climbing Stairs
第三方软件测评有什么作用?权威软件检测机构推荐
鸿蒙开发从hello world开始
随机推荐
广东10个项目入选工信部2021年物联网示范项目名单
Existing in the rain of PFAS chemical poses a threat to the safety of drinking water
友邦人寿可观测体系设计与落地
机器学习实战(2)——端到端的机器学习项目
递归递推之计算组合数
Pointer (preliminary solution of C language)
网络安全——XSS之被我们忽视的Cookie
DNS欺骗-教程详解
Vivado crashes or the message is not displayed
I would like to ask the big guys, how to solve this error when cdc oracle initializes a 3 million table task running
一种能让大型数据聚类快2000倍的方法,真不戳
recursive recursive function
vivado闪退或者message无显示
【MinIO】工具类使用
Efficient and Robust 2D-to-BEV Representation Learning via Geometry-guided Kernel Transformer 论文笔记
Open Office XML 格式里如何描述多段具有不同字体设置的段落
ES5和SE6来实现一个Promise效果
leetcode 739. Daily Temperatures 每日温度(中等)
Basic knowledge of switches
C# 当前上下文中不存在InitializeComponent()