首页 > 编程语言 > 详细

[Swift]LeetCode345. 反转字符串中的元音字母 | Reverse Vowels of a String

时间:2019-01-13 11:33:51      阅读:36      评论:0      收藏:0      [点我收藏+]

标签:pan   col   UNC   def   eve   break   clas   cstring   inpu   

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:

Input: "hello"
Output: "holle"

Example 2:

Input: "leetcode"
Output: "leotcede"

Note:
The vowels does not include the letter "y".


编写一个函数,以字符串作为输入,反转该字符串中的元音字母。

示例 1:

输入: "hello"
输出: "holle"

示例 2:

输入: "leetcode"
输出: "leotcede"

说明:
元音字母不包含字母"y"。


84ms

 1 class Solution {
 2     func reverseVowels(_ s: String) -> String {
 3         var a = Array(s)
 4         var i = 0
 5         var j = a.count - 1
 6         
 7         while i < j {
 8             if !isVowel(a[i]) {
 9                 i += 1
10             } else if !isVowel(a[j]) {
11                 j -= 1
12             } else {
13                 (a[i], a[j]) = (a[j], a[i])
14                 i += 1
15                 j -= 1
16             }
17         }
18         
19         return String(a)
20     }
21     
22     func isVowel(_ c: Character) -> Bool {
23         switch c {
24         case "a","e","i","o","u","A","E","I","O","U":
25             return true
26         default:
27             return false
28         }
29     }
30 }

120ms

 1 class Solution {
 2     let vowels: Set<Character> = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
 3     func reverseVowels(_ s: String) -> String {
 4         if s.count < 2 { return s }
 5         var chars = Array(s)
 6         var start = 0
 7         var end = chars.count - 1
 8         while start < end {
 9             while start < end && !vowels.contains(chars[start]) {
10                 start += 1
11             }
12             
13             while end > start && !vowels.contains(chars[end]) {
14                 end -= 1
15             }
16             if start != end {
17                 chars.swapAt(start, end)
18                 start += 1
19                 end -= 1
20             }
21         }
22         return String(chars)
23     }
24 }

144ms

 1 class Solution {
 2     func reverseVowels(_ s: String) -> String {
 3         let set: Set<Character> = [Character("a"), Character("e"), Character("i"), Character("o"), Character("u"),Character("A"), Character("E"), Character("I"), Character("O"), Character("U")]
 4         var chars = Array(s)
 5         var left = 0
 6         var right = s.count - 1
 7         while left < right {
 8             if set.contains(chars[left]) && set.contains(chars[right]) {
 9                 chars.swapAt(left, right)
10                 left += 1
11                 right -= 1
12             } else if set.contains(chars[left]) {
13                 right -= 1
14             } else {
15                 left += 1
16             }
17         }
18 
19         return String(chars)
20     }
21 }

152ms

 1 class Solution {
 2     func reverseVowels(_ s: String) -> String {
 3         if s.count == 0 {
 4            return ""
 5         }
 6         
 7         var chas = s.cString(using:.ascii)!
 8         
 9         var left = 0
10         var right = s.count - 1
11         
12         var loop = true
13         
14         while loop {
15             if left > right {
16                 loop = false
17                 break
18             } 
19             
20             let leftChar = chas[left]
21             
22             if !checkIsYuanChar(leftChar) {
23                 left = left + 1
24                 continue
25             }
26             
27             let rightChar = chas[right] 
28             
29             if !checkIsYuanChar(rightChar) {
30                 right = right - 1
31                 continue
32             }
33             
34             chas[left] = rightChar
35             chas[right] = leftChar
36             
37             left = left + 1
38             right = right - 1
39         }
40         
41         var newStr = String.init(cString:chas)
42         
43         return newStr
44     }
45     
46 
47     
48     func checkIsYuanChar(_ c: CChar) -> Bool {
49         if c == 65 || c == 97 {
50             return true
51         }
52         if c == 69 || c == 101 {
53             return true
54         }
55         if c == 73 || c == 105 {
56             return true
57         }
58         if c == 79 || c == 111 {
59             return true
60         }
61         if c == 85 || c == 117 {
62             return true
63         }
64         
65         return false
66     
67     }
68 }

192ms

 1 class Solution {
 2     func reverseVowels(_ s: String) -> String {
 3         // a e i o u
 4         let set: Set<Character> = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
 5         var indexArray = [Int]()
 6         var valueArray = [Character]()
 7         var array = [Character](s)
 8         for (index, c) in array.enumerated() {
 9             if set.contains(c) {
10                 indexArray.append(index)
11                 valueArray.append(c)
12             }
13         }
14         let reversed = Array(valueArray.reversed())
15         for i in 0 ..< indexArray.count {
16             array[indexArray[i]] = reversed[i]
17         }
18         return String(array)
19     }
20 }

260ms

 1 class Solution {
 2     func reverseVowels(_ s: String) -> String {
 3         let yuanyin:[Character] = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
 4         
 5         var arr = Array(s)
 6         
 7         var left = 0
 8         var right = s.count - 1
 9         while left < right {
10             
11             while !yuanyin.contains(arr[left]) && left < right {
12                 left += 1
13             }
14             while !yuanyin.contains(arr[right]) && left < right {
15                 right -= 1
16             }
17             
18             (arr[left], arr[right]) = (arr[right], arr[left])
19             left += 1
20             right -= 1
21         }
22         
23         return String(arr)
24     }
25 }

428ms

 1 class Solution {
 2     func reverseVowels(_ s: String) -> String {
 3         var sArr = Array(s)
 4         var vowels = [Character]()
 5         let v = "aeiouAEIOU"
 6         
 7         for c in sArr where v.contains(c) {
 8             vowels.append(c)
 9         }
10         
11         for i in 0..<sArr.count  {
12             let c = sArr[i]
13             if v.contains(c) {
14                 sArr[i] = vowels.removeLast()
15             }
16         }
17         
18         return String(sArr)
19     }
20 }

 

[Swift]LeetCode345. 反转字符串中的元音字母 | Reverse Vowels of a String

标签:pan   col   UNC   def   eve   break   clas   cstring   inpu   

原文:https://www.cnblogs.com/strengthen/p/10262100.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号