简单二分法-First Position of Target
2018-01-23 11:07
399 查看
Problem 7:
For a given sorted array (ascending order) and a
of this number in
If the target number does not exist in the array, return
数组设置start,end,mid变量进行index循环比较,start=end时也就是指的是同一个元素时跳出循环,此时判断array[start]是否与target一致:
For a given sorted array (ascending order) and a
targetnumber, find the first index
of this number in
O(log n)time complexity.
If the target number does not exist in the array, return
-1.
数组设置start,end,mid变量进行index循环比较,start=end时也就是指的是同一个元素时跳出循环,此时判断array[start]是否与target一致:
class Solution { public: /** * @param nums: The integer array. * @param target: Target number to find. * @return: The first position of target. Position starts from 0. */ int binarySearch(vector<int> &array, int target) { // write your code here //Using binary search if(array.empty()||array.size() == 0 ) return -1; int start = 0, end = array.size()-1; while(start < end){ int mid = start + (end - start)/2; if(array[mid] == target) end = mid; else if (array[mid] < target) start = mid +1; else end = mid -1; } if(array[start] == target) return start; return -1; } };若当start = end -1 时跳出循环,此时start与end 相邻,需要判断start和end两个位置对应的值是否与target一致:
class Solution { public: /** * @param nums: The integer array. * @param target: Target number to find. * @return: The first position of target. Position starts from 0. */ int binarySearch(vector<int> &array, int target) { // write your code here //Using binary search if(array.empty()||array.size() == 0 ) return -1; int start = 0, end = array.size() -1; while(start +1 < end){ int mid = start + (end - start)/2; if(array[mid] == target) end = mid; else if (array[mid] < target) start = mid; else end = mid; } if(array[start] == target) return start; if(array[end] == target) return end; return -1; } };上述两个方法是迭代实现,如果要递归实现:
class Solution { /** * @param nums: The integer array. * @param target: Target to find. * @return: The first position of target. Position starts f 4000 rom 0. */ public int binarySearch(int[] nums, int target) { //write your code here if(nums.length==1&&target==nums[0]) return 0; int len = nums.length; int index = binaryFind(nums,target,0,len); return index; } public int binaryFind(int[] nums,int target,int left,int right){ int median = (left+right)/2; if(nums[left]==target) return left; if(left >= right ) return -1; if(target == nums[median]&&target != nums[median-1]) return median;//获取最前匹配位置 if(target > nums[median]) return binaryFind(nums,target,median+1,right); if(target <= nums[median]) return binaryFind(nums,target,left,median); return -1; } }递归算法参照http://www.cnblogs.com/wangnanabuaa/p/4987304.html
相关文章推荐
- 【简单】Lintcode 14:First Position of Target
- LintCode: First Position of Target
- First Position of Target
- lintcode-easy-First Position of Target
- lintcode:First Position of Target
- 二分查找(first-position-of-target)
- Lintcode14 First Position of Target Solution 题解
- First Position of Target
- [lintcode 14] First Position of Target
- [lintcode 14] First Position of Target
- [LintCode] 14 First Position of Target
- Lintcode: First Position of Target (Binary Search)
- first-position-of-target
- Lintcode 14 first position of target
- last position of target / first position of target
- lintcode:First Position of Target
- #14 First Position of Target
- Binary Search---First Position of Target
- [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)
- Last Position of Target