Equal Rational Numbers LeetCode solution in Java

1
class Solution {
2

3
private List<Double> ratios = Arrays.asList(1.0, 1.0 / 9, 1.0 / 99, 1.0 / 999, 1.0 / 9999);
4

5
public boolean isRationalEqual(String S, String T) {
6
return Math.abs(computeValue(S) - computeValue(T)) < 1e-9;
7
}
8

9
private double computeValue(String s) {
10
if (!s.contains("(")) {
11
return Double.valueOf(s);
12
} else {
13
double intNonRepeatingValue = Double.valueOf(s.substring(0, s.indexOf('(')));
14
int nonRepeatingLength = s.indexOf('(') - s.indexOf('.') - 1;
15
int repeatingLength = s.indexOf(')') - s.indexOf('(') - 1;
16
int repeatingValue = Integer.parseInt(s.substring(s.indexOf('(') + 1, s.indexOf(')')));
17
return intNonRepeatingValue
18
+ repeatingValue * Math.pow(0.1, nonRepeatingLength) * ratios.get(repeatingLength);
19
}
20
}
21
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0