将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。
比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下:
L C I R
E T O E S I I G
E D H N
之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"LCIRETOESIIGEDHN"。
示例 1:
输入: s = "LEETCODEISHIRING", numRows = 3
输出: "LCIRETOESIIGEDHN"
示例 2:
输入: s = "LEETCODEISHIRING", numRows = 4
输出: "LDREOEIIECIHNTSG"
解释:
L D R
E O E I I
E C I H N
T S G
package com.loo;
import java.util.ArrayList;
import java.util.List;
public class ZString {
public static void main(String[] args) {
String str = "LEETCODEISHIRING";
int rows1 = 3,rows2 = 4;
System.out.println(zString1(str , rows1));
System.out.println(zString1(str , rows2));
System.out.println(zString2(str , rows1));
System.out.println(zString2(str , rows2));
}
/*
使用 min(numRows,len(s)) 个列表来表示 Z 字形图案中的非空行。
从左到右迭代 str ,将每个字符添加到合适的行。可以使用当前行和当前方向这两个变量对合适的行进行跟踪。向上移动到最上面的行或向下移动到最下面的行时,当前方向才会发生改变。
时间复杂度:O(n)O(n)O(n),其中 n==len(s)
空间复杂度:O(n)。
*/
public static String zString1(String str , int rows) {
if (str == null || rows <= 1 || str.length() == 1) {
return str;
}
List<StringBuilder> listRows = new ArrayList<StringBuilder>();
for (int i=0;i<Math.min(rows, str.length());i++) {
listRows.add(new StringBuilder());
}
int currentRow = 0;
boolean down = false;
for (char c:str.toCharArray()) {
listRows.get(currentRow).append(c);
if (currentRow>=rows-1 || currentRow<=0) {
down = !down;
}
currentRow += down ? 1 : -1;
}
StringBuilder result = new StringBuilder();
for (StringBuilder r : listRows) {
result.append(r);
}
return result.toString();
}
/*
首先访问 第 0 行中的所有字符,接着访问 第 1 行,然后 第 2 行,依此类推...
时间复杂度:O(n),其中 n==len(s)。每个索引被访问一次。
空间复杂度:O(n)。
*/
public static String zString2(String str , int rows) {
if (str == null || rows <= 1 || str.length() == 1) {
return str;
}
int len = str.length();
StringBuilder result = new StringBuilder();
int step = 2 * rows - 2;
for (int i=0;i<rows;i++) {
for (int j=0;j+i<len;j+=step) {
result.append(str.charAt(j+i));
if (i!=0&&i!=rows-1&&j+step-i<len) {
result.append(str.charAt(j+step-i));
}
}
}
return result.toString();
}
}