在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a Example 1: Input: text = [[3,7],[9,13],[10,17]]
Example 2: Input: text = [[0,1],[0,2],[2,3],[2,4]]
Explanation:
Notice that matches can overlap, see "aba" is found in [0,2] and [2,4].
Note:
给出 字符串 text 和 字符串列表 words, 返回所有的索引对 [i, j] 使得在索引对范围内的子字符串 text[i]...text[j](包括 i 和 j)属于字符串列表 words。 输入: text = "thestoryofleetcodeandme", words = ["story","fleet","leetcode"] 输入: text = "ababa", words = ["aba","ab"] 1.所有字符串都只包含小写字母。 Runtime: 92 ms Memory Usage: 21.4 MB 1 class Solution { 2 func indexPairs(_ text: String, _ words: [String]) -> [[Int]] { 3 var ret:[[Int]] = [[Int]]() 4 let arrText:[Character] = Array(text) 5 for word in words 6 { 7 let arrStr:[Character] = Array(word) 8 let num:Int = arrStr.count 9 for i in 0..<arrText.count 10 { 11 if (arrText[i] == arrStr.first!) && (i + num <= arrText.count) && (Array(arrText[i..<(i + num)]) == arrStr) 12 { 13 ret.append([i,i + num - 1]) 14 } 15 } 16 } 17 ret.sort(){ 18 if $0[0] == $1[0] 19 { 20 return $0[1] <= $1[1] 21 } 22 else 23 { 24 return $0[0] <= $1[0] 25 } 26 } 27 return ret 28 } 29 }
|
请发表评论