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

[Swift]LeetCode541. 反转字符串 II | Reverse String II

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

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

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

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

Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.

 Example:

Input: s = "abcdefg", k = 2
Output: "bacdfeg"

 Restrictions:

  1. The string consists of lower English letters only.
  2. Length of the given string and k will in the range [1, 10000]

给定一个字符串和一个整数 k,你需要对从字符串开头算起的每个 2k 个字符的前k个字符进行反转。如果剩余少于 k 个字符,则将剩余的所有全部反转。如果有小于 2k 但大于或等于 k 个字符,则反转前 k 个字符,并将剩余的字符保持原样。

示例:

输入: s = "abcdefg", k = 2
输出: "bacdfeg"

要求:

  1. 该字符串只包含小写的英文字母。
  2. 给定字符串的长度和 k 在[1, 10000]范围内。

308ms

 1 class Solution {
 2     func reverseStr(_ s: String, _ k: Int) -> String {
 3         var arr:[String] = [String]()
 4         //将字符串的字符存入数组中
 5         for char in s.characters
 6         {
 7             arr.append(String(char))
 8         }
 9         for i in stride(from: 0,to: s.count ,by: 2 * k)
10         {
11             var num:Int = min(i + k - 1,s.count - 1)
12             for (index, value) in arr[i...num].reversed().enumerated()
13             {
14                 arr[i + index] = value
15             }
16         }
17         //数组转换为字符串
18         return arr.reduce("", {$0 + $1})
19     }
20 }

32ms

 1 class Solution {
 2     func reverseStr(_ s: String, _ k: Int) -> String {
 3         var arr = Array(s.characters)
 4         var index = 0
 5         
 6         while index < arr.count {
 7             reverse(&arr, index, min(index + k - 1, arr.count - 1))
 8             index += 2 * k
 9         }
10         return String(arr)
11     }
12     
13     func reverse(_ arr: inout [Character], _ startIndex: Int, _ endIndex: Int) {
14         var start = startIndex
15         var end = endIndex
16         while start <= (startIndex + endIndex) / 2 {
17             (arr[start], arr[end]) = (arr[end], arr[start])
18             start += 1
19             end -= 1
20         }
21     }
22 }

36ms

 1 class Solution {
 2     func reverseStr(_ s: String, _ k: Int) -> String {
 3         if k == 0 {
 4             return s
 5         }
 6         let count = s.count
 7         let time = count/(2*k)
 8         let left = count%(2*k)
 9         
10         var array = [String]()
11         for i in 0..<time {
12             array.append(String(s.suffix(count-2*k*i).prefix(k*2)))
13         }
14         
15         var result = ""
16         for str in array {
17             result += String(str.prefix(k).reversed()+str.suffix(k))
18         }
19         
20         let leftStr = s.suffix(left)
21         let leftReversed = leftStr.prefix(k).reversed()
22         let leftUnreversed = leftStr.suffix(leftStr.count-leftReversed.count)
23         
24         result += leftReversed+leftUnreversed
25         return result
26     }
27 }

40ms

 1 class Solution {
 2     func reverseStr(_ s: String, _ k: Int) -> String {
 3         var chars = s.map { $0 }
 4         
 5         var x = 0
 6         while x < chars.count {
 7             let rem = chars.count - x
 8             if rem >= 2*k {
 9                 var curr = x
10                 var max = x + k - 1
11                 for _ in 0..<k/2 {
12                     chars.swapAt(curr, max)
13                     curr += 1
14                     max -= 1
15                 }
16                 x += 2*k
17             } else if rem < k {
18                 var curr = x
19                 var max = chars.count-1
20                 for _ in 0..<rem/2 {
21                     chars.swapAt(curr, max)
22                     curr += 1
23                     max -= 1
24                 }
25                 break
26             } else {
27                 var curr = x
28                 var max = x + k - 1
29                 for _ in 0..<k/2 {
30                     chars.swapAt(curr, max)
31                     curr += 1
32                     max -= 1
33                 }
34                 break
35             }
36         }
37         
38         return String(chars)
39     }
40 }

80ms

 1 class Solution {
 2     func reverseStr(_ s: String, _ k: Int) -> String {
 3         if s.count <= k { return String(s.reversed())}
 4         var array = s.map{ String($0) }
 5         var start = 0
 6         var i = 0
 7         while i < array.count {
 8             reverse(in: &array, from:i, to:i+k)
 9             i += 2*k
10         }
11         return array.reduce("", +)
12     }
13     
14     func reverse(in array: inout [String], from: Int, to: Int) {
15         var start = from
16         var end = min(to, array.count) - 1
17         while start < end {
18             let tmp = array[start]
19             array[start] = array[end]
20             array[end] = tmp
21             
22             start += 1
23             end -= 1
24         }
25     }
26 }

84ms

 1 class Solution {
 2     func reverseStr(_ s: String, _ k: Int) -> String {
 3         var array = s.map{ String($0) }
 4         var start = 0
 5         var i = 0
 6         while i < array.count {
 7             reverse(in: &array, from:i, to:i+k)
 8             i += 2*k
 9         }
10         return array.reduce("", +)
11     }
12     
13     func reverse(in array: inout [String], from: Int, to: Int) {
14         var start = from
15         var end = min(to, array.count) - 1
16         while start < end {
17             let tmp = array[start]
18             array[start] = array[end]
19             array[end] = tmp
20             
21             start += 1
22             end -= 1
23         }
24     }
25 }

92ms

 1 class Solution {
 2     func reverseStr(_ s: String, _ k: Int) -> String {
 3 
 4         var result = [String]()
 5         var array = s.map{ String($0) }
 6         var start = 0
 7         var i = 0
 8         while i < array.count {
 9             if i == start {
10                 var end = start+k >= array.count ? array.count : start+k
11                 var slice = array[start...end-1]
12                 result += Array(slice.reversed())
13                 start+=2*k
14             } else {
15                 result.append(contentsOf: array[i ..< min(i+k, array.count)])
16             }
17             i += k
18         }
19         return result.reduce("", +)
20     }
21 }

96ms

 1 class Solution {
 2     func reverseStr(_ s: String, _ k: Int) -> String {
 3 
 4         var result = [String]()
 5         var array = s.map{ String($0) }
 6         var start = 0
 7         var i = 0
 8         while i < array.count {
 9             if i == start {
10                 var end = start+k >= array.count ? array.count : start+k
11                 var slice = array[start...end-1]
12                 result += Array(slice.reversed())
13                 start+=2*k
14             } else {
15                 var j = i
16                 while j<min(i+k, array.count) {
17                     result.append(array[j])
18                     j += 1
19                 }         
20             }
21             i += k
22         }
23         return result.reduce("", +)
24     }
25 }

100ms

 1 class Solution {
 2     func reverseStr(_ s: String, _ k: Int) -> String {
 3         var array = s.map{ String($0) }
 4         var start = 0
 5         var i = 0
 6         while i < array.count {
 7             reverse(in: &array, from:i, to:i+k)
 8             i += 2*k
 9         }
10         return array.reduce("", +)
11     }
12     
13     func reverse(in array: inout [String], from: Int, to: Int) {
14         var start = from
15         var end = min(to, array.count) - 1
16         while start < end {
17             let tmp = array[start]
18             array[start] = array[end]
19             array[end] = tmp
20             
21             start += 1
22             end -= 1
23         }
24     }
25 }

116ms

 1 class Solution {
 2     func reverseStr(_ s: String, _ k: Int) -> String {
 3         if k == 1 {
 4             return s
 5         }
 6         if s.count <= k {
 7             return String(s.reversed())
 8         }
 9         var chars = Array(s)
10         var i = 0
11         while i * k < s.count {
12             if i % 2 == 0 {
13                 if (i + 1) * k > s.count {
14                     let end = s.count - i * k - 1
15                     for ii in 0 ... (end / 2) {
16                         let left = i * k + ii
17                         let right = i * k + end - ii
18                         let tmp = chars[left]
19                         chars[left] = chars[right]
20                         chars[right] = tmp
21                     }
22                 } else {
23                     for ii in 0 ... (k / 2 - 1) {
24                         let left = i * k + ii
25                         let right = i * k + k - ii - 1
26                         let tmp = chars[left]
27                         chars[left] = chars[right]
28                         chars[right] = tmp
29                     }
30                 }
31                 
32             }
33             i += 1
34         }
35         return String(chars)
36     }
37 }

136ms

 1 class Solution {
 2     func reverseStr(_ s: String, _ k: Int) -> String {
 3 
 4         var result = [String]()
 5         var array = s.map{ String($0) }
 6         var start = 0
 7         var i = 0
 8         while i < array.count {
 9             if i == start {
10                 var end = start+k >= array.count ? array.count : start+k
11                 var slice = array[start...end-1]
12                 result += Array(slice.reversed())
13                 start+=2*k
14             } else {
15                 result.append(contentsOf: array[i ..< min(i+k, array.count)])
16             }
17             i += k
18         }
19         return result.reduce("", +)
20     }
21 }

256ms

 1 class Solution {
 2     func reverseStr(_ s: String, _ k: Int) -> String {
 3         var reverseIndex = true
 4         var index = 0
 5         var newStr = ""
 6         while(index < s.count - k) {
 7             let range = s.index(s.startIndex, offsetBy: index)..<s.index(s.startIndex, offsetBy: index + k)
 8             let substr = String(s[range])
 9             newStr += reverseIndex ? reverse(substr) : substr
10             
11             reverseIndex = !reverseIndex
12             index += k
13         }
14         
15         let range = s.index(s.startIndex, offsetBy: index)..<s.endIndex
16             let substr = String(s[range])
17             newStr += reverseIndex ? reverse(substr) : substr
18         
19         return newStr
20     }
21     
22     func reverse(_ str: String) -> String {
23         var strArr = Array(str)
24         let count = strArr.count
25         guard count > 1 else { return str }
26         var index = 0
27         while(index < count / 2) {
28             let temp = strArr[count - 1 - index]
29             strArr[count - 1 - index] = strArr[index]
30             strArr[index] = temp
31             index += 1
32         }
33         return strArr.map { String(describing: $0) }.joined()
34         
35         
36     }
37 }

 296ms

 1 class Solution {
 2     func reverseStr(_ s: String, _ k: Int) -> String {
 3        var str = Array(s)
 4         var i = 0
 5         while(i < s.count) {
 6             var start = i, end = min(i + k, s.count) - 1
 7             while(start < end) {
 8                 let temp = str[end]
 9                 str[end] = str[start]
10                 str[start] = temp
11                 start += 1
12                 end -= 1
13             }
14             i += 2 * k
15         }
16         return str.map{ String(describing: $0)}.joined()
17     }
18 }

 

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