翻转单词顺序列
题目描述
牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?
public String ReverseSentence(String str) {
char[] chars = str.toCharArray();
reverse(chars,0,chars.length - 1);
int blank = -1;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == ' ') {
reverse(chars, blank + 1, i - 1); //blank + 1到nextBlank - 1就是要被反转的单词
blank = i;
}
}
reverse(chars, blank + 1, chars.length - 1);//最后一个单词单独进行反转
return new String(chars); //构建一个新的字符串,char[]作为参数传入
}
public void reverse(char[] chars, int low, int high) {
while (low < high) {
char temp = chars[low];
chars[low] = chars[high];
chars[high] = temp;
low++;
high--;
}
}
浙公网安备 33010602011771号