当前位置:网站首页>力扣解法汇总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);
}
}边栏推荐
猜你喜欢

重要通知 | “移动云杯”算力网络应用创新大赛初赛延期!!

进程和计划任务管理

机器学习实战(2)——端到端的机器学习项目

借数据智能,亚马逊云科技助力企业打造品牌内生增长力

记录几道整型提升的题目

高数_证明_弧微分公式

Efficient and Robust 2D-to-BEV Representation Learning via Geometry-guided Kernel Transformer Paper Notes

SenseTime self-developed robotic arm, the first product is an AI chess-playing robot: Guo Jingjing is also invited as an endorsement

友邦人寿可观测体系设计与落地

神了!阿里数据库专家纯手写了这份604页的Oracle+MySQL攻坚指南
随机推荐
leetcode 739. Daily Temperatures 每日温度(中等)
Fragment's show and hide
A method that can make large data clustering 2000 times faster
Reversing words in a string in LeetCode
3DS MAX 批量导出文件脚本 MAXScript 带界面
Vivado crashes or the message is not displayed
Redis 定长队列的探索和实践
C# error The 'xmins' attribute is not supported in this context
交换机的基础知识
简单的写一个防抖跟节流
一汽奥迪:持续34年聚焦品质与体验 立足市场需求推进产品迭代
Blast!ByteDance successfully landed, only because the interview questions of LeetCode algorithm were exhausted
Data product manager thing 2
用低代码驱动IT现代化
友邦人寿可观测体系设计与落地
Error: Rule can only have one resource source (provided resource and test + include + exclude)
SecureCRTPortable – 破解
C# InitializeComponent() does not exist in the current context
PHP judges whether the file has content, and if there is no content, copy another file to write
【219】慕课三千多的那个go工程师的培训课笔记 02 go语言的编程思想