Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.
If the fractional part is repeating, enclose the repeating part in parentheses.
Example 1:
Input: numerator = 1, denominator = 2 Output: "0.5"
Example 2:
Input: numerator = 2, denominator = 1 Output: "2"
Example 3:
Input: numerator = 2, denominator = 3 Output: "0.(6)"
class Solution {
//一旦出现了重复的余数,就说明结果是无限循环小数
public String fractionToDecimal(int numerator, int denominator) {
if(numerator == 0) {
return "0";
}
StringBuilder res = new StringBuilder();
//numerator和denominator都不为0
res.append( (numerator > 0) ^ (denominator > 0) ? "-" : "" );//符号位
long num = Math.abs((long)numerator);
long den = Math.abs((long)denominator);
//integral part
res.append(num / den);//商
num %= den;//余数
if(num == 0){//整除了
return res.toString();
}
// fractional part
res.append(".");
HashMap<Long, Integer> map = new HashMap<Long, Integer>();
map.put(num, res.length());//记录余数 以及 (余数*10)除以denominator的商位置
while (num != 0) {
num *= 10;
res.append(num / den);
num %= den;
if(map.containsKey(num)) {//余数出现重复
int index = map.get(num);
res.insert(index, "(");
res.append(")");
break;
}
else{
map.put(num, res.length());
}
}
return res.toString();//string builder to string
}
}