public boolean isRationalEqual(String S, String T) {
if (S.equals(T)) {
return true;
}
long[] shu = getShu(S);
long[] shu2 = getShu(T);
return shu[0] * shu2[1] == shu[1] * shu2[0];
}
//小数化分数
private long[] getShu(String s) {
if (s.indexOf(".") < 0) {
return new long[]{Long.parseLong(s), 1};
}
int point = s.indexOf(".");
//整数部分
long zheng = Long.parseLong(s.substring(0, point));
s = s.substring(point + 1);
int kuo = s.indexOf("(");
if (kuo < 0) {
long xs = s.length() == 0 ? 0 : Long.parseLong(s);
long num = getTen(s.length());
return new long[]{xs + zheng * num, num};
}
String xsS = s.substring(0, kuo);
//不重复部分
long xs = xsS.length() == 0 ? 0 : Long.parseLong(xsS);
long num = getTen(xsS.length());
long xc = Long.parseLong(s.substring(kuo + 1, s.indexOf(")")));
//重复部分
long num2 = getTen(s.substring(kuo + 1, s.indexOf(")")).length());
long fenm = (num2 - 1) * num;
//凑成分数
return new long[]{zheng * fenm + xs * (num2 - 1) + xc, fenm};
}
private long getTen(long len) {
int num = 1;
for (int i = 0; i < len; i++) {
num *= 10;
}
return num;
}
leetcode-972. 相等的有理数
最新推荐文章于 2021-07-19 17:57:18 发布