-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #586 from 0xff-dev/166
Add solution and test-cases for problem 166
- Loading branch information
Showing
3 changed files
with
113 additions
and
12 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
leetcode/101-200/0166.Fraction-to-Recurring-Decimal/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# [166.Fraction to Recurring Decimal][title] | ||
|
||
## Description | ||
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. | ||
|
||
If multiple answers are possible, return **any of them**. | ||
|
||
It is **guaranteed** that the length of the answer string is less than `10^4` for all the given inputs. | ||
|
||
**Example 1:** | ||
|
||
``` | ||
Input: numerator = 1, denominator = 2 | ||
Output: "0.5" | ||
``` | ||
|
||
**Example 2:** | ||
|
||
``` | ||
Input: numerator = 2, denominator = 1 | ||
Output: "2" | ||
``` | ||
|
||
**Example 3:** | ||
|
||
``` | ||
Input: numerator = 4, denominator = 333 | ||
Output: "0.(012)" | ||
``` | ||
|
||
## 结语 | ||
|
||
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] | ||
|
||
[title]: https://leetcode.com/problems/fraction-to-recurring-decimal | ||
[me]: https://github.com/kylesliu/awesome-golang-algorithm |
67 changes: 65 additions & 2 deletions
67
leetcode/101-200/0166.Fraction-to-Recurring-Decimal/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,68 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
import ( | ||
"bytes" | ||
"fmt" | ||
) | ||
|
||
func Solution(numerator int, denominator int) string { | ||
const maxn = 65536 | ||
var flag bool | ||
if numerator*denominator < 0 { | ||
flag = false | ||
} else { | ||
flag = true | ||
} | ||
if numerator%denominator == 0 { | ||
return fmt.Sprintf("%d", numerator/denominator) | ||
} | ||
if numerator < 0 { | ||
numerator = -numerator | ||
} | ||
if denominator < 0 { | ||
denominator = -denominator | ||
} | ||
quotient := make([]int, maxn) | ||
cf := make([]int, maxn) | ||
cf[0] = numerator % denominator | ||
quotient[0] = numerator / denominator | ||
buf := bytes.NewBufferString("") | ||
if flag { | ||
buf.WriteString(fmt.Sprintf("%d.", quotient[0])) | ||
} else { | ||
buf.WriteString(fmt.Sprintf("-%d.", quotient[0])) | ||
} | ||
for i := 0; i < denominator-1; i++ { | ||
tmp := (cf[i] * 10) % denominator | ||
quotient[i+1] = (cf[i] * 10) / denominator | ||
if tmp == 0 { | ||
for index := 1; index <= i+1; index++ { | ||
buf.WriteString(fmt.Sprintf("%d", quotient[index])) | ||
} | ||
return buf.String() | ||
} | ||
cf[i+1] = tmp | ||
for index := 0; index <= i; index++ { | ||
if cf[i+1] == cf[index] { | ||
if index != 0 { | ||
for inner := 1; inner <= index; inner++ { | ||
buf.WriteString(fmt.Sprintf("%d", quotient[inner])) | ||
} | ||
buf.WriteString("(") | ||
for inner := index + 1; inner <= i+1; inner++ { | ||
buf.WriteString(fmt.Sprintf("%d", quotient[inner])) | ||
} | ||
buf.WriteString(")") | ||
} else { | ||
buf.WriteString("(") | ||
for inner := 1; inner <= i+1; inner++ { | ||
buf.WriteString(fmt.Sprintf("%d", quotient[inner])) | ||
} | ||
buf.WriteString(")") | ||
} | ||
return buf.String() | ||
} | ||
} | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters