日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Swift]LeetCode522. 最长特殊序列 II | Longest Uncommon Subsequence II

發布時間:2025/5/22 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [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:

  • All the given strings' lengths will not exceed 10.
  • The length of the given list will be in the range of [2, 50].

  • 給定字符串列表,你需要從它們中找出最長的特殊序列。最長特殊序列定義如下:該序列為某字符串獨有的最長子序列(即不能是其他字符串的子序列)。

    子序列可以通過刪去字符串中的某些字符實現,但不能改變剩余字符的相對順序??招蛄袨樗凶址淖有蛄?#xff0c;任何字符串為其自身的子序列。

    輸入將是一個字符串列表,輸出是最長特殊序列的長度。如果最長特殊序列不存在,返回 -1 。?

    示例:

    輸入: "aba", "cdc", "eae" 輸出: 3?

    提示:

  • 所有給定的字符串長度不會超過 10 。
  • 給定字符串列表的長度將在 [2, 50 ] 之間。

  • 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的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。