File tree Expand file tree Collapse file tree 2 files changed +102
-0
lines changed Expand file tree Collapse file tree 2 files changed +102
-0
lines changed Original file line number Diff line number Diff line change 1+ public class convert {
2+
3+
4+ public static String convert (String s , int numRows ) {
5+ int length = s .length ();
6+ int nodeLength = numRows * 2 - 2 ;
7+
8+ if (length == 0 | numRows == 0 | numRows == 1 ) {
9+ return s ;
10+ }
11+ String result = "" ;
12+ for (int i = 0 ; i < numRows ; i ++) {
13+ for (int j = i ; j < length ; j += nodeLength ) {
14+ // 整列数据直接等差
15+ result += s .charAt (j );
16+ int single = j - 2 * i + nodeLength ;
17+ // 去除首行,尾行,且不能超过最长长度
18+ if (i != 0 && i != numRows - 1 && single < length ) {
19+ result += s .charAt (single );
20+ }
21+ }
22+ }
23+
24+ return result ;
25+ }
26+
27+ public static void main (String [] args ) {
28+ System .out .println (convert ("ABCDEFGHIGKLMNO" , 5 ));
29+ }
30+
31+ }
Original file line number Diff line number Diff line change 1+ # Z字型变化
2+
3+ ## 日期
4+
5+ 2018-11-15
6+
7+ ## 题目描述
8+
9+ 将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数:
10+
11+ ```
12+ P A H N
13+ A P L S I I G
14+ Y I R
15+ ```
16+
17+ 之后从左往右,逐行读取字符:"PAHNAPLSIIGYIR"
18+
19+ 实现一个将字符串进行指定行数变换的函数:
20+
21+ string convert(string s, int numRows);
22+
23+ 示例:
24+
25+ ```
26+ 示例 1:
27+
28+ 输入: s = "PAYPALISHIRING", numRows = 3
29+ 输出: "PAHNAPLSIIGYIR"
30+ 示例 2:
31+
32+ 输入: s = "PAYPALISHIRING", numRows = 4
33+ 输出: "PINALSIGYAHRPI"
34+ 解释:
35+
36+ P I N
37+ A L S I G
38+ Y A H R
39+ P I
40+ ```
41+
42+ ## 想法
43+
44+ ## My
45+
46+ ```
47+ class Solution {
48+ public String convert(String s, int numRows) {
49+ int length = s.length();
50+ int nodeLength = numRows * 2 - 2;
51+
52+ if (length == 0 | numRows == 0 | numRows == 1) {
53+ return s;
54+ }
55+ String result = "";
56+ for (int i = 0; i < numRows; i++) {
57+ for (int j = i; j < length; j += nodeLength) {
58+ // 整列数据直接等差
59+ result += s.charAt(j);
60+ int single = j - 2 * i + nodeLength;
61+ // 去除首行,尾行,且不能超过最长长度
62+ if (i != 0 && i != numRows - 1 && single < length) {
63+ result += s.charAt(single);
64+ }
65+ }
66+ }
67+
68+ return result;
69+ }
70+ }
71+ ```
You can’t perform that action at this time.
0 commit comments