-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path821.java
More file actions
59 lines (58 loc) · 1.77 KB
/
821.java
File metadata and controls
59 lines (58 loc) · 1.77 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
55
56
57
58
59
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public int[] shortestToChar(String s, char c) {
int[] res = new int[s.length()];
int last = -1;
while (last != s.length() - 1) {
int index = s.indexOf(c, last + 1);
if (index != -1) {
for (int i = last + 1; i <= index; i++) {
if (last != -1) {
res[i] = Math.min(i - last, index - i);
} else {
res[i] = index - i;
}
}
last = index;
} else {
for (int i = last + 1; i < res.length; i++) {
res[i] = i - last;
}
last = res.length - 1;
}
}
return res;
}
}
__________________________________________________________________________________________________
sample 37000 kb submission
class Solution {
public int[] shortestToChar(String S, char C) {
if(S==null || S.length() == 0)
{
return null;
}
int[] ans = new int[S.length()];
int min = Integer.MIN_VALUE/2;
for(int i = 0 ; i < S.length();i++)
{
if(S.charAt(i)==C)
{
min=i;
}
ans[i] = i-min;
}
min = Integer.MAX_VALUE/2;
for(int i = S.length()-1; i >= 0 ; i--)
{
if(S.charAt(i)==C)
{
min = i;
}
ans[i] = Math.min(ans[i],min-i);
}
return ans;
}
}
__________________________________________________________________________________________________