Reverse Words in a String II
Problem Descriptionβ
Visit LeetCode for the full problem description.
Solutionsβ
Solution 1: C# (Best: 476 ms)β
| Metric | Value |
|---|---|
| Runtime | 476 ms |
| Memory | N/A |
| Date | 2018-07-01 |
Solution
public class Solution {
public void ReverseWords(char[] str) {
if(str.Length<2) return;
int i=0, start=0;
reverse(str, 0, str.Length-1);
while(str[i]==' ') { i++;}
while(i<str.Length)
{
start=i;
while(i<str.Length && str[i]!=' ') {i++;}
reverse(str,start, i-1);
i++;
}
}
void reverse(char[] str, int start, int end)
{
while(start<end)
{
var temp = str[start];
str[start] = str[end];
str[end] =temp;
start++;
end--;
}
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Solution | To be analyzed | To be analyzed |