-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution35.go
More file actions
44 lines (42 loc) · 769 Bytes
/
solution35.go
File metadata and controls
44 lines (42 loc) · 769 Bytes
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
package solution35
// ============================================================================
// 35. Search Insert Position
// https://leetcode.com/problems/search-insert-position/
// ============================================================================
func searchInsert(nums []int, target int) int {
n := len(nums)
i := n / 2
switch {
case nums[0] > target:
return 0
case nums[i] == target:
return i
case nums[i] < target:
for {
i++
if i >= n {
return i
}
if nums[i] == target {
return i
}
if nums[i] > target {
return i
}
}
case nums[i] > target:
for {
i--
if i < 0 {
return i
}
if nums[i] == target {
return i
}
if nums[i] < target {
return i + 1
}
}
}
return 0
}