-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
42 lines (41 loc) · 1001 Bytes
/
Copy pathsolution.java
File metadata and controls
42 lines (41 loc) · 1001 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
// 557. Reverse Words in a String III
// https://leetcode.com/problems/reverse-words-in-a-string-iii/
// Easy | Java | Accepted 2022-08-06
// Runtime 1619 ms | Memory 495.9 MB
class Solution {
public String reverseWords(String s) {
int start = s.length()-1;
s+=" ";
String joe = "";
int j = s.indexOf(" ");
if(j<0)
{
while(start>=0)
{
joe+=s.substring(start, start+1);
start--;
}
return joe;
}
else
{
while(start>=0)
{
start+=j;
int k = j;
while(j-1>=0)
{
joe+=s.substring(j-1,j);
j--;
}
if(s.length()>0)
{
joe+=s.substring(k,k+1);
}
s = s.substring(k+1);
j = s.indexOf(" ");
}
return joe.substring(0,joe.length()-1);
}
}
}