diff --git a/Algorithms/0034. Find First and Last Position of Element in Sorted Array/34. Find First and Last Position of Element in Sorted Array.go b/Algorithms/0034. Find First and Last Position of Element in Sorted Array/34. Find First and Last Position of Element in Sorted Array.go new file mode 100644 index 00000000..6c678071 --- /dev/null +++ b/Algorithms/0034. Find First and Last Position of Element in Sorted Array/34. Find First and Last Position of Element in Sorted Array.go @@ -0,0 +1,78 @@ +package leetcode + +func searchRange(nums []int, target int) []int { + return []int{searchFirstEqualElement(nums, target), searchLastEqualElement(nums, target)} + +} + +// 二分查找第一个与 target 相等的元素,时间复杂度 O(logn) +func searchFirstEqualElement(nums []int, target int) int { + low, high := 0, len(nums)-1 + for low <= high { + mid := low + ((high - low) >> 1) + if nums[mid] > target { + high = mid - 1 + } else if nums[mid] < target { + low = mid + 1 + } else { + if (mid == 0) || (nums[mid-1] != target) { // 找到第一个与 target 相等的元素 + return mid + } + high = mid - 1 + } + } + return -1 +} + +// 二分查找最后一个与 target 相等的元素,时间复杂度 O(logn) +func searchLastEqualElement(nums []int, target int) int { + low, high := 0, len(nums)-1 + for low <= high { + mid := low + ((high - low) >> 1) + if nums[mid] > target { + high = mid - 1 + } else if nums[mid] < target { + low = mid + 1 + } else { + if (mid == len(nums)-1) || (nums[mid+1] != target) { // 找到最后一个与 target 相等的元素 + return mid + } + low = mid + 1 + } + } + return -1 +} + +// 二分查找第一个大于等于 target 的元素,时间复杂度 O(logn) +func searchFirstGreaterElement(nums []int, target int) int { + low, high := 0, len(nums)-1 + for low <= high { + mid := low + ((high - low) >> 1) + if nums[mid] >= target { + if (mid == 0) || (nums[mid-1] < target) { // 找到第一个大于等于 target 的元素 + return mid + } + high = mid - 1 + } else { + low = mid + 1 + } + } + return -1 +} + +// 二分查找最后一个小于等于 target 的元素,时间复杂度 O(logn) +func searchLastLessElement(nums []int, target int) int { + low, high := 0, len(nums)-1 + for low <= high { + mid := low + ((high - low) >> 1) + if nums[mid] <= target { + if (mid == len(nums)-1) || (nums[mid+1] > target) { // 找到最后一个小于等于 target 的元素 + return mid + } + low = mid + 1 + } else { + high = mid - 1 + } + } + return -1 +} diff --git a/Algorithms/0034. Find First and Last Position of Element in Sorted Array/34. Find First and Last Position of Element in Sorted Array_test.go b/Algorithms/0034. Find First and Last Position of Element in Sorted Array/34. Find First and Last Position of Element in Sorted Array_test.go new file mode 100644 index 00000000..52d408aa --- /dev/null +++ b/Algorithms/0034. Find First and Last Position of Element in Sorted Array/34. Find First and Last Position of Element in Sorted Array_test.go @@ -0,0 +1,47 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question34 struct { + para34 + ans34 +} + +// para 是参数 +// one 代表第一个参数 +type para34 struct { + nums []int + target int +} + +// ans 是答案 +// one 代表第一个答案 +type ans34 struct { + one []int +} + +func Test_Problem34(t *testing.T) { + + qs := []question34{ + + question34{ + para34{[]int{5, 7, 7, 8, 8, 10}, 8}, + ans34{[]int{3, 4}}, + }, + + question34{ + para34{[]int{5, 7, 7, 8, 8, 10}, 6}, + ans34{[]int{-1, -1}}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 34------------------------\n") + for _, q := range qs { + _, p := q.ans34, q.para34 + fmt.Printf("【input】:%v 【output】:%v\n", p, searchRange(p.nums, p.target)) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/0034. Find First and Last Position of Element in Sorted Array/README.md b/Algorithms/0034. Find First and Last Position of Element in Sorted Array/README.md new file mode 100755 index 00000000..831b0756 --- /dev/null +++ b/Algorithms/0034. Find First and Last Position of Element in Sorted Array/README.md @@ -0,0 +1,36 @@ +# [34. Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/) + + +## 题目: + +Given an array of integers `nums` sorted in ascending order, find the starting and ending position of a given `target` value. + +Your algorithm's runtime complexity must be in the order of *O*(log *n*). + +If the target is not found in the array, return `[-1, -1]`. + +**Example 1:** + + Input: nums = [5,7,7,8,8,10], target = 8 + Output: [3,4] + +**Example 2:** + + Input: nums = [5,7,7,8,8,10], target = 6 + Output: [-1,-1] + +## 题目大意 + +给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。你的算法时间复杂度必须是 O(log n) 级别。如果数组中不存在目标值,返回 [-1, -1]。 + + +## 解题思路 + +- 给出一个有序数组 `nums` 和一个数 `target`,要求在数组中找到第一个和这个元素相等的元素下标,最后一个和这个元素相等的元素下标。 +- 这一题是经典的二分搜索变种题。二分搜索有 4 大基础变种题: + 1. 查找第一个值等于给定值的元素 + 2. 查找最后一个值等于给定值的元素 + 3. 查找第一个大于等于给定值的元素 + 4. 查找最后一个小于等于给定值的元素 + + 这一题的解题思路可以分别利用变种 1 和变种 2 的解法就可以做出此题。或者用一次变种 1 的方法,然后循环往后找到最后一个与给定值相等的元素。不过后者这种方法可能会使时间复杂度下降到 O(n),因为有可能数组中 n 个元素都和给定元素相同。(4 大基础变种的实现见代码)