★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-pgwlzqwp-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
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.git
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:github
<IntegerPart>
(e.g. 0, 12, 123)<IntegerPart><.><NonRepeatingPart>
(e.g. 0.5, 1., 2.12, 2.0001)<IntegerPart><.><NonRepeatingPart><(><RepeatingPart><)>
(e.g. 0.1(6), 0.9(9), 0.00(1212))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)this
Both 0.1(6) or 0.1666(6) or 0.166(66) are correct representations of 1 / 6.spa
Example 1:rest
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:code
Input: S = "0.1666(6)", T = "0.166(66)" Output: true
Example 3:orm
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:htm
<IntegerPart>
will not begin with 2 or more zeros. (There is no other restriction on the digits of each part.)1 <= <IntegerPart>.length <= 4
0 <= <NonRepeatingPart>.length <= 4
1 <= <RepeatingPart>.length <= 4
給定兩個字符串 S
和 T
,每一個字符串表明一個非負有理數,只有當它們表示相同的數字時才返回 true;不然,返回 false。字符串中能夠使用括號來表示有理數的重複部分。
一般,有理數最多能夠用三個部分來表示:整數部分 <IntegerPart>
、小數非重複部分 <NonRepeatingPart>
和小數重複部分 <(><RepeatingPart><)>
。數字能夠用如下三種方法之一來表示:
<IntegerPart>
(例:0,12,123)<IntegerPart><.><NonRepeatingPart>
(例:0.5,2.12,2.0001)<IntegerPart><.><NonRepeatingPart><(><RepeatingPart><)>
(例:0.1(6),0.9(9),0.00(1212))十進制展開的重複部分一般在一對圓括號內表示。例如:
1 / 6 = 0.16666666... = 0.1(6) = 0.1666(6) = 0.166(66)
0.1(6) 或 0.1666(6) 或 0.166(66) 都是 1 / 6 的正確表示形式。
示例 1:
輸入:S = "0.(52)", T = "0.5(25)" 輸出:true 解釋:由於 "0.(52)" 表明 0.52525252...,而 "0.5(25)" 表明 0.52525252525.....,則這兩個字符串表示相同的數字。
示例 2:
輸入:S = "0.1666(6)", T = "0.166(66)" 輸出:true
示例 3:
輸入:S = "0.9(9)", T = "1." 輸出:true 解釋: "0.9(9)" 表明 0.999999999... 永遠重複,等於 1 。[有關說明,請參閱此連接] "1." 表示數字 1,其格式正確:(IntegerPart) = "1" 且 (NonRepeatingPart) = "" 。
提示:
<IntegerPart>
不會以 2 個或更多的零開頭。(對每一個部分的數字沒有其餘限制)。1 <= <IntegerPart>.length <= 4
0 <= <NonRepeatingPart>.length <= 4
1 <= <RepeatingPart>.length <= 4
8ms
1 class Solution { 2 func isRationalEqual(_ S: String, _ T: String) -> Bool { 3 return abs(convertToDouble(S) - convertToDouble(T)) < 1e-8 4 } 5 6 func convertToDouble(_ S: String) -> Double 7 { 8 var index = S.firstIndex(of: "(") ?? S.endIndex 9 if index == S.endIndex 10 { 11 return Double(S)! 12 } 13 else 14 { 15 var sb:String = String(S[..<index]) 16 let rightIndex = S.firstIndex(of: ")") ?? S.startIndex 17 if rightIndex != S.startIndex 18 { 19 index = S.index(after: index) 20 var rep:String = String(S[index..<rightIndex]) 21 while(sb.count < 50) 22 { 23 sb += rep 24 } 25 } 26 return Double(sb)! 27 } 28 } 29 }
12ms
1 class Solution { 2 func isRationalEqual(_ S: String, _ T: String) -> Bool { 3 if abs(parse(str: S) - parse(str: T)) < 0.0000001 { 4 return true 5 } 6 return false 7 } 8 func parse(str: String) -> Double { 9 if let dotIndex = str.firstIndex(of: "(") { 10 let ddIndex = str.firstIndex(of: ".") 11 var ans: Double = 0 12 ans += Double(String(str[str.startIndex..<dotIndex]))! 13 var dotStr = String(str[dotIndex..<str.endIndex]).dropFirst().dropLast() 14 let nn = dotIndex.encodedOffset - ddIndex!.encodedOffset + dotStr.count - 1 15 for i in 0...10 { 16 ans += Double(dotStr)! * pow(10.0, -Double(nn+i*(dotStr.count))) 17 } 18 return ans 19 } else { 20 return Double(str)! 21 } 22 } 23 }