-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
54 lines (54 loc) · 1.19 KB
/
main.cpp
File metadata and controls
54 lines (54 loc) · 1.19 KB
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
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>
#include <string>
using namespace std;
class Solution{
public:
vector<int> shortestToChar(string S, char C) {
int n = S.length(),pos = -n;
vector<int> res(n,n);
for(int i=0; i<n; i++){
if(S[i] == C) pos = i;
res[i] = min(res[i], abs(i - pos));
}
for(int i=n-1; i>=0; i--){
if(S[i] == C) pos = i;
res[i] = min(res[i], abs(i-pos));
}
return res;
}
};
/*class Solution {
public:
vector<int> shortestToChar(string S, char C) {
int n = S.length();
vector<int> res(n,n);
for(int i=0; i<n; i++){
if(S[i] == C)
res[i] = 0;
}
for(int i=1; i<n; i++){
res[i] = min(res[i], res[i-1]+1);
}
for(int i=n-2; i>=0; i--){
res[i] = min(res[i], res[i+1]+1);
}
return res;
}
};
*/
int main()
{
Solution s;
vector<int> res;
string S = "loveleetcode";
char C = 'e';
res = s.shortestToChar(S,C);
for(int i=0; i<res.size(); i++){
cout << res[i] << endl;
}
return 0;
}