Skip to content

Commit

Permalink
Merge pull request #586 from 0xff-dev/166
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 166
  • Loading branch information
6boris authored Aug 22, 2023
2 parents 7739639 + 21cc5e8 commit 59bc6a0
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 12 deletions.
38 changes: 38 additions & 0 deletions leetcode/101-200/0166.Fraction-to-Recurring-Decimal/README.md
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 leetcode/101-200/0166.Fraction-to-Recurring-Decimal/Solution.go
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 ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
n, d int
expect string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 1, 2, "0.5"},
{"TestCase2", 2, 1, "2"},
{"TestCase3", 4, 333, "0.(012)"},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.n, c.d)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.n, c.d)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 59bc6a0

Please sign in to comment.