[Swift]LeetCode522. 最长特殊序列 II | Longest Uncommon Subsequence II
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號:山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:https://www.cnblogs.com/strengthen/p/10397495.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強烈建議點擊原文地址閱讀!支持作者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be?any?subsequence of the other strings.
A?subsequence?is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.
The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1.
Example 1:
Input: "aba", "cdc", "eae" Output: 3?Note:
給定字符串列表,你需要從它們中找出最長的特殊序列。最長特殊序列定義如下:該序列為某字符串獨有的最長子序列(即不能是其他字符串的子序列)。
子序列可以通過刪去字符串中的某些字符實現,但不能改變剩余字符的相對順序??招蛄袨樗凶址淖有蛄?#xff0c;任何字符串為其自身的子序列。
輸入將是一個字符串列表,輸出是最長特殊序列的長度。如果最長特殊序列不存在,返回 -1 。?
示例:
輸入: "aba", "cdc", "eae" 輸出: 3?提示:
Runtime:?16 ms Memory Usage:?19.8 MB 1 class Solution { 2 func findLUSlength(_ strs: [String]) -> Int { 3 var strs = strs 4 var n:Int = strs.count 5 var s:Set<String> = Set<String>() 6 strs.sort(by:{ 7 if $0.count == $1.count 8 { 9 return $0 > $1 10 } 11 return $0.count > $1.count 12 }) 13 for i in 0..<n 14 { 15 if i == n - 1 || strs[i] != strs[i + 1] 16 { 17 var found:Bool = true 18 for a in s 19 { 20 var j:Int = 0 21 for c in a.characters 22 { 23 if c == strs[i][j] {j += 1} 24 if j == strs[i].count {break} 25 } 26 if j == strs[i].count 27 { 28 found = false 29 break 30 } 31 } 32 if found {return strs[i].count} 33 } 34 s.insert(strs[i]) 35 } 36 return -1 37 } 38 } 39 40 extension String { 41 //subscript函數可以檢索數組中的值 42 //直接按照索引方式截取指定索引的字符 43 subscript (_ i: Int) -> Character { 44 //讀取字符 45 get {return self[index(startIndex, offsetBy: i)]} 46 } 47 }
16ms
1 class Solution { 2 func findLUSlength(_ strs: [String]) -> Int { 3 var sortStrs = strs.sorted { 4 return $1.count < $0.count || ($1.count == $0.count && $1 < $0 ) 5 } 6 let duplicates = getDuplicates(sortStrs) 7 for i in sortStrs.indices { 8 if !duplicates.contains(sortStrs[i]) { 9 if i == 0 { return sortStrs[i].count } 10 for j in 0..<i { 11 if isSubsequence(Array(sortStrs[j]), Array(sortStrs[i])) { 12 break 13 } 14 if j == i-1 { 15 return sortStrs[i].count 16 } 17 } 18 } 19 } 20 return -1 21 } 22 23 func isSubsequence(_ chars1: [Character], _ chars2: [Character]) -> Bool { 24 var i = 0, j = 0 25 while i<chars1.count && j<chars2.count { 26 if chars1[i] == chars2[j] { 27 j += 1 28 } 29 i += 1 30 } 31 return j == chars2.count 32 } 33 34 func getDuplicates(_ strs: [String]) -> Set<String> { 35 var set = Set<String>() 36 var duplicates = Set<String>() 37 for s in strs { 38 if set.contains(s) { 39 duplicates.insert(s) 40 }else { 41 set.insert(s) 42 } 43 } 44 return duplicates 45 } 46 }?
轉載于:https://www.cnblogs.com/strengthen/p/10397495.html
總結
以上是生活随笔為你收集整理的[Swift]LeetCode522. 最长特殊序列 II | Longest Uncommon Subsequence II的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 论抗日战争胜利的原因,作为新时代青年,我
- 下一篇: js 鼠标事件详细