-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2515.go
56 lines (51 loc) · 1.05 KB
/
2515.go
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
func backward(cnt *int, words []string, target string, idx1 int, idx2 int) bool{
for i := idx1; i > idx2; i-- {
*cnt += 1
if(words[i] == target){
return true;
}
}
return false
}
func forward(cnt *int, words []string, target string, idx1 int, idx2 int) bool{
for i := idx1; i < idx2; i++ {
*cnt += 1
if(words[i] == target){
return true;
}
}
return false
}
func closetTarget(words []string, target string, startIndex int) int {
if(words[startIndex] == target){
return 0
}
left := 0
right := 0
firstCheck := false
sz := len(words)
for i := 0; i < sz; i++ {
if(words[i] == target){
firstCheck = true
break
}
}
if firstCheck == false {
return -1
}
check := false
check = backward(&left, words, target, startIndex - 1, -1)
if check == false {
check = backward(&left, words, target, sz - 1, startIndex)
}
check = false
check = forward(&right, words, target, startIndex + 1, sz)
if check == false {
check = forward(&right, words, target, 0, startIndex)
}
if(left < right){
return left
} else{
return right;
}
}