package 字符串操作.q6_Z字形变换;
import java.util.ArrayList;
import java.util.List;
/**
* o(n) 可用一boolean变量代替求余操作
*/
public class Solution {
public String convert(String s, int numRows) {
if (numRows == 1) {
return s;
}
int len = s.length();
int col = 0;
int n = 0;
List<StringBuffer> list = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
StringBuffer temp = new StringBuffer();
list.add(temp);
}
while (n < len) {
int y = col % (numRows - 1);
if (y == 0) {
for (int i = 0; i < numRows && n < len; i++) {
list.get(i).append(s.charAt(n));
n++;
}
} else {
list.get(numRows - 1 - y).append(s.charAt(n));
n++;
}
col++;
}
String rs = "";
for (int i = 0; i < list.size(); i++) {
rs += list.get(i).toString();
}
return rs;
}
public static void main(String[] args) {
System.out.println(new Solution().convert("LEETCODEISHIRING", 4));
}
}
q6_Z字形变换
作品《LeetCode题目分类与面试问题整理 - q6_Z字形变换》由 不喝星巴克 发布于 匠果,转载请注明出处及链接地址:
http://www.jiangguo.net/c/9r6/732.html