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 xis 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 "=":
leftPartis x+5-3+x;rightPartis 6+x-2; - Secondly, we sum up coefficient and the rest numbers separately, i.e.
leftValsis 2x + 2, i.e., [2, 2];rightValsis 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 ( ...
随机推荐
- jquery live()方法 语法
jquery live()方法 语法 作用:live() 方法为被选元素附加一个或多个事件处理程序,并规定当这些事件发生时运行的函数.通过 live() 方法附加的事件处理程序适用于匹配选择器的当前及 ...
- luogu3812 【模板】线性基
Code: #include <cstdio> #include <algorithm> #define ll long long #define N 64 #define s ...
- 深度理解链式前向星——转载自ACdreamer
转载自ACdreamer [转载]深度理解链式前向星 我们首先来看一下什么是前向星. 前向星是一种特殊的边集数组,我们把边集数组中的每一条边按照起点从小到大排序,如果起点相同就按照终点从小到大排序 ...
- 最近公共祖先LCA(Tarjan算法)的思考和算法实现——转载自Vendetta Blogs
LCA 最近公共祖先 Tarjan(离线)算法的基本思路及其算法实现 小广告:METO CODE 安溪一中信息学在线评测系统(OJ) //由于这是第一篇博客..有点瑕疵...比如我把false写成了f ...
- mac重启php7.0
killall php-fpm /usr/local/opt/php70/sbin/php70-fpm restart
- codeforces269B
Greenhouse Effect CodeForces - 269B Emuskald is an avid horticulturist and owns the world's longest ...
- SpringBoot的文件上传&下载
前言:不多BB直接上代码 文件上传 pom依赖添加commons-io <!-- 上传/下载jar https://mvnrepository.com/artifact/commons-io/c ...
- js对象之间的"继承"的五种方法
今天要介绍的是,对象之间的"继承"的五种方法. 比如,现在有一个"动物"对象的构造函数. function Animal(){ this.species = & ...
- java 百度地图判断两点距离2
package baiduApi; public class BaiDuMap { static double DEF_PI = 3.14159265359; // PI static double ...
- Win10 的微软输入法输入稍快竟然会导致死机
一周前,新装机器一次,竟然死机两三次,多发生在敲字时,最近逐步排查发现的这个问题,查阅了一下网上方案,果断采用了第三方输入法,至今没再死机过. 不过第三方输入法也不安分,是不是推送点头条新闻过来,和驱 ...