题目:
6. Z 字形变换
题解:
R
=3时:
P A H N
A P L S I I G
Y I R
1 5 9 13
2 4 6 8 10 12 14
3 7 11
R
=4时:
P I N
A L S I G
Y A H R
P I
1 7 13
2 6 8 12 14
3 5 9 11
4 10
假设当前行数是r,总行数R,
I(n
)表示某行第n个字母在原字符串中的index,n从
0开始:
当r
=1,r
=R时,
I(n
+1) = I(n
)+2(R
-1);
当
1<r
<R时,
I(n
+1) = I(n
)+2(R
-r
) n为偶数时,
I(n
+1) = I(n
)+2(r
-1) n为奇数时。
代码:
public class code6 {
public static String
convert(String s
, int numRows
) {
if (s
== null
|| s
.length() == 0 || numRows
== 1 || numRows
>= s
.length()) {
return s
;
}
StringBuilder sb
= new StringBuilder();
for (int i
= 1; i
< s
.length() + 1; i
+= 2 * (numRows
- 1)) {
sb
.append(s
.charAt(i
- 1));
}
for (int i
= 2; i
< numRows
; i
++) {
boolean k
= true;
for (int j
= i
; j
< s
.length() + 1; j
+= (k
) ? 2 * (numRows
- i
) : 2 * (i
- 1), k
= !k
) {
sb
.append(s
.charAt(j
- 1));
}
}
for (int i
= numRows
; i
< s
.length() + 1; i
+= 2 * (numRows
- 1)) {
sb
.append(s
.charAt(i
- 1));
}
return sb
.toString();
}
public static void main(String
[] args
) {
String s1
= "LEETCODEISHIRING";
int numRows1
= 4;
String str1
= convert(s1
, numRows1
);
System
.out
.println(str1
);
String s2
= "LEETCODEISHIRING";
int numRows2
= 3;
String str2
= convert(s2
, numRows2
);
System
.out
.println(str2
);
}
}
参考:
LeetCode第六题之Z字形变换Z 字形变换画解算法:6. Z 字形变换Z 字形变换(清晰图解)6. Z 字形变换
附:
Java StringBuffer 和 StringBuilder 类StringBuilder用法小结深入理解Java常用类-----StringBuilderJava集合–ListJava—List的用法与实例详解JAVA集合 - list常用方法Java List类Java 列表