[Swift]LeetCode917. 僅僅反轉字母 | Reverse Only Letters

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-acbpbbst-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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.git

 

Example 1:github

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,返回 「反轉後的」 字符串,其中不是字母的字符都保留在原地,而全部字母的位置發生反轉。app

示例 1:oop

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

示例 2:spa

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

示例 3:code

輸入:"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 }

12mshtm

 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 }
相關文章
相關標籤/搜索