当前位置:网站首页>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:
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);}}
边栏推荐
- Vivado crashes or the message is not displayed
- 学习日记9
- 【MinIO】工具类使用
- CodeForces - 834C
- Error: Rule can only have one resource source (provided resource and test + include + exclude)
- Existing in the rain of PFAS chemical poses a threat to the safety of drinking water
- 友邦人寿可观测体系设计与落地
- Fragment's show and hide
- Fragment的show和hide
- 进程和计划任务管理
猜你喜欢
商汤自研机械臂,首款产品是AI下棋机器人:还请郭晶晶作代言
交换机的基础知识
ABAP file operations involved in the Chinese character set of problems and solutions for trying to read
【量化交易行情不够快?】一文搞定通过Win10 wsl2 +Ubuntu+redis+pickle实现股票行情极速读写
日志@Slf4j介绍使用及配置等级
Send a post request at the front desk can't get the data
Nanodlp v2.2/v3.0 light curing circuit board, connection method of mechanical switch/photoelectric switch/proximity switch and system state level setting
bgp dual plane experiment routing strategy to control traffic
普林斯顿微积分读本05第四章--求解多项式的极限问题
这一次,话筒给你:向自由软件之父斯托曼 提问啦!
随机推荐
雨水中存在的PFAS化学物质对饮用水安全构成了威胁
一种能让大型数据聚类快2000倍的方法,真不戳
The recursive recursive Fighting_ silver study ah but level 4
DNS欺骗-教程详解
[Study Notes] Persistence of Redis
【ECCV 2022|Millions of Prizes】PSG Competition: Pursuing the "Most Comprehensive" Scene Understanding
ES5和SE6来实现一个Promise效果
Vivado crashes or the message is not displayed
神了!阿里数据库专家纯手写了这份604页的Oracle+MySQL攻坚指南
系统的安全和应用(不会点安全的东西你怎么睡得着?)
bgp dual plane experiment routing strategy to control traffic
图式图例规范尺寸
The basic components of Loudi plant cell laboratory construction
Stream通过findFirst()查找满足条件的一条数据
学习日记9
3DS MAX 批量导出文件脚本 MAXScript 带界面
Cloud Migration Practice of Redis
C# InitializeComponent() does not exist in the current context
Fragment-hide和show
2022-08-09: What does the following go code output?A: No, it will panic; B: Yes, it can run correctly; C: Not sure, see the voting result.package main import (“fmt“ “syn