LeetCode 之 Search for a Range — C++ 实现
2015-06-11 14:15
651 查看
Search for a Range
Given a sorted array of integers, 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
For example,
Given
return
给定一个有序的整型数组,找出给定目标值在数组中的起始位置和终止位置。
算法时间复杂度要求为 O(logn).
如果目标值未在数组中,返回[-1,
-1]
例如,
给定数组[5,
7, 7, 8, 8, 10] 和目标值 8,返回[3,
4]。
分析:
二分查找。
实现1:
先用二分法找到一个值,然后在这个值的左、右利用上次的上(high)下(low)界,分别在 low 和 mid 以及 mid 和 high 之间再进行二分查找。
实现2:
对左、右范围位置分别进行二分查找。查找左范围时,当mid位置的值小于等于 target 时,向左边查找;查找又范围时,当mid值大于等于 target 时,向右边查找。
Given a sorted array of integers, 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].
For example,
Given
[5, 7, 7, 8, 8, 10]and target value 8,
return
[3, 4].
给定一个有序的整型数组,找出给定目标值在数组中的起始位置和终止位置。
算法时间复杂度要求为 O(logn).
如果目标值未在数组中,返回[-1,
-1]
例如,
给定数组[5,
7, 7, 8, 8, 10] 和目标值 8,返回[3,
4]。
分析:
二分查找。
实现1:
先用二分法找到一个值,然后在这个值的左、右利用上次的上(high)下(low)界,分别在 low 和 mid 以及 mid 和 high 之间再进行二分查找。
class Solution { public: vector<int> searchRange(vector<int>& nums, int target) { int low = 0, high = nums.size()-1; int mid = 0, lmid = 0, rmid = 0; int left = 0, right = 0; int isfind = 0; //存在标志 vector<int> returnSize(2, -1); if(nums.empty()) //空指针或空数组 { return returnSize; } while(low <= high) //二分查找 { mid = (low + high)/2; if(nums[mid] == target) //找到一个匹配节点 { isfind = 1; break; } else if(nums[mid] < target) { low = mid + 1; } else { high = mid - 1; } } if(isfind) { left = mid; while(low <= left)//二分找左边界 { lmid = (low + left)/2; if(nums[lmid] == target) { if(lmid-1 >= low) { if(nums[lmid-1] == target) { left = lmid - 1; } else { returnSize[0] = lmid; break; } } else { returnSize[0] = lmid; break; } } else { low = lmid + 1; } } right = mid; while(right <= high)//二分找右边界 { rmid = (right + high)/2; if(nums[rmid] == target) { if(rmid+1 <= high) { if(nums[rmid+1] == target) { right = rmid + 1; } else { returnSize[1] = rmid; break; } } else { returnSize[1] = rmid; break; } } else { high = rmid - 1; } } } return returnSize; } };
实现2:
对左、右范围位置分别进行二分查找。查找左范围时,当mid位置的值小于等于 target 时,向左边查找;查找又范围时,当mid值大于等于 target 时,向右边查找。
class Solution { public: vector<int> searchRange(vector<int>& nums, int target) { int low = 0, high = nums.size()-1; int lmid = 0, rmid = 0; vector<int> returnSize(2, -1); if(nums.empty()) //空指针或空数组 { return returnSize; } while(low <= high) //对左边界进行二分查找 { lmid = (low + high)/2; if(nums[lmid] < target) { low = lmid + 1; } else { high = lmid - 1; } } if(nums[low] != target)//没找到 { return returnSize; } else { returnSize[0] = low; } low = 0; high = nums.size()-1; while(low <= high) //对右边界进行二分查找 { rmid = (low + high)/2; if(nums[rmid] <= target) { low = rmid + 1; } else { 4000 high = rmid - 1; } } returnSize[1] = high; return returnSize; } };
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解
- LeetCode [Day 5] Longest Palindromic Substring 题解
- LeetCode [Day 6] ZigZag Conversion 题解
- LeetCode 2: Add Two Numbers (JAVA)
- LeetCode 1: Two Sum (JAVA)