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 "=":
    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 that
exp.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的更多相关文章

  1. 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 ...

  2. hdu 2199 Can you solve this equation?(二分搜索)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  3. hdu 2199:Can you solve this equation?(二分搜索)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  4. 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 ...

  5. HDU 2199 Can you solve this equation? (二分 水题)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  6. hdoj 2199 Can you solve this equation?【浮点型数据二分】

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  7. Can you solve this equation?(二分)

    Can you solve this equation? Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Ja ...

  8. Can you solve this equation?

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...

  9. HDU 2199 Can you solve this equation(二分答案)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  10. HDU - 2199 Can you solve this equation? 二分 简单题

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

随机推荐

  1. java常见问题 ——运行报错1

    错误1 打印乱码 相关代码 response.getWriter().print(tbItem.toString()); response.setContentType("text/html ...

  2. java多线程批量下载文件

    多线程下载文件 平时开发中有时会用到文件下载,为了提高文件的下载速率,采用多线程下载能够达到事半功倍的效果: package test; /** * 文件下载类 * @author luweichen ...

  3. OI路上 day -9

    /* 嗯还有9天. 就只有9天了. 啊还剩9天吖! 多年后 我可能还会记得 那些年,我们学过的算法. 多年后 我可能会对别人说 我学过OI我喜欢OI并一直热爱着它. 9天后 我可能再也不会来到这个地方 ...

  4. noi.ac #546 分组

    题目链接:戳我 题目描述 现在有n个字符串,你需要从中选出一些字符串,使得选出的字符串能被分组,满足每组大小为2,且可以从每组选出该组的两个字符串的一个非空公共后缀,使得每组选出的串互不相同. 输入格 ...

  5. vue中改变数组或对象,页面没做出对应的渲染

    原文链接 数组更新检测 变异方法 Vue 包含一组观察数组的变异方法,所以它们也将会触发视图更新.这些方法如下: push() pop() shift() unshift() splice() sor ...

  6. 3dmax 欢迎页卡住

    C:\Program Files (x86)\Autodesk\3ds Max 9\stdplugs\dxplugins\dxDDS.mbi,把dxDDS.mbi重命名就OK了,只不过会提示他加载错误 ...

  7. php 调用python接口出现的一系列问题(原)

    调用示例代码(python写的一个谷歌翻译接口): $name = '中国'; exec("/mob360/EditImage/venv/bin/python /EditImage/fany ...

  8. [CSP-S模拟测试]:那一天我们许下约定(DP+组合数学)

    题目传送门(内部题2) 输入格式 每个测试点有多组测试数据.对于每组数据,有一行共三个整数$N$,$D$,$M$含义如题.输入结束标识为$“0 0 0”$ (不含引号). 输出格式 对于每组数据,输出 ...

  9. Java - 可循环队列

    队列是一种特殊的线性表,是一种先进先出的数据结构.只允许在表的前端进行删除操作,在表的后端进行插入操作.进行插入操作的端称为队尾,进行删除操作的端称为队头.队列中没有元素时,称为空队列. 简单的循环队 ...

  10. 获取客户端ip(包含中间反向代理)

    /** * 根据request获取用户的IP地址 * @param request * @return */ public static String getRemoteHost(HttpServle ...