// 1592. Rearrange Spaces Between Words
class Solution {
public String reorderSpaces(String text) {
int spaceQuantity = 0;
char[] chars = text.toCharArray();
for (char c : chars) {
if (c == ' ') {
++spaceQuantity;
}
}
String[] strings = text.split("\\s+");
ArrayList<String> arrayList = new ArrayList<>();
for (String s : strings) {
if (!s.equals("")) {
arrayList.add(s);
}
}
int wordQuantity = arrayList.size();
StringBuilder spaces = new StringBuilder();
StringBuilder ans = new StringBuilder();
if (wordQuantity == 1) {
ans.append(arrayList.get(0));
for (int i = 0; i < spaceQuantity; ++i) {
ans.append(' ');
}
} else {
for (int i = 0; i < spaceQuantity / (wordQuantity - 1); ++i) {
spaces.append(' ');
}
for (int i = 0; i < arrayList.size() - 1; ++i) {
ans.append(arrayList.get(i));
ans.append(spaces);
}
ans.append(arrayList.get(arrayList.size() - 1));
for (int i = 0; i < spaceQuantity % (wordQuantity - 1); ++i) {
ans.append(' ');
}
}
return ans.toString();
}
}
学习笔记:
这是一道简单的字符串题目。
这道题还是错了两次,如果只有一个单词的话,wordQuantity - 1就会变成0,会出现ZeroDivisionError,这个的确需要注意。