Skip to content

Latest commit

 

History

History
147 lines (58 loc) · 3.27 KB

File metadata and controls

147 lines (58 loc) · 3.27 KB

中文文档

Description

Given two strings S and T, each of which represents a non-negative rational number, return True if and only if they represent the same number. The strings may use parentheses to denote the repeating part of the rational number.

In general a rational number can be represented using up to three parts: an integer part, a non-repeating part, and a repeating part. The number will be represented in one of the following three ways:

    <li><code>&lt;IntegerPart&gt;</code> (e.g. 0, 12, 123)</li>
    
    <li><code>&lt;IntegerPart&gt;&lt;.&gt;&lt;NonRepeatingPart&gt;</code> &nbsp;(e.g. 0.5, 1., 2.12, 2.0001)</li>
    
    <li><code>&lt;IntegerPart&gt;&lt;.&gt;&lt;NonRepeatingPart&gt;&lt;(&gt;&lt;RepeatingPart&gt;&lt;)&gt;</code> (e.g. 0.1(6), 0.9(9), 0.00(1212))</li>
    

The repeating portion of a decimal expansion is conventionally denoted within a pair of round brackets.  For example:

1 / 6 = 0.16666666... = 0.1(6) = 0.1666(6) = 0.166(66)

Both 0.1(6) or 0.1666(6) or 0.166(66) are correct representations of 1 / 6.

 

Example 1:

Input: S = "0.(52)", T = "0.5(25)"

Output: true

Explanation:

Because "0.(52)" represents 0.52525252..., and "0.5(25)" represents 0.52525252525..... , the strings represent the same number.

Example 2:

Input: S = "0.1666(6)", T = "0.166(66)"

Output: true

Example 3:

Input: S = "0.9(9)", T = "1."

Output: true

Explanation: 

"0.9(9)" represents 0.999999999... repeated forever, which equals 1.  [See this link for an explanation.]

"1." represents the number 1, which is formed correctly: (IntegerPart) = "1" and (NonRepeatingPart) = "".

 

Note:

    <li>Each part consists only of digits.</li>
    
    <li>The <code>&lt;IntegerPart&gt;</code>&nbsp;will&nbsp;not begin with 2 or more zeros.&nbsp; (There is no other restriction on the digits of each part.)</li>
    
    <li><code>1 &lt;= &lt;IntegerPart&gt;.length &lt;= 4 </code></li>
    
    <li><code>0 &lt;= &lt;NonRepeatingPart&gt;.length &lt;= 4 </code></li>
    
    <li><code>1 &lt;= &lt;RepeatingPart&gt;.length &lt;= 4</code></li>
    

Solutions

Python3

Java

...