Solve the Equation
Solve a given equation and return the value of x
in the form of string "x=#value". The equation contains only '+', '-' operation, the variable x
and its coefficient.
If there is no solution for the equation, return "No solution".
If there are infinite solutions for the equation, return "Infinite solutions".
If there is exactly one solution for the equation, we ensure that the value of x
is an integer.
Example 1:
Input: "x+5-3+x=6+x-2"
Output: "x=2"
Example 2:
Input: "x=x"
Output: "Infinite solutions"
Example 3:
Input: "2x=x"
Output: "x=0"
Example 4:
Input: "2x+3x-6x=x+2"
Output: "x=-1"
Example 5:
Input: "x=x+2"
Output: "No solution" 分析:https://leetcode.com/problems/solve-the-equation/discuss/150021/Clear-Java-Code-with-Detailed-Example
Example
e.g. x+5-3+x=6+x-2
- Firstly, we split the equation by "=":
leftPart
is x+5-3+x;rightPart
is 6+x-2; - Secondly, we sum up coefficient and the rest numbers separately, i.e.
leftVals
is 2x + 2, i.e., [2, 2];rightVals
is x + 4, i.e., [1, 4]; - Thirdly, we solve the simplified equation by moving all elements to the left of "=",
cntX = leftVals[0] - rightVals[0];
, i.e., 2 - 1 = 1,cntNum = leftVals[1] - rightVals[1];
, i.e., 2 - 4 = -2,cntX * x + cntNum = 0
, i.e., 1 * x + (-2) = 0,x = (-cntNum) / cntX
, i.e., x = 2
Please note thatexp.split("");
split exp by 0-length string ("a+b-c" to "a", "+", "b", "-", "c")exp.split("(?=[-+])");
split exp by 0-length string only if they are follow by "-" or "+" ("a+b-c" to "a", "+b", "-c")
class Solution {
public String solveEquation(String equation) {
String[] parts = equation.split("=");
String leftPart = parts[];
String rightPart = parts[];
int[] leftVals = evaluate(leftPart);
int[] rightVals = evaluate(rightPart);
int cntX = leftVals[] - rightVals[];
int cntNum = leftVals[] - rightVals[];
if (cntX == ) {
if (cntNum != ) {
return "No solution";
}
return "Infinite solutions";
}
int valX = (-cntNum) / cntX;
StringBuilder result = new StringBuilder();
result.append("x=").append(valX);
return result.toString();
} private int[] evaluate(String exp) {
int[] result = new int[];
String[] expElements = exp.split("(?=[-+])"); for (String ele : expElements) {
if (ele.equals("+x") || ele.equals("x")) {
result[]++;
} else if (ele.equals("-x")) {
result[]--;
} else if (ele.contains("x")) {
result[] += Integer.valueOf(ele.substring(, ele.indexOf("x")));
} else {
result[] += Integer.valueOf(ele);
}
}
return result;
}
}
Solve the Equation的更多相关文章
- ACM:HDU 2199 Can you solve this equation? 解题报告 -二分、三分
Can you solve this equation? Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...
- hdu 2199 Can you solve this equation?(二分搜索)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 2199:Can you solve this equation?(二分搜索)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 2199 Can you solve this equation?(高精度二分)
http://acm.hdu.edu.cn/howproblem.php?pid=2199 Can you solve this equation? Time Limit: 2000/1000 MS ...
- HDU 2199 Can you solve this equation? (二分 水题)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdoj 2199 Can you solve this equation?【浮点型数据二分】
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- Can you solve this equation?(二分)
Can you solve this equation? Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Ja ...
- Can you solve this equation?
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- HDU 2199 Can you solve this equation(二分答案)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU - 2199 Can you solve this equation? 二分 简单题
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
随机推荐
- TTTTTTTTTTTTTT hdu 5763 Another Meaning 哈希+dp
Another Meaning Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- web 新能优化
网上的东西太多了都是搜来的东西 留着自己看吧! 摘自 :http://www.cnblogs.com/50614090/archive/2011/08/19/2145620.html 打开网站慢现状分 ...
- django 网站上传资源的显示与配置
1. 上传资源的配置 1. 首先在项目里创建一个名称叫media的文件夹专门保存用户上传 2. settings.py文件配置上传资源的路径 # 上传资源路径,如果图片,上传文件等 MEDIA_UR ...
- 20165213 Exp 8 Web基础
Exp 8 Web基础 一.基础问题回答 (1)什么是表单 表单在网页中主要负责数据采集功能.一个表单有三个基本组成部分: 表单标签:这里面包含了处理表单数据所用CGI程序的URL以及数据提交到服务器 ...
- MySql 时区错误
mysql的时区错误问题: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one ...
- C++ 编程风格指南
C++ Programming Style Guidelines 他人翻译中文版:click 让程序具有好的可读性 “避免日后 有人(或者自己)指着你的代码骂娘:这特么谁写的破烂 玩意”(来自:知乎- ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 后 ...
- Mathematica——绘制3D图形
Plot3D Plot3D[ + y, {x, -, }, {y, -, }] ListPointPlot3D 绘制点集 ListPointPlot3D[{{, , }, {, , }}, Color ...
- spark streaming 6: BlockGenerator、RateLimiter
BlockGenerator和RateLimiter其实很简单,但是它包含了几个很重要的属性配置的处理,所以记录一下. ))) , SECONDS) From WizNote
- 阶段3 3.SpringMVC·_07.SSM整合案例_01.ssm整合说明
Spring去整合另外的两个框架