|
@@ -1,58 +1,78 @@
|
|
|
package main
|
|
|
|
|
|
-// ??? so many problems here
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+)
|
|
|
+
|
|
|
/* func search(nums []int, target int) bool {
|
|
|
- if len(nums) == 0 {
|
|
|
+ beg, end := 0, len(nums)-1
|
|
|
+ // for empty array
|
|
|
+ if end == -1 {
|
|
|
return false
|
|
|
}
|
|
|
- beg, end := 0, len(nums)-1
|
|
|
- var mid int
|
|
|
- // how to find the smallest num of arr?
|
|
|
+ // check if the target is at the beg or at the end of the arr
|
|
|
+ if nums[beg] == target || nums[end] == target {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ // the array is not rotated
|
|
|
+ if nums[beg] < nums[end] {
|
|
|
+ return binarySearch(nums, target, beg, end)
|
|
|
+ }
|
|
|
+ // else find the top of the clip
|
|
|
for beg < end {
|
|
|
- mid = (beg + end) / 2
|
|
|
- if nums[mid] > nums[end] {
|
|
|
- beg = mid + 1
|
|
|
+ mid := (beg + end) / 2
|
|
|
+ if nums[mid] > nums[beg] {
|
|
|
+ beg = mid
|
|
|
} else if nums[mid] < nums[end] {
|
|
|
- end = mid - 1
|
|
|
+ end = mid
|
|
|
} else {
|
|
|
- for nums[mid] < nums[(mid+1)%len(nums)] {
|
|
|
- mid = (mid + len(nums) - 1) % len(nums)
|
|
|
+ // find the top point one by one
|
|
|
+ if nums[beg] > nums[beg+1] {
|
|
|
+ break
|
|
|
}
|
|
|
- break
|
|
|
+ beg++
|
|
|
}
|
|
|
}
|
|
|
- fmt.Println(mid)
|
|
|
- if target == nums[mid] {
|
|
|
- return true
|
|
|
- }
|
|
|
- if target < nums[0] {
|
|
|
- beg, end = mid+1, len(nums)-1
|
|
|
- } else {
|
|
|
- beg, end = 0, mid-1
|
|
|
+ // check the area where the target is in
|
|
|
+ if target > nums[0] {
|
|
|
+ return binarySearch(nums, target, 0, beg)
|
|
|
}
|
|
|
+ return binarySearch(nums, target, beg+1, len(nums)-1)
|
|
|
+} */
|
|
|
+
|
|
|
+func binarySearch(nums []int, target, beg, end int) bool {
|
|
|
for beg <= end {
|
|
|
- mid = (beg + end) / 2
|
|
|
- if target < nums[mid] {
|
|
|
+ mid := (beg + end) / 2
|
|
|
+ if nums[mid] > target {
|
|
|
end = mid - 1
|
|
|
- } else if target > nums[mid] {
|
|
|
+ } else if nums[mid] < target {
|
|
|
beg = mid + 1
|
|
|
} else {
|
|
|
return true
|
|
|
}
|
|
|
}
|
|
|
return false
|
|
|
-} */
|
|
|
+}
|
|
|
+
|
|
|
+func testSearch(nums []int, target int) {
|
|
|
+ isIn := search(nums, target)
|
|
|
+ checkAgain := false
|
|
|
+ for _, num := range nums {
|
|
|
+ if num == target {
|
|
|
+ checkAgain = true
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fmt.Println(isIn, checkAgain)
|
|
|
+}
|
|
|
|
|
|
/* func main() {
|
|
|
- a1 := []int{4, 5, 6, 6, 6, 7, 7, 0, 0, 1, 1, 2, 2, 3}
|
|
|
- fmt.Println(search(a1, 6))
|
|
|
- fmt.Println(search(a1, 4))
|
|
|
- fmt.Println(search(a1, 3))
|
|
|
- fmt.Println(search(a1, 7))
|
|
|
- fmt.Println(search(a1, 9))
|
|
|
- a2 := []int{1, 3}
|
|
|
- fmt.Println(search(a2, 3))
|
|
|
- a3 := []int{3, 1}
|
|
|
- fmt.Println(search(a3, 3))
|
|
|
- fmt.Println(search(a3, 1))
|
|
|
+ nums1 := []int{4, 5, 6, 7, 0, 1, 2}
|
|
|
+ nums2 := []int{2, 5, 6, 0, 0, 1, 2}
|
|
|
+ nums3 := []int{1, 2, 3, 4, 5}
|
|
|
+ nums4 := []int{1, 1}
|
|
|
+ testSearch(nums1, 1)
|
|
|
+ testSearch(nums2, 0)
|
|
|
+ testSearch(nums3, 5)
|
|
|
+ testSearch(nums4, 0)
|
|
|
} */
|