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

Appearance settings

Latest commit

 

History

History
History
33 lines (31 loc) · 939 Bytes

File metadata and controls

33 lines (31 loc) · 939 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package leetcode;
public class leetcode557 {
public String reverseWords(String s) {
int head=0;
int tail=0;
char[] chars=s.toCharArray(); //character Array
while (tail<chars.length){
//寻找各个单词的尾侧
while(tail<chars.length&&chars[tail]!=' '){
tail++;
}
tail--; //回退一格
//开始逆转
int i=head;
int j=tail;
for(;i<=(tail+head)/2&&j>(tail+head)/2;i++,j--){
char temp=chars[i];
chars[i]=chars[j];
chars[j]=temp;
}
//逆转结束
head=tail+2;
tail=head;
}
String res=String.valueOf(chars);
return String.valueOf(chars);
}
public static void main(String[] args) {
new leetcode557().reverseWords("Let's take LeetCode contest");
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.