forked from ecmadao/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNo06.zigzag-conversion.swift
More file actions
67 lines (63 loc) · 1.87 KB
/
No06.zigzag-conversion.swift
File metadata and controls
67 lines (63 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* Difficulty:
* Medium
*
* Desc:
* 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 text, int nRows);
* convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
/*
* 找规律
* "PAYPALISHIRING", 4
* P I N // 3
* A L S I G // 5
* Y A H R // 4
* P I // 2
* "PAYPALISHIRING", 2
* P Y A I H R N
* A P L S I I G
*/
class Solution {
func char(s: String, row: Int, column: Int, numRows: Int) -> Character? {
let baseBefore = numRows + max(numRows - 2, 0)
var offset = baseBefore * (column / 2) + row
if column % 2 != 0 {
// 奇数列
offset = Int(ceil(Double(column) / 2)) * baseBefore - row
}
if offset < s.count && offset >= 0 {
let index = s.index(s.startIndex, offsetBy: offset)
return s[index]
}
return nil
}
func convert(_ s: String, _ numRows: Int) -> String {
if numRows == 1 {
return s
}
var result = [Character]()
let maxColumn = Int(ceil(Double(s.count * 2) / Double(numRows + max(numRows - 2, 0))))
for row in 0..<numRows {
var column = 0
let columnOffset = row == 0 || row == numRows - 1 ? 2 : 1
while column < maxColumn {
let char = self.char(s: s, row: row, column: column, numRows: numRows)
if char == nil {
break
}
result += [char!]
column += columnOffset
}
}
return String(result)
}
}