-
Notifications
You must be signed in to change notification settings - Fork 1
/
166.分数到小数.java
93 lines (87 loc) · 2.46 KB
/
166.分数到小数.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* @lc app=leetcode.cn id=166 lang=java
*
* [166] 分数到小数
*
* https://leetcode-cn.com/problems/fraction-to-recurring-decimal/description/
*
* algorithms
* Medium (24.75%)
* Likes: 76
* Dislikes: 0
* Total Accepted: 5.7K
* Total Submissions: 22.8K
* Testcase Example: '1\n2'
*
* 给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数。
*
* 如果小数部分为循环小数,则将循环的部分括在括号内。
*
* 示例 1:
*
* 输入: numerator = 1, denominator = 2
* 输出: "0.5"
*
*
* 示例 2:
*
* 输入: numerator = 2, denominator = 1
* 输出: "2"
*
* 示例 3:
*
* 输入: numerator = 2, denominator = 3
* 输出: "0.(6)"
*
*
*/
// @lc code=start
class Solution {
public String fractionToDecimal(int numerator, int denominator) {
if (numerator == 0) {
return "0";
}
if (numerator % denominator == 0) {
return "" + ((long) numerator / denominator);
}
int prev = numerator / denominator;
StringBuilder value = new StringBuilder(prev + ".");
List<Long> remainders = new ArrayList<>();
List<Long> quotients = new ArrayList<>();
long remainder = numerator;
boolean rec = false;
int recStart;
while (true) {
remainder = remainder % denominator;
if (remainder == 0) {
recStart = remainders.indexOf(remainder);
break;
}
if (remainders.contains(remainder)) {
rec = true;
recStart = remainders.indexOf(remainder);
break;
}
remainders.add(remainder);
long quotient = remainder * 10 / denominator;
remainder = remainder * 10 % denominator;
quotients.add(quotient);
}
for (int i = 0; i < quotients.size(); i++) {
if (rec && i == recStart) {
value.append("(");
}
value.append(Math.abs(quotients.get(i)));
}
if (rec) {
value.append(")");
}
if (((numerator ^ denominator) & 0x80000000) == 0x80000000
&& !value.toString().equals("0") && !value.toString().startsWith("-")) {
value.insert(0, "-");
}
// return numerator + "/" + denominator + "=" + value.toString();
return value.toString();
}
}
// @lc code=end