当前位置:网站首页>力扣解法汇总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);
}
}边栏推荐
- 【JS高级】ES5标准规范之创建子对象以及替换this_10
- WebView的优化与常见问题解决方案
- 大佬们有遇到过这个问题吗? MySQL 2.2 和 2.3-SNAPSHOT 都这样,貌似是
- 22!Beijing Changping District notified catering service enterprises with food safety problems
- Makefile missing separator. Stop.怎么解决「建议收藏」
- R语言使用gt包和gtExtras包优雅地、漂亮地显示表格数据:gtExtras包的gt_highlight_rows函数高亮(highlight)表格中特定的数据行、配置高亮行的特定数据列数据加粗
- Ethernet channel Ethernet channel
- 进程和计划任务管理
- 作业
- Efficient and Robust 2D-to-BEV Representation Learning via Geometry-guided Kernel Transformer Paper Notes
猜你喜欢
![[target detection] small script: extract training set images and labels and update the index](/img/9d/0f88b484cee1b85df6bc1153d9b6b4.png)
[target detection] small script: extract training set images and labels and update the index

八大排序总是忘?快来这里~

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

AWS 安全基础知识

Interface Automation Testing Basics
![[JS Advanced] Creating sub-objects and replacing this_10 in ES5 standard specification](/img/3e/14a1d7c2837c896eaa0ca625eaa040.png)
[JS Advanced] Creating sub-objects and replacing this_10 in ES5 standard specification

ABAP 里文件操作涉及到中文字符集的问题和解决方案试读版

3DS MAX 批量导出文件脚本 MAXScript 带界面

mSystems | Zhongnong Wang Jie Group Reveals the Mechanisms Affecting Soil "Plastic Interstitial" Microbial Communities

记录几道整型提升的题目
随机推荐
NodeJs原理 - Stream(二)
data product manager
鸿蒙开发从hello world开始
22!Beijing Changping District notified catering service enterprises with food safety problems
A method that can make large data clustering 2000 times faster
【学习笔记】Redis的持久化
Code Casual Recording Notes_Dynamic Programming_70 Climbing Stairs
tampercfg内核模块导致机器频繁crash
R语言使用gt包和gtExtras包优雅地、漂亮地显示表格数据:gtExtras包的gt_highlight_rows函数高亮(highlight)表格中特定的数据行、配置高亮行的特定数据列数据加粗
Calculate the number of combinations recursively
商汤自研机械臂,首款产品是AI下棋机器人:还请郭晶晶作代言
Shell:数组
高数_证明_曲率公式
3DS MAX 批量导出文件脚本 MAXScript 带界面
【POI 2008, BLO】割点
YTU 2295: KMP pattern match one (string)
Ethernet channel Ethernet channel
shell:常用小工具(sort、uniq、tr、cut)
Vivado crashes or the message is not displayed
AWS 安全基础知识