代码
package com.zou.d1115;
import java.util.ArrayList;
import java.util.List;
public class PrintWordsVertically {
public static void main(String[] args) {
String str = "CONTEST IS COMING";
System.out.println(printVertically(str));
}
public static List<String> printVertically(String s) {
String[] split = s.split(" ");
List<String> list = new ArrayList<>();
int maxLength = 0;
for (String s1 : split) {
int tempLength = s1.length();
if (tempLength > maxLength) {
maxLength = tempLength;
}
}
for (int i = 0; i < maxLength; i++) {
StringBuilder sb = new StringBuilder();
for (String s1 : split) {
int tempLength = s1.length();
if (tempLength > i) {
char c = s1.charAt(i);
sb.append(c);
} else {
sb.append(" ");
}
}
int endIndex = sb.length() - 1;
for (; endIndex >= 0; endIndex--) {
char c = sb.charAt(endIndex);
if (c != ' ') {
break;
}
}
list.add(sb.substring(0, endIndex + 1));
}
return list;
}
}
结果
评论区 0