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 ( ...
随机推荐
- 【Java Web】简易商品信息管理系统——首个Web项目
正文之前 在学习了一段时间的Java Web的内容之后,当然需要有个项目来练练手,我相信大多数人的首选项目都是信息管理系统吧,所以我选择了商品信息管理系统 目前项目源码已全部上传至GitHub,欢迎大 ...
- Linux内核概述
概述 1. 多数服务器都是Linux,Windows只在PC方面应用. 2. .NET只能在Windows中应用,适用于中小型项目,在大型项目中应用很少.现在出现了Windows服务器(外围的服务器) ...
- apt 和 apt-get的区别
apt 和 apt-get的区别 - liudsl的博客 - CSDN博客 https://blog.csdn.net/liudsl/article/details/79200134 Linux软件 ...
- 推荐系统系列(一):FM理论与实践
背景 在推荐领域CTR(click-through rate)预估任务中,最常用到的baseline模型就是LR(Logistic Regression).对数据进行特征工程,构造出大量单特征,编码之 ...
- JS中的原型链和原型的认识
这篇文章主要是学习一下JavaScript中的难点------原型和原型链 自定义一个对象 我们学习一门编程语言,必然要使用它完成一些特定的功能,而面向对象的语言因为符合人类的认知规律,在这方面做得很 ...
- Flutter文本框TextField
参数详解TextField同时也使用Text 的部分属性: 属性 作用controller 控制器,如同 Android View iddecoration 输入器装饰keyboardType 输入的 ...
- 2018-2019-2 20165215《网络对抗技术》Exp8 Web基础
目录 实验内容 基础问题回答 实验步骤 (一)Web前端HTML (二) Web前端javascipt (三)Web后端:MySQL基础:正常安装.启动MySQL,建库.创建用户.修改密码.建表 (四 ...
- Mac安装软件提示文件损坏
Mac安装软件提示文件损坏,请移至废纸篓 sudo spctl --master-disable
- vmvare ubuntu虚拟机固定ip设置
1.vmvare网络设置 2.window网络设置 3.ubuntu网络设置 先ifconfig 查看ip 已经设置为192.168.122.128 sudo vim /etc/network/int ...
- Mysql 纪录用户操作日志
有时,我们想追踪某个数据库操作记录,如想找出是谁操作了某个表(比如谁将字段名改了). 二进制日志记录了操作记录,线程号等信息,但是却没有记录用户信息,因此需要结合init-connect来实现追踪. ...