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

[Swift]LeetCode917. 仅仅反转字母 | Reverse Only Letters

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

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

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

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

Given a string S, return the "reversed" string where all characters that are not a letter stay in the same place, and all letters reverse their positions.

 

Example 1:

Input: "ab-cd"
Output: "dc-ba"

Example 2:

Input: "a-bC-dEf-ghIj"
Output: "j-Ih-gfE-dCba"

Example 3:

Input: "Test1ng-Leet=code-Q!"
Output: "Qedo1ct-eeLg=ntse-T!"

给定一个字符串 S,返回 “反转后的” 字符串,其中不是字母的字符都保留在原地,而所有字母的位置发生反转。

示例 1:

输入:"ab-cd"
输出:"dc-ba"

示例 2:

输入:"a-bC-dEf-ghIj"
输出:"j-Ih-gfE-dCba"

示例 3:

输入:"Test1ng-Leet=code-Q!"
输出:"Qedo1ct-eeLg=ntse-T!"

40ms
 1 class Solution {
 2     func reverseOnlyLetters(_ S: String) -> String {
 3         //空字符串情况
 4         guard S != nil else {
 5             return S
 6         }
 7         //创建字典记录非字母
 8         var chars:[Int:Character] = [Int:Character]()
 9         //存储顺序字母
10         var temp:String = String()
11         //按索引遍历字符串
12         for i in 0..<S.count
13         {
14             //获取字符
15             let letter:Character = S[S.index(S.startIndex, offsetBy: i)]
16             //判断字母
17             if checkLetter(letter)
18             {
19                 //存储字母
20                 temp.append(letter)
21             }
22             else
23             {
24                 //记录非字母
25                 chars[i] = letter
26             }
27         }
28         //字符串整体反转
29         var result:String = String(temp.reversed())
30         //创建等长度字符串数组
31         var resultArray = Array(repeating:"", count: S.count)
32         //判断记录是否为空
33         if chars.isEmpty
34         {
35             return result
36         }
37         else
38         {
39             //遍历非字母
40             for key in chars.keys
41             {
42                 //填入非字母
43                 resultArray[key] = String(chars[key]!)
44             }
45             //遍历逆序字母
46             for char in result
47             {
48                 //遍历数组
49                 for j in 0..<resultArray.count
50                 {
51                     //判断是否包含非字母
52                     if String(resultArray[j]) == ""
53                     {
54                         //将逆序字母依次填入数组
55                         resultArray[j] = String(char)
56                         break
57                     }
58                 }
59             }
60             //将数组转化为字符串
61             result  = resultArray.joined(separator: "")
62         }
63         return result
64     }
65     //判断字母
66     func checkLetter(_ char:Character)->Bool
67     {
68         var num:UInt32 = UInt32()
69         for asc in char.unicodeScalars
70         {
71             num = asc.value
72         }
73         //65~90:A-Z
74         //97~122:a-z
75         if (num > 64 && num < 91) || (num > 96 && num < 123)
76         {
77             return true
78         }
79         else
80         {
81             return false
82         }
83     }
84 }

8ms
 1 class Solution {
 2     func isAlpha(_ c: UInt8) -> Bool {
 3       return (97...122 ~= c) || (65...90 ~= c)
 4     }
 5     func reverseOnlyLetters(_ S: String) -> String {
 6       var sarr = Array(S.utf8)
 7       var b = 0, e = sarr.count-1
 8       while b < e {
 9         if !isAlpha(sarr[b]) {
10           b += 1
11         } else if !isAlpha(sarr[e]) {
12           e -= 1
13         } else {
14           let t = sarr[b]
15           sarr[b] = sarr[e]
16           sarr[e] = t
17           b += 1
18           e -= 1
19         }
20       }
21       return String(bytes: sarr, encoding: .utf8)!
22     }
23 }

12ms

 1 class Solution {
 2     func reverseOnlyLetters(_ S: String) -> String {
 3         var array :[Character] = Array(S)
 4         var startIndex = 0
 5         var endIndex = S.count - 1
 6         
 7         while endIndex > 0 && startIndex < S.count - 1 {
 8             while isLetter(c: array[startIndex]) == false && startIndex < S.count - 1 {
 9                 startIndex += 1
10             }
11             
12             while isLetter(c: array[endIndex]) == false && endIndex > 0 {
13                 endIndex -= 1
14             }
15             
16             if startIndex > S.count - 1
17                 || endIndex <= 0 
18                 || startIndex >= endIndex {
19                 break
20             }
21             let tmp = array[endIndex]
22             array[endIndex] = array[startIndex] 
23             array[startIndex] = tmp
24             startIndex += 1
25             endIndex -= 1
26         }
27         return String(array)
28     }
29     
30     func isLetter(c : Character) -> Bool {
31         return  (c >= "a" && c <= "z") || 
32         (c >= "A" && c <= "Z")
33     }
34 }

16ms

 1 class Solution {
 2      func reverseOnlyLetters(_ s: String) -> String {
 3         var arr = s.map({ String($0) })
 4         var arrCopy = arr
 5         var lastCount: Int = arr.count - 1
 6         for (index, string) in arr.enumerated() {
 7             //All elements looped
 8             guard lastCount >= index else {
 9                 break
10             }
11             //Not special character
12             guard string.lowercased() != string || string.uppercased() != string else {
13                 continue
14             }
15             while arr[lastCount].lowercased() == arr[lastCount],
16                 arr[lastCount].uppercased() == arr[lastCount] {
17                 lastCount -= 1
18             }
19             arrCopy.swapAt(index, lastCount)
20             lastCount -= 1
21         }
22         return arrCopy.joined()
23     }
24 }

20ms

 1 class Solution {
 2     func reverseOnlyLetters(_ S: String) -> String {
 3         var array :[Character] = Array(S)
 4         var startIndex = 0
 5         var endIndex = S.count - 1
 6         
 7         while endIndex > 0 && startIndex < S.count - 1 {
 8             while isLetter(c: array[startIndex]) == false && startIndex < S.count - 1 {
 9                 startIndex += 1
10             }
11             
12             // if startIndex > S.count - 1 {
13             //     break
14             // }
15             
16             while isLetter(c: array[endIndex]) == false && endIndex > 0 {
17                 endIndex -= 1
18             }
19             
20             if startIndex > S.count - 1
21                 || endIndex <= 0 
22                 || startIndex >= endIndex 
23             {
24                 break
25             }
26             
27             // if startIndex >= endIndex {
28             //     break
29             // }
30             
31             let tmp = array[endIndex]
32             array[endIndex] = array[startIndex] 
33             array[startIndex] = tmp
34             print(String(array))
35             
36             startIndex += 1
37             endIndex -= 1
38         }
39         
40         return String(array)
41     }
42     
43     func isLetter(c : Character) -> Bool {
44         return  (c >= "a" && c <= "z") || 
45         (c >= "A" && c <= "Z")
46     }
47 }

28ms

 1 class Solution {
 2     func reverseOnlyLetters(_ S: String) -> String {
 3         var array :[Character] = Array(S)
 4         var startIndex = 0
 5         var endIndex = S.count - 1
 6         
 7         while endIndex > 0 && startIndex < S.count - 1 {
 8             while isLetter(c: array[startIndex]) == false && startIndex < S.count - 1 {
 9                 startIndex += 1
10             }
11             
12             if startIndex > S.count - 1 {
13                 break
14             }
15             
16             while isLetter(c: array[endIndex]) == false && endIndex > 0 {
17                 endIndex -= 1
18             }
19             
20             
21             if endIndex <= 0 {
22                 break
23             }
24             
25             if startIndex >= endIndex {
26                 break
27             }
28             print("#####")
29             print(startIndex)
30             print(endIndex)
31             
32             let tmp = array[endIndex]
33             array[endIndex] = array[startIndex] 
34             array[startIndex] = tmp
35             print(String(array))
36             
37             startIndex += 1
38             endIndex -= 1
39         }
40         
41         return String(array)
42     }
43     
44     func isLetter(c : Character) -> Bool {
45         return  (c >= "a" && c <= "z") || 
46         (c >= "A" && c <= "Z")
47     }
48 }

32ms

 1 class Solution {
 2     func reverseOnlyLetters(_ S: String) -> String {
 3         
 4         var letters = [Character]()
 5         var alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
 6         
 7         var a = S.filter{alphabet.contains($0) == true}
 8         var ab = Array(a.reversed())
 9         var j = 0
10         
11         for (i, ch) in S.enumerated() {
12             if !alphabet.contains(ch) {
13                 letters.append(ch)
14             } else {
15                 letters.append(ab[j])
16                 j += 1
17             }
18         }        
19 
20         return String(letters)
21     }
22 }

36ms

 

 1 class Solution {
 2     func reverseOnlyLetters(_ S: String) -> String {
 3         
 4         var letters = [Character]()
 5         var alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
 6         
 7         var a = S.filter{alphabet.contains($0) == true}
 8         var ab = Array(a.reversed())
 9         var j = 0
10         
11         for (i, ch) in S.enumerated() {
12             if !alphabet.contains(ch) {
13                 letters.append(ch)
14             } else {
15                 letters.append(ab[j])
16                 j += 1
17             }
18         }
19         
20         print("ab: ", ab)
21         
22 
23         return String(letters)
24     }
25 }

 

posted @ 2018-10-07 18:23  为敢技术  阅读(728)  评论(0编辑  收藏  举报