Solve the Equation
来源:互联网 发布:眼睛变大知乎 编辑:程序博客网 时间:2024/05/24 20:07
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"
分成等号的左右两个部分,分别parse出未知数x前的系数,以及常数和, 然后计算
解析的过程中:
1. 碰到+ - 符号需要变号, 但是需要注意前面是常数的结束,需要结算 (如果是带有未知数的,则在第3步计算)
2. 遇到数字那就累加数字
3. 遇到x, 那么结算当前的x的系数,如果number != 0, 则说明有系数;如果 == 0 那么有两种可能 1. 如果x的index >0, 那么如果系数==0 则真的是==0,2. 如果x的index=0,那么默认系数就是1
代码:
class Solution { class Wrap { int numX; int sumConstant; Wrap(int numX, int sumConstant) { this.numX = numX; this.sumConstant = sumConstant; } } public String solveEquation(String equation) { String[] split = equation.split("="); Wrap left = parse(split[0]); Wrap right = parse(split[1]); int xCount = left.numX - right.numX; int constant = right.sumConstant - left.sumConstant; if(xCount == 0 && constant == 0) return "Infinite solutions"; if(xCount == 0) return "No solution"; return "x=" + (constant/xCount); } private Wrap parse(String string) { int numX = 0; int sumConstant = 0; int number = 0; boolean positive = true; for(int i=0;i<string.length();i++) { char ch = string.charAt(i); if(ch=='+' || ch=='-') { if(number != 0) { if(positive) { sumConstant += number; } else sumConstant -= number; number = 0; } if(ch=='+'){ positive = true; continue; } if(ch=='-') { positive = false; continue; } } if(ch>='0' && ch<='9') { number = number * 10 + (ch - '0'); } else if( ch == 'x') { if((i>0 && number == 0 && string.charAt(i-1)!='0' ) || (number == 0 && i==0)) number = 1; if(positive) { numX += number; } else numX -= number; number = 0; positive = true; } } if(number != 0) { if(positive) { sumConstant += number; } else sumConstant -= number; } return new Wrap(numX, sumConstant); }}
阅读全文
0 0
- 640. Solve the Equation
- 640. Solve the Equation
- Solve the Equation
- [leetcode]640. Solve the Equation
- leetcode 640. Solve the Equation
- LeetCode 640 : Solve the Equation(python)
- Solve the Equation问题及解法
- Solve equation
- Solve equation
- Solve equation
- Solve equation
- Solve equation
- hdu 2199~Can you solve the equation?~二分法求解
- Can you solve the equation?(二分求方程解)
- 646. Maximum Length of Pair Chain/640. Solve the Equation
- FZOJ2102:Solve equation
- FZU 2102 Solve equation
- FZU 2102 Solve equation
- 05-1 JS的DOM
- how to fix "Cannot find a valid baseurl for repo: poptop-stable/7" in centos 7 *64 when use "yum -y
- 在CentOS上搭建PHP服务器环境
- How to get the SHA-1 fingerprint certificate in Android Studio for debug mode?
- java.lang.ClassNotFoundException: com.sun.faces.config.ConfigureListener
- Solve the Equation
- Android百度地图实例详解之仿摩拜单车APP(包括附近车辆、规划路径、行驶距离、行驶轨迹记录,导航等)
- OpenGL(LibGDX)激光束
- 方正何处去?
- 想要
- Script到Code Blocks、Code Behind到MVC、MVP、MVVM
- Linux下spi驱动开发
- 基于S3C2440的嵌入式Linux驱动——SPI子系统解读(一)
- S3C2440 Linux驱动移植——SPI