-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
35 lines (34 loc) · 1.15 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
//记录C出现的位置在一个列表record中,然后遍历S,找出每个字母与最近的一个C的距离。用一个指针定位record,可以减少无用的遍历。
using System;
using System.Collections.Generic;
namespace ShortestDistanceToACharacter
{
class Program
{
static void Main(string[] args)
{
string S = "aaab";
char C = 'b';
Console.WriteLine(ShortestToChar(S, C));
}
static int[] ShortestToChar(string S, char C)
{
var record = new List<int>();
for (int i = 0; i < S.Length; i++)
if (S[i] == C)
record.Add(i);
int[] res = new int[S.Length];
int index = 0;
for (int i = 0; i < S.Length; i++)
{
if (i > record[index] && index < record.Count - 1)
index++;
if (index > 0)
res[i] = Math.Min(Math.Abs(i - record[index - 1]), Math.Abs(i - record[index]));
else
res[i] = Math.Abs(i - record[index]);
}
return res;
}
}
}