题目:
求解一个给定的方程,将x以字符串”x=#value”的形式返回。该方程仅包含’+’,’ - ‘操作,变量 x 和其对应系数。
如果方程没有解,请返回“No solution”。
如果方程有无限解,则返回“Infinite solutions”。
如果方程中只有一个解,要保证返回值 x 是一个整数。
示例 1:
输入: "x+5-3+x=6+x-2"
输出: "x=2"
示例 2:
输入: "x=x"
输出: "Infinite solutions"
示例 3:
输入: "2x=x"
输出: "x=0"
示例 4:
输入: "2x+3x-6x=x+2"
输出: "x=-1"
示例 5:
输入: "x=x+2"
输出: "No solution"
我的思路:
我是通过xListL 保存式子左边的带有x的系数,xListR 保存式子右边的带有x的系数,再用constantsListL保存式子左边的常数,constantsListR 保存式子右边的常数。
然后带有x的系数(左边减去右边)相减,常数(右边减去左边)相减,相当于数学的交换率,最后右边的常数除以左边的带有x的系数即答案了。
即如下所示:
2x = 42
x = 42/2
x= 21
当然,这里有些点需要注意:
(1)、左边带有x的系数为0的情况要考虑
(2)、系数可能为负数,不一定都是正整数。
我的解答:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
class Solution {
public String solveEquation(String equation) {
String equations[] = equation.split("=");
List<Integer> xListL = new ArrayList<>(); //x前面的参数(左边)
List<Integer> constantsListL = new ArrayList<>(); // 常数(左边)
List<Integer> xListR = new ArrayList<>(); //x前面的参数(右边)
List<Integer> constantsListR = new ArrayList<>(); // 常数(右边)
xListL = findXpre(equations[0],xListL);
xListR = findXpre(equations[1],xListR);
constantsListL = findNumbers(equations[0],constantsListL);
constantsListR = findNumbers(equations[1],constantsListR);
int xNumber = count(xListL)-count(xListR);
int constantNumber = count(constantsListR)-count(constantsListL);
if(xNumber==0 && constantNumber !=0){
return "No solution";
}
if(xNumber==0){
return "Infinite solutions";
}
double d = 1.0*constantNumber/xNumber;
return "x="+(int)d;
}
public int count(List<Integer> list){
int xNumber = 0;
Iterator<Integer> it = list.iterator();
while(it.hasNext()){
int y = it.next();
xNumber += y;
}
return xNumber;
}
public List<Integer> findNumbers(String str, List<Integer> xListL){
Pattern p= Pattern.compile("[+-]");
String s[] = p.split(str);
ArrayList list = new ArrayList();
for(int i=0;i<s.length;i++){
if(!s[i].equals("")){
list.add(s[i]);
}
}
s = (String[])list.toArray(new String[list.size()]);
int index = 0 ;
boolean isReduce = false;
if(!str.startsWith("-")){
str = "+"+str;
}
for( int j=0;j<str.length();j++){
int plus = str.indexOf("+",j);
int reduce = str.indexOf("-",j);
if((plus==-1 && reduce == -1)||index>=s.length){
return xListL;
}
if(plus<reduce&& plus != -1){
j = plus;
}
else if(reduce != -1){
j = reduce;
isReduce = true;
}
if(s[index].contains("x")){
index++;
isReduce = false;
continue;
}else{
if(isReduce){
xListL.add(-Integer.parseInt(s[index]));
isReduce = false;
}else{
xListL.add(Integer.parseInt(s[index]));
}
index++;
}
}
return xListL;
}
public List<Integer> findXpre(String str, List<Integer> xListL){
for(int i=0;i<str.length();i++){
i = str.indexOf("x",i);
if(i != -1){
if(i == 0){
xListL.add(1);
}else{
int number = findNumber(str,i,i);
xListL.add(number);
}
}else{
return xListL;
}
}
return xListL;
}
private int findNumber(String str, int begin, int end) {
if(begin==0){
return Integer.parseInt(str.substring(begin,end));
}
char c = str.charAt(begin-1);
if(c=='-'||c=='+'){
if(begin == end){
if(c=='-'){
return -1;
}else{
return 1;
}
}
int finalNumber = Integer.parseInt(str.substring(begin,end));
if(c=='-'){
return -finalNumber;
}
return finalNumber;
}else{
return findNumber(str,begin-1,end);
}
}
}
用时20ms,速度稍显慢了,如果有大神有更好的解法,请分享一下,共同进步!
当然,有不懂的同学也欢迎提问,我尽量解释清楚!