★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9872595.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
The string "PAYPALISHIRING"
is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N A P L S I I G Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string s, int numRows);
Example 1:
Input: s = "PAYPALISHIRING", numRows = 3 Output: "PAHNAPLSIIGYIR"
Example 2:
Input: s = "PAYPALISHIRING", numRows = 4 Output: "PINALSIGYAHRPI" Explanation: P I N A L S I G Y A H R P I
将字符串 "PAYPALISHIRING"
以Z字形排列成给定的行数:
P A H N A P L S I I G Y I R
之后从左往右,逐行读取字符:"PAHNAPLSIIGYIR"
实现一个将字符串进行指定行数变换的函数:
string convert(string s, int numRows);
示例 1:
输入: s = "PAYPALISHIRING", numRows = 3 输出: "PAHNAPLSIIGYIR"
示例 2:
输入: s = "PAYPALISHIRING", numRows = 4 输出: "PINALSIGYAHRPI" 解释: P I N A L S I G Y A H R P I
44ms
1 class Solution { 2 func convert(_ s: String, _ numRows: Int) -> String { 3 guard numRows > 1 else { return s } 4 let characters = Array(s) 5 let interval = 2 * (numRows - 1) 6 7 var result = [Character]() 8 var i = 0, offset = 0, pos = 0 9 10 while offset < numRows { 11 i = 0 12 while true { 13 pos = interval * i + offset 14 guard pos < characters.count else { break } 15 result.append(characters[pos]) 16 17 if offset > 0 && offset < numRows - 1 { 18 pos = interval * (i + 1) - offset 19 guard pos < characters.count else { break } 20 result.append(characters[pos]) 21 } 22 23 i += 1 24 } 25 26 offset += 1 27 } 28 29 return String(result) 30 } 31 }
48ms
1 class Solution { 2 func convert(_ s: String, _ numRows: Int) -> String { 3 if numRows == 1 { 4 return s 5 } 6 var stringArray: Array<Character> = Array(s) 7 8 var answerArray: Array<String> = Array(repeating: String(), count: numRows) 9 var flag: Int = 0 10 var currentRow: Int = 0 11 var answer: String = String() 12 for i in 0..<stringArray.count { 13 if (flag + i) % numRows == 0 { 14 flag += 1 15 } 16 answerArray[currentRow].append(stringArray[i]) 17 if flag % 2 == 0 { 18 currentRow -= 1 19 } else { 20 currentRow += 1 21 } 22 } 23 for str in answerArray { 24 answer += str 25 } 26 return answer 27 } 28 }
52ms
1 class Solution { 2 func convert(_ s: String, _ numRows: Int) -> String { 3 if numRows == 1 || numRows >= s.count { 4 return s 5 } 6 var stringArray: Array<Character> = Array(s) 7 8 var answerArray: Array<String> = Array(repeating: String(), count: numRows) 9 var flag: Int = 0 10 var currentRow: Int = 0 11 var answer: String = String() 12 for i in 0..<stringArray.count { 13 if (flag + i) % numRows == 0 { 14 flag += 1 15 } 16 answerArray[currentRow].append(stringArray[i]) 17 currentRow = currentRow + 2 * (flag % 2) - 1 18 } 19 for str in answerArray { 20 answer += str 21 } 22 return answer 23 } 24 }
64ms
1 class Solution { 2 func convert(_ s: String, _ numRows: Int) -> String { 3 if numRows == 1 { 4 return s 5 } 6 var stringArray: Array<Character> = Array(s) 7 var answerArray: Array<String> = Array(repeating: String(), count: numRows) 8 var flag: Int = 0 9 var currentRow: Int = 0 10 var answer: String = String() 11 for i in 0..<stringArray.count { 12 if (flag + i) % numRows == 0 { 13 flag += 1 14 } 15 answerArray[currentRow].append(stringArray[i]) 16 if flag % 2 == 0 { 17 currentRow -= 1 18 } else { 19 currentRow += 1 20 } 21 } 22 return answerArray.joined() 23 } 24 }
76ms
1 class Solution { 2 func convert(_ s: String, _ numRows: Int) -> String { 3 guard numRows > 1 else { 4 return s 5 } 6 var result = "" 7 let arr = s.map {$0} 8 let interval = (numRows-1)*2 9 for rowIndex in 0..<numRows { 10 for index in stride(from: rowIndex, to: arr.count, by: interval) { 11 result.append(arr[index]) 12 if rowIndex > 0 && rowIndex < numRows - 1 { 13 let next = index + (interval - 2*rowIndex) 14 if next < arr.count { 15 result.append(arr[next]) 16 } 17 } 18 } 19 } 20 return result 21 } 22 }
80ms
1 class Solution { 2 func convert(_ s: String, _ numRows: Int) -> String { 3 if numRows == 1 { 4 return s 5 } 6 7 let chars = s.utf8CString 8 var result = [CChar]() 9 let count = chars.count - 1 10 let cycle = 2 * numRows - 2 11 for i in 0..<numRows { 12 var j = 0 13 while i + j < count { 14 defer { 15 j += cycle 16 } 17 18 result.append(chars[i + j]); 19 if i != 0 && i != numRows - 1 20 && j + cycle - i < count { 21 result.append(chars[j + cycle - i]) 22 } 23 } 24 } 25 result.append(0) 26 return String(cString: result) 27 } 28 }
88ms
1 class Solution { 2 func convert(_ s: String, _ numRows: Int) -> String { 3 var chars = Array(s) 4 var result = "" 5 6 let groupCount = max(1, (numRows - 1) * 2) 7 8 for r in 0..<numRows { 9 var current = r 10 var skip = (numRows - r - 1) * 2 11 if skip == 0 { 12 skip = groupCount 13 } 14 while current < chars.count { 15 result.append(chars[current]) 16 current += skip 17 skip = groupCount - skip 18 if skip == 0 { 19 skip = groupCount 20 } 21 } 22 } 23 return result 24 } 25 }
96ms
1 class Solution { 2 func convert(_ s: String, _ numRows: Int) -> String { 3 if s == "" { return "" } 4 if numRows == 1 { return s} 5 6 var output = Array(s) 7 let array = Array(s) 8 let len = array.count 9 var row = numRows 10 var k=0 11 12 for j in 0..<numRows { 13 let cycle1 = numRows - j - 1 14 let cycle2 = numRows - row 15 var i = j 16 17 while i < len { 18 if cycle1 > 0, i < len { 19 output[k] = array[i] 20 k = k + 1 21 i = i + cycle1 + cycle1 22 } 23 if cycle2 > 0, i < len { 24 output[k] = array[i] 25 k = k + 1 26 i = i + cycle2 + cycle2 27 } 28 } 29 row = row - 1 30 } 31 return String(output) 32 } 33 }
100ms
1 class Solution { 2 func convert(_ s: String, _ numRows: Int) -> String { 3 if numRows == 1 {return s} 4 var ret:String = String() 5 var n:Int = s.count 6 var cycleLen = 2 * numRows - 2 7 8 for i in 0..<numRows 9 { 10 var j:Int = 0 11 while( j + i < n) 12 { 13 ret.append(s[s.index(s.startIndex, offsetBy:j + i)]) 14 if i != 0 && i != numRows - 1 && j + cycleLen - i < n 15 { 16 ret.append(s[s.index(s.startIndex, offsetBy:j + cycleLen - i)]) 17 } 18 //注意应放在语句最后 19 j += cycleLen 20 } 21 } 22 return String(ret) 23 } 24 }