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. HDU 5335 Walk Out BFS 比较坑

    H - H Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status ...

  2. less的解析笔记

    Less是一种动态的样式语言.Less扩展了CSS的动态行为,比如说,设置变量(Variables).混合书写模式(mixins).操作(operations)和功能(functions)等等,最棒的 ...

  3. [BZOJ4827][Hnoi2017]礼物(FFT)

    4827: [Hnoi2017]礼物 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1315  Solved: 915[Submit][Status] ...

  4. java动态生成HTML文件

    在eclipse中,用java动态生成html文件. //用于存储html字符串 StringBuilder stringHtml = new StringBuilder(); try{ //打开文件 ...

  5. What makes a good feature

    import numpy as np import matplotlib.pyplot as plt greyhounds = 500 # 灰猎犬500只 labs = 500 # 拉布拉多犬500只 ...

  6. Backen-Development record 1

    单例模式 在应用这个模式时,单例对象的类必须保证只有一个实例存在. 服务进程中的其他对象再通过这个单例对象获取这些配置信息.这种方式简化了在复杂环境下的配置管理. __new__实现 用装饰器实现单例 ...

  7. 重入锁 ReentrantLock (转)(学习记录)

    重入锁(ReentrantLock)是一种递归无阻塞的同步机制.以前一直认为它是synchronized的简单替代,而且实现机制也不相差太远.不过最近实践过程中发现它们之间还是有着天壤之别. 以下是官 ...

  8. vscode+python+flake8+cmder配置

    {     "window.zoomLevel": 0,     "[python]": {},     "kite.showWelcomeNotif ...

  9. 码云上webide怎么提交

    修改后想提交,它会提示:“暂存文件后才能提交”, 我拿放大镜找遍了整个界面也没找到“暂存”按钮, 原来,文件旁边那个+号就是暂存,好歹鼠标方式去之后给个tip,服了. 点一下这个加号,提交按钮就可用了 ...

  10. Pythonnumpy提取矩阵的某一行或某一列的实例

    Python numpy 提取矩阵的某一行或某一列的实例 下面小编就为大家分享一篇Python numpy 提取矩阵的某一行或某一列的实例,具有很好的参考价值,希望对大家有所帮助.一起跟随小编过来看看 ...