Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit267a8b2

Browse files
committed
convert
1 parentdc964fd commit267a8b2

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

‎string/Convert.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
packageAlgorithms.string;
2+
3+
publicclassConvert {
4+
publicstaticvoidmain(String[]strs) {
5+
System.out.println(convert("A",1));
6+
}
7+
8+
publicstaticStringconvert(Strings,intnRows) {
9+
if (s ==null) {
10+
returnnull;
11+
}
12+
13+
// 第一个小部分的大小
14+
intsize =2 *nRows -2;
15+
16+
// 当行数为1的时候,不需要折叠。
17+
if (nRows <=1) {
18+
returns;
19+
}
20+
21+
StringBuilderret =newStringBuilder();
22+
23+
intlen =s.length();
24+
for (inti =0;i <nRows;i++) {
25+
// j代表第几个BLOCK
26+
for (intj =i;j <len;j +=size) {
27+
ret.append(s.charAt(j));
28+
29+
// 即不是第一行,也不是最后一行,还需要加上中间的节点
30+
intmid =j +size -i *2;
31+
if (i !=0 &&i !=nRows -1 &&mid <len) {
32+
charc =s.charAt(mid);
33+
ret.append(c);
34+
}
35+
}
36+
}
37+
38+
returnret.toString();
39+
}
40+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp