,敢教日月换新天。为有牺牲多壮志

[Swift]LeetCode316. 去除重复字母 | Remove Duplicate Letters

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10260578.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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:

"bcabc"
"abc"

Example 2:

"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] -= 1
15             if used[ch, default: false] == true {
16                 continue
17             }                      
18             
19             while let last = ans.last, ch < last, counts[last]! > 0 {
20                 used[last] = false
21                 ans.removeLast()           
22             }
23             ans.append(ch)
24             used[ch] = true
25         }
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 ans
102     }
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] = 1
10             }else {
11                 counts[c]! += 1
12             }
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]! -= 1
26         }
27         
28         return String(res)
29     }
30 }
复制代码

 

posted @   为敢技术  阅读(386)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°