From e7edbf2e65019920b263fd81c7bb5d5ded1bb285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=E1=BB=93=20V=C4=83n=20H=C3=B2a?= <56647826+hovanhoa@users.noreply.github.com> Date: Fri, 29 Nov 2024 10:05:34 +0700 Subject: [PATCH] Time: 0 ms (100%), Space: 4.3 MB (14.25%) - LeetHub --- .../0033-search-in-rotated-sorted-array.go | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.go diff --git a/0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.go b/0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.go new file mode 100644 index 0000000..f1350d3 --- /dev/null +++ b/0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.go @@ -0,0 +1,25 @@ +func search(nums []int, target int) int { + l, r := 0, len(nums) - 1 + for l <= r { + m := (l + r) / 2 + if target == nums[m] { + return m + } + + if nums[l] <= nums[m] { + if target > nums[m] || target < nums[l] { + l = m + 1 + } else { + r = m - 1 + } + } else { + if target < nums[m] || target > nums[r] { + r = m - 1 + } else { + l = m + 1 + } + } + } + + return -1 +} \ No newline at end of file