Nav: << previous: 165.比较版本号 | next: 167.两数之和 II - 输入有序数组 >>


Description

tab: English
 
<p>Given two integers representing the <code>numerator</code> and <code>denominator</code> of a fraction, return <em>the fraction in string format</em>.</p>
 
<p>If the fractional part is repeating, enclose the repeating part in parentheses</p>
 
<p>If multiple answers are possible, return <strong>any of them</strong>.</p>
 
<p>It is <strong>guaranteed</strong> that the length of the answer string is less than <code>10<sup>4</sup></code> for all the given inputs.</p>
 
<p><strong>Note</strong> that if the fraction can be represented as a <em>finite length string</em>, you <strong>must</strong> return it.</p>
 
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
 
<pre>
<strong>Input:</strong> numerator = 1, denominator = 2
<strong>Output:</strong> &quot;0.5&quot;
</pre>
 
<p><strong class="example">Example 2:</strong></p>
 
<pre>
<strong>Input:</strong> numerator = 2, denominator = 1
<strong>Output:</strong> &quot;2&quot;
</pre>
 
<p><strong class="example">Example 3:</strong></p>
 
<pre>
<strong>Input:</strong> numerator = 4, denominator = 333
<strong>Output:</strong> &quot;0.(012)&quot;
</pre>
 
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
 
<ul>
	<li><code>-2<sup>31</sup> &lt;=&nbsp;numerator, denominator &lt;= 2<sup>31</sup> - 1</code></li>
	<li><code>denominator != 0</code></li>
</ul>
 
 
 
> [!tip]- Hint 1
> 
> No scary math, just apply elementary math knowledge. Still remember how to perform a <i>long division</i>?
 
> [!tip]- Hint 2
> 
> Try a long division on 4/9, the repeating part is obvious. Now try 4/333. Do you see a pattern?
 
> [!tip]- Hint 3
> 
> Notice that once the remainder starts repeating, so does the divided result.
 
> [!tip]- Hint 4
> 
> Be wary of edge cases! List out as many test cases as you can think of and test your code thoroughly.
 
 
---
 
[submissions](https://leetcode.com/problems/fraction-to-recurring-decimal/submissions/) | [solutions](https://leetcode.com/problems/fraction-to-recurring-decimal/solutions/)
 
 
tab: 中文
 
<p>给定两个整数,分别表示分数的分子&nbsp;<code>numerator</code> 和分母 <code>denominator</code>,以 <strong>字符串形式返回小数</strong> 。</p>
 
<p>如果小数部分为循环小数,则将循环的部分括在括号内。</p>
 
<p>如果存在多个答案,只需返回 <strong>任意一个</strong> 。</p>
 
<p>对于所有给定的输入,<strong>保证</strong> 答案字符串的长度小于 <code>10<sup>4</sup></code> 。</p>
 
<p><strong>注意</strong>,如果分数可以表示为有限长度的字符串,则 <strong>必须</strong> 返回它。</p>
 
<p>&nbsp;</p>
 
<p><strong>示例 1:</strong></p>
 
<pre>
<strong>输入:</strong>numerator = 1, denominator = 2
<strong>输出:</strong>"0.5"
</pre>
 
<p><strong>示例 2:</strong></p>
 
<pre>
<strong>输入:</strong>numerator = 2, denominator = 1
<strong>输出:</strong>"2"
</pre>
 
<p><strong>示例 3:</strong></p>
 
<pre>
<strong>输入:</strong>numerator = 4, denominator = 333
<strong>输出:</strong>"0.(012)"
</pre>
 
<p>&nbsp;</p>
 
<p><strong>提示:</strong></p>
 
<ul>
	<li><code>-2<sup>31</sup> &lt;=&nbsp;numerator, denominator &lt;= 2<sup>31</sup> - 1</code></li>
	<li><code>denominator != 0</code></li>
</ul>
 
 
 
> [!tip]- 提示 1
> 
> No scary math, just apply elementary math knowledge. Still remember how to perform a <i>long division</i>?
 
> [!tip]- 提示 2
> 
> Try a long division on 4/9, the repeating part is obvious. Now try 4/333. Do you see a pattern?
 
> [!tip]- 提示 3
> 
> Notice that once the remainder starts repeating, so does the divided result.
 
> [!tip]- 提示 4
> 
> Be wary of edge cases! List out as many test cases as you can think of and test your code thoroughly.
 
 
---
 
[提交记录](https://leetcode.cn/problems/fraction-to-recurring-decimal/submissions/) | [题解](https://leetcode.cn/problems/fraction-to-recurring-decimal/solution/)
 
 

Solutions & Notes

properties:
  note.updated:
    displayName: Last Updated
  note.relative_links:
    displayName: Related Links
  note.desc:
    displayName: Description
  note.grade:
    displayName: Rating
  note.program_language:
    displayName: Language
  note.time_complexity:
    displayName: TC
  note.space_complexity:
    displayName: SC
views:
  - type: table
    name: Solutions & Notes
    filters:
      and:
        - file.hasLink(this.file)
        - file.tags.containsAny("leetcode/solution", "leetcode/note")
    order:
      - file.name
      - desc
      - program_language
      - time_complexity
      - space_complexity
      - grade
      - relative_links
      - updated
    sort:
      - property: grade
        direction: ASC
      - property: time_complexity
        direction: ASC
      - property: program_language
        direction: ASC
    columnSize:
      file.name: 104
      note.space_complexity: 65
      note.grade: 126
 

Similar Problems

properties:
  note.lcTopics:
    displayName: Topics
  note.lcAcRate:
    displayName: AC Rate
  note.favorites:
    displayName: Favorites
  note.grade:
    displayName: Rating
  note.translatedTitle:
    displayName: Title (CN)
  note.lcDifficulty:
    displayName: Difficulty
views:
  - type: table
    name: Similar Problems
    filters:
      and:
        - file.hasLink(this.file)
        - similarQuestions.contains(this.file)
    order:
      - file.name
      - translatedTitle
      - lcTopics
      - lcDifficulty
      - lcAcRate
      - grade
      - favorites
    sort:
      - property: file.name
        direction: ASC
      - property: lcTopics
        direction: DESC
    columnSize:
      note.translatedTitle: 240
      note.lcTopics: 347
      note.lcAcRate: 75
      note.grade: 122