1. 程式人生 > >[Swift]LeetCode187. 重複的DNA序列 | Repeated DNA Sequences

[Swift]LeetCode187. 重複的DNA序列 | Repeated DNA Sequences

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.

Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.

Example:

Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"

Output: ["AAAAACCCCC", "CCCCCAAAAA"]

所有 DNA 由一系列縮寫為 A,C,G 和 T 的核苷酸組成,例如:“ACGAATTCCG”。在研究 DNA 時,識別 DNA 中的重複序列有時會對研究非常有幫助。

編寫一個函式來查詢 DNA 分子中所有出現超多一次的10個字母長的序列(子串)。

示例:

輸入: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"

輸出: ["AAAAACCCCC", "CCCCCAAAAA"]

超出時間限制
 1 class Solution {
 2     func findRepeatedDnaSequences(_ s: String) -> [String] {
 3         if s.count < 9 {return []}
 4         var res:Set<String> = Set<String>()
 5         var st:Set<String> = Set<String>()
 6         for i in 0..<(s.count - 9
) 7 { 8 var t:String = s.subString(i,10) 9 if st.contains(t) 10 { 11 res.insert(t) 12 } 13 else 14 { 15 st.insert(t) 16 } 17 } 18 //Set轉陣列[String] 19 return Array(res) 20 } 21 } 22 23 extension String { 24 // 擷取字串:指定索引和字元數 25 // - begin: 開始擷取處索引 26 // - count: 擷取的字元數量 27 func subString(_ begin:Int,_ count:Int) -> String { 28 let start = self.index(self.startIndex, offsetBy: max(0, begin)) 29 let end = self.index(self.startIndex, offsetBy: min(self.count, begin + count)) 30 return String(self[start..<end]) 31 } 32 33 }

超出時間限制
 1 class Solution {
 2     func findRepeatedDnaSequences(_ s: String) -> [String] {
 3         if s.count < 9 {return []}
 4         var res:Set<String> = Set<String>()
 5         var st:Set<String> = Set<String>()
 6         var cur:Int = 0
 7         for i in 0..<9
 8         {
 9             cur = cur << 3 | (s[i].ascii & 7)
10         }
11         
12         for i in 9..<s.count
13         {
14             cur = ((cur & 0x7ffffff) << 3) | (s[i].ascii & 7)
15             var t:String = s.subString(i - 9, 10)
16             if st.contains(t)
17             {
18                 res.insert(t)
19             }
20             else
21             {
22                 st.insert(t)
23             }
24         }
25         
26         //Set轉陣列[String]
27         return Array(res)
28     }
29 }
30 
31 extension String {
32     //subscript函式可以檢索陣列中的值
33     //直接按照索引方式擷取指定索引的字元
34     subscript (_ i: Int) -> Character {
35         //讀取字元
36         get {return self[index(startIndex, offsetBy: i)]}
37     }
38     
39     // 擷取字串:指定索引和字元數
40     // - begin: 開始擷取處索引
41     // - count: 擷取的字元數量
42     func subString(_ begin:Int,_ count:Int) -> String {
43         let start = self.index(self.startIndex, offsetBy: max(0, begin))
44         let end = self.index(self.startIndex, offsetBy:  min(self.count, begin + count))
45         return String(self[start..<end]) 
46     }
47     
48 }
49 
50 //Character擴充套件方法  
51 extension Character  
52 {  
53   //屬性:ASCII整數值(定義小寫為整數值)
54    var ascii: Int {
55         get {
56             let s = String(self).unicodeScalars
57             return Int(s[s.startIndex].value)
58         }
59     }
60 }