-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
57 lines (56 loc) · 1.66 KB
/
Program.cs
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
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
namespace ReverseWordsInAStringII
{
class Program
{
static void Main(string[] args)
{
char[] s = {'t', 'h', 'e', ' ', 's', 'k', 'y', ' ', 'i', 's', ' ', 'b', 'l', 'u', 'e'};
ReverseWords(s);
}
static void ReverseWords(char[] s)
{
var reversed = new char[s.Length];
var spaceIndex = new List<int>();
for (int i = 0; i < s.Length; i++)
{
if(s[i] == ' ')
spaceIndex.Add(i);
}
spaceIndex.Add(s.Length);
int count = 0, index = 0;
for (int i = 0; i < s.Length; i++)
{
if (i == spaceIndex[index])
{
index++;
count = 0;
continue;
}
reversed[s.Length - spaceIndex[index] + count] = s[i];
count++;
}
for (int i = 0; i < reversed.Length; i++)
s[i] = reversed[i] == 0 ? ' ' : reversed[i];
}
static void ReverseWords_InPlace(char[] s)
{
Array.Reverse(s);
var word = "";
for (int i = 0; i < s.Length; i++)
{
if (s[i] != ' ')
word += s[i];
else
{
for (int j = 0; j < word.Length; j++)
s[i - j - 1] = word[j];
word = "";
}
}
for (int i = 0; i < word.Length; i++)
s[^(i + 1)] = word[i];
}
}
}