题目不难,但是感觉很考写代码的功底
Given a string representing an expression of fraction addition and subtraction, you need to return the calculation result in string format. The final result should be irreducible fraction. If your final result is an integer, say 2
, you need to change it to the format of fraction that has denominator 1
. So in this case, 2
should be converted to 2/1
.
Example 1:
Input:"-1/2+1/2" Output: "0/1"
Example 2:
Input:"-1/2+1/2+1/3" Output: "1/3"
Example 3:
Input:"1/3-1/2" Output: "-1/6"
Example 4:
Input:"5/3+1/3" Output: "2/1"
Note:
- The input string only contains
'0'
to'9'
,'/'
,'+'
and'-'
. So does the output. - Each fraction (input and output) has format
±numerator/denominator
. If the first input fraction or the output is positive, then'+'
will be omitted. - The input only contains valid irreducible fractions, where the numerator and denominator of each fraction will always be in the range [1,10]. If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above.
- The number of given fractions will be in the range [1,10].
- The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int.
public class Solution {
public String fractionAddition(String expression) {
ArrayList<Character> judge=new ArrayList<>();
ArrayList<Integer> fenziList=new ArrayList<>();
ArrayList<Integer> fenmuList=new ArrayList<>();
String temp=expression;
if(expression.length()==0){
return "0";
}
int cur=0;
if(expression.charAt(0)=='-'){
judge.add(expression.charAt(0));
expression=expression.substring(1,expression.length());
}else{
judge.add('+');
}
for(int i=0;i<expression.length();i++){
if(expression.charAt(i)=='-'||expression.charAt(i)=='+'){
judge.add(expression.charAt(i));
String str=expression.substring(cur,i);
cur=i+1;
String[] words=str.split("/");
fenziList.add(Integer.valueOf(words[0]));
fenmuList.add(Integer.valueOf(words[1]));
}
if(i==expression.length()-1){
String str=expression.substring(cur,expression.length());
String[] words=str.split("/");
fenziList.add(Integer.valueOf(words[0]));
fenmuList.add(Integer.valueOf(words[1]));
}
}
if(fenziList.size()<=1){
return temp;
}
int temptemp=maxCommonDivisor(fenmuList.get(0),fenmuList.get(1));
int minCommonMultiple=fenmuList.get(0)*fenmuList.get(1)/temptemp;
for(int i=2;i<fenmuList.size();i++){
temptemp=maxCommonDivisor(minCommonMultiple,fenmuList.get(i));
minCommonMultiple=fenmuList.get(i)*minCommonMultiple/temptemp;
}
int ans=0;
ArrayList<Integer> newFenziList=new ArrayList<>();
for(int i=0;i<fenziList.size();i++){
newFenziList.add(minCommonMultiple/fenmuList.get(i)*fenziList.get(i));
}
for(int i=0;i<fenziList.size();i++){
if(judge.get(i)=='+'){
ans=ans+newFenziList.get(i);
}else{
ans=ans-newFenziList.get(i);
}
}
if(ans==0){
return "0/1";
}
int tempMaxCommonDivisor=maxCommonDivisor(ans,minCommonMultiple);
ans=ans/tempMaxCommonDivisor;
minCommonMultiple=minCommonMultiple/tempMaxCommonDivisor;
if(ans<0||minCommonMultiple<0){
ans=Math.abs(ans);
minCommonMultiple=Math.abs(minCommonMultiple);
return "-"+ans+"/"+minCommonMultiple;
}
return ans+"/"+minCommonMultiple;
}
public static int maxCommonDivisor(int m, int n) {
if (m < n) {// 保证m>n,若m<n,则进行数据交换
int temp = m;
m = n;
n = temp;
}
while (m % n != 0) {// 在余数不能为0时,进行循环
int temp = m % n;
m = n;
n = temp;
}
return n;// 返回最大公约数
}
}