-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6-Convert.py
More file actions
140 lines (114 loc) · 3.35 KB
/
6-Convert.py
File metadata and controls
140 lines (114 loc) · 3.35 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Python3
class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows == 1:
return s
n = min(numRows, len(s))
rows = [""]*n
curRow = 0
goingDown = False
for c in s:
rows[curRow] += c
#print(rows)
if curRow == 0 or curRow == n-1:
goingDown = not goingDown
curRow += 1 if goingDown else -1
res = ""
for i in range(n):
res += rows[i]
return res
def main():
test = Solution()
res = test.convert("ABC" , 2)
print(res)
if __name__ == "__main__":
main()
# # C++ 按行排序 时间复杂度 O(n) 空间复杂度 O(n)
# class Solution {
# public:
# string convert(string s, int numRows) {
# if (numRows == 1) return s;
# vector<string> rows(min(numRows, int(s.size()))); # 使用列表表示Z字形图案中的非空行
# int curRow = 0;
# bool goingDown = false;
# for (char c : s) {
# rows[curRow] += c;
# if (curRow == 0 || curRow == numRows - 1) {
# goingDown = !goingDown;
# curRow += goingDown ? 1 : -1;
# }
# }
# string ret;
# for (string row : rows) {
# ret += row;
# }
# return ret;
# }
# };
# # Java 按行排序
# class Solution {
# public String convert(String s, int numRows) {
# if (numRows == 1) {
# return s;
# }
# List<StringBuilder> rows = new ArrayList<>();
# for (int i = 0; i < Math.min(numRows, s.length()); i++) {
# rows.add(new StringBuilder());
# }
# int curRow = 0;
# boolean goingDown = false;
# for (char c : s.toCharArray()) {
# rows.get(curRow).append(c);
# if (curRow == 0 || curRow == numRows - 1) {
# goingDown = !goingDown;
# }
# curRow += goingDown ? 1 : -1;
# }
# StringBuilder ret = new StringBuilder();
# for (StringBuilder row : rows) {
# ret.append(row);
# }
# return ret.toString();
# }
# }
# C++ 按行访问 时间复杂度 O(n)
class Solution {
public:
string convert(string s, int numRows) {
if (numRows == 1) {
return s;
}
string ret;
int n = s.size();
int cycleLen = 2 * numRows - 2; # 周期规律
for (int i = 0; i < numRows; i++) {
for (int j = 0; j + i < n; j += cycleLen) {
ret += s[j + i];
if (i != 0 && i != numRows - 1 && j + cycleLen - i < n) {
ret += s[j + cycleLen - i];
}
}
}
return ret;
}
};
# Java
class Solution {
public String convert(String s, int numRows) {
if (numRows == 1) {
return s;
}
StringBuilder ret = new StringBuilder();
int n = s.length();
int cycleLen = 2 * numRows - 2;
for (int i = 0; i < numRows; i++) {
for (int j = 0; j + i < n; j += cycleLen) {
ret.append(s.charAt(j + i));
if (i != 0 && i != numRows - 1 && j + cycleLen - i < n) {
ret.append(s.charAt(j + cycleLen - i));
}
}
}
return ret.toString();
}
}