Skip to content

Commit 88d1ab9

Browse files
committed
ZigZag Conversion.
1 parent b2b8c23 commit 88d1ab9

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

Medium/5.Longest Palindromic Substring.playground/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Solution {
2222
private var rightPosition = 0
2323
private var maxLength = 0
2424
func longestPalindrome(_ s: String) -> String {
25-
let characterArr = s.map { $0 }
25+
let characterArr = Array(s)
2626
for i in 0..<s.count {
2727
var index1 = i
2828
var index2 = i
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
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)
3+
4+
P A H N
5+
A P L S I I G
6+
Y I R
7+
And then read line by line: "PAHNAPLSIIGYIR"
8+
9+
Write the code that will take a string and make this conversion given a number of rows:
10+
11+
string convert(string s, int numRows);
12+
 
13+
14+
Example 1:
15+
Input: s = "PAYPALISHIRING", numRows = 3
16+
Output: "PAHNAPLSIIGYIR"
17+
18+
Example 2:
19+
Input: s = "PAYPALISHIRING", numRows = 4
20+
Output: "PINALSIGYAHRPI"
21+
Explanation:
22+
P I N
23+
A L S I G
24+
Y A H R
25+
P I
26+
27+
Example 3:
28+
Input: s = "A", numRows = 1
29+
Output: "A"
30+
 
31+
32+
Constraints:
33+
- 1 <= s.length <= 1000
34+
- s consists of English letters (lower-case and upper-case), ',' and '.'.
35+
- 1 <= numRows <= 1000
36+
*/
37+
class Solution {
38+
func convert(_ s: String, _ numRows: Int) -> String {
39+
if numRows == 1 { return s }
40+
var matrix: [[Character]] = Array(repeating: Array(), count: numRows)
41+
42+
for (i, e) in s.enumerated() {
43+
let remiander = i % (numRows - 1)
44+
let quotient = i / (numRows - 1)
45+
if quotient % 2 == 0 {
46+
matrix[remiander].append(e)
47+
} else {
48+
matrix[numRows - 1 - remiander].append(e)
49+
}
50+
}
51+
52+
var res = ""
53+
for arr in matrix {
54+
for e in arr {
55+
res.append(e)
56+
}
57+
}
58+
return res
59+
}
60+
}
61+
62+
let s = Solution()
63+
let r = s.convert("PAYPALISHIRING", 4)
64+
print(r)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Medium/6.ZigZag Conversion.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,4 @@
108108
1. [Min Stack](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/155.Min%20Stack.playground/Contents.swift)
109109
2. [Longest Substring Without Repeating Characters](https://github.com/recherst/leetcode-algtorithm/blob/main/Medium/3.Longest%20Substring%20Without%20Repeating%20Characters.playground/Contents.swift)
110110
3. [Longest Palindromic Substring](https://github.com/recherst/leetcode-algtorithm/blob/main/Medium/5.Longest%20Palindromic%20Substring.playground/Contents.swift)
111+
4. [ZigZag Conversion](https://github.com/recherst/leetcode-algtorithm/blob/main/Medium/6.ZigZag%20Conversion.playground/Contents.swift)

0 commit comments

Comments
 (0)