-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitchByZ.java
More file actions
56 lines (54 loc) · 2.03 KB
/
SwitchByZ.java
File metadata and controls
56 lines (54 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class SwitchByZ {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String s = sc.next();
int n = sc.nextInt();
String result = convert(s, n);
System.out.println(result);
}
public static String convert(String s, int numRows) {
List<Character> list = new ArrayList<>();
int n = s.length();
Character[][] martix = new Character[numRows][n];
int row = 0;
int col = 0;
int count =0;
boolean rowflag = false; //向右移动
boolean colflag = true; //向下移动
if(numRows ==1) return s;
while( count != s.length() ) {
martix[row][col] = s.charAt(count);
count++;
if(row != numRows-1 && colflag && !rowflag) { //向下存储
row++;
}
else if(row == numRows-1 ) { // 转向向右上移动
rowflag = !rowflag;
colflag = !colflag;
row--;
col++;
}
else if(row > 0 &&row < numRows-1 && rowflag && !colflag){ //向右上移动
row--;
col++;
}
else if( row == 0 && rowflag && !colflag) {
rowflag = !rowflag; //转向向下移动
colflag = !colflag;
row++;
}
}
StringBuffer result = new StringBuffer();
for(int i = 0; i < numRows ; i++) {
for(int j =0; j < n ; j++) {
if (martix[i][j] == null) continue;
if(martix[i][j] < 'z' || martix[i][j]> 'a' || martix[i][j] < 'Z' || martix[i][j]> 'A') result.append(martix[i][j]);
}
}
String a = result.toString();
return a;
}
}