-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseWordsII.java
More file actions
44 lines (39 loc) · 845 Bytes
/
ReverseWordsII.java
File metadata and controls
44 lines (39 loc) · 845 Bytes
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
package StringProbs;
public class ReverseWordsII {
public char[] reverseWords(char[] str)
{
int left=0, right = str.length-1;
reverse(str,left,right);
for(int i=0;i<=right;++i)
{
if(str[i]==' ')
{
reverse(str,left, i-1);
left = i+1;
}
if(i==right)
{
reverse(str,left,right);
}
}
return str;
}
private void reverse(char[] str, int left, int right)
{
System.out.println(left+" "+right);
while(left<right)
{
char temp = str[left];
str[left]= str[right];
str[right]= temp;
left++;
right--;
}
}
public static void main(String[] args)
{
char[] str = new char[] {'h','e','l','l','o',' ','w','o','r','l','d',' ','D','a','v','e'};
ReverseWordsII rev = new ReverseWordsII();
System.out.println(rev.reverseWords(str));
}
}