Given a string which contains only lowercase letters,remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.
Example 1:
input: Output: "bcabc""abc"
Example 2:
input: Output: "cbacdcbc""acdb"
给定一个仅包含小写字母的字符串,去除字符串中重复的字母,使得每个字母只出现一次。需保证返回结果的字典序最小(要求不能打乱其他字符的相对位置)。
示例 1:
输入: 输出: "bcabc""abc"
示例 2:
输入: 输出: "cbacdcbc""acdb"
32ms
1 class Solution { 2 func removeDuplicateLetters(_ s: String) -> String { 3 4 var counts = [Character: Int]() 5 var used = [Character:Bool]() 6 7 s.forEach { 8 counts[$0,default: 0] += 1 9 }10 11 var ans = [Character]()12 13 for ch in s {14 counts[ch,default:0] -= 115 if used[ch,default: false] == true {16 continue17 } 18 19 while let last = ans.last,ch < last,counts[last]! > 0 {20 used[last] = false21 ans.removeLast() 22 }23 ans.append(ch)24 used[ch] = true25 }26 27 return String(ans)28 }29 }
36ms
1 extension Character { 2 var ascii: UInt32? { 3 guard let first = unicodeScalars.first else { 4 return nil 5 } 6 return first.isASCII == true ? first.value : nil 7 } 8 } 9 10 class Solution { 11 class IndexedTree { 12 var n: Int 13 var arr: [Int] 14 15 init(_ n: Int) { 16 self.n = n 17 arr = [Int](repeating: 0,count: n+1) 18 } 19 20 func insert(_ index: Int) -> VoID { 21 var x = index 22 while x <= n { 23 arr[x] += 1 24 x = x + x&(-x) 25 } 26 } 27 28 func query(_ index: Int) -> Int { 29 var x = index 30 var sum = 0 31 while x > 0 { 32 sum += arr[x] 33 x = x&(x-1) 34 } 35 return sum 36 } 37 } 38 39 40 func transIndex(_ n: Int,_ index: Int) -> Int { 41 return n-index 42 } 43 44 func removeDuplicateLetters(_ s: String) -> String { 45 let chararr = Array(s) 46 var ans = "" 47 48 let n = chararr.count 49 if n == 0 { 50 return ans 51 } 52 53 var counts = [Int](repeating: 0,count: 26) 54 var indice = [Int?](repeating: nil,count: 26) 55 var used = [Bool](repeating: false,count: 26) 56 57 let indexedTree = IndexedTree(chararr.count) 58 let aAsc = "a".first!.ascii! 59 60 for (i,ch) in chararr.enumerated(){ 61 let ci = Int(ch.ascii!-aAsc) 62 counts[ci] += 1 63 } 64 65 var que = [Int]() 66 for (i,ch) in chararr.enumerated() { 67 let ci = Int(ch.ascii!-aAsc) 68 if counts[ci] == 0 { 69 continue 70 } 71 72 73 74 if indice[ci] == nil { 75 while let last = que.last { 76 if last > ci { 77 que.removeLast() 78 indice[last] = nil 79 } else { 80 break 81 } 82 } 83 84 indice[ci] = i 85 que.append(ci) 86 } 87 88 counts[ci] -= 1 89 if counts[ci] == 0 { 90 while let ii = que.first,ii <= ci { 91 let index = indice[ii]! 92 ans += String(chararr[index]) 93 indice[ii] = nil 94 counts[ii] = 0 95 que.removeFirst() 96 } 97 } 98 99 }100 101 return ans102 }103 }
52ms
1 class Solution { 2 func removeDuplicateLetters(_ s: String) -> String { 3 let sArr = Array(s) 4 5 var counts = [Character : Int]() 6 7 for c in sArr { 8 if counts[c] == nil { 9 counts[c] = 110 }else {11 counts[c]! += 112 }13 }14 15 var res = [Character]()16 17 for c in sArr {18 if !res.contains(c) {19 while !res.isEmpty && counts[res.last!] != 0 && res.last! > c {20 res.removeLast()21 }22 res.append(c)23 24 }25 counts[c]! -= 126 }27 28 return String(res)29 }30 }总结
以上是内存溢出为你收集整理的[Swift]LeetCode316. 去除重复字母 | Remove Duplicate Letters全部内容,希望文章能够帮你解决[Swift]LeetCode316. 去除重复字母 | Remove Duplicate Letters所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)