last position of target / first position of target
2016-12-13 10:27
281 查看
Last Position of Target
Find the last position of a target number in a sorted array. Return -1 if target does not exist.
O(log n)
public class Solution {
/**
* @param nums: An integer array sorted in ascending order
* @param target: An integer
* @return an integer
*/
public int lastPosition(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return -1;
}
int start = 0;
int end = nums.length - 1;
while (start + 1 < end) {
int mid = (start + end) / 2;
if (target < nums[mid]) { //向后逼近
end = mid;
} else {
start = mid;
}
}
if (nums[end] == target) {
return end;
} else if (nums[start] == target) {
return start;
} else {
return -1;
}
}
}
For a given sorted array (ascending order) and a
the first index of this number in
If the target number does not exist in the array, return
Example
If the array is
O(log n)
Find the last position of a target number in a sorted array. Return -1 if target does not exist.
O(log n)
public class Solution {
/**
* @param nums: An integer array sorted in ascending order
* @param target: An integer
* @return an integer
*/
public int lastPosition(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return -1;
}
int start = 0;
int end = nums.length - 1;
while (start + 1 < end) {
int mid = (start + end) / 2;
if (target < nums[mid]) { //向后逼近
end = mid;
} else {
start = mid;
}
}
if (nums[end] == target) {
return end;
} else if (nums[start] == target) {
return start;
} else {
return -1;
}
}
}
First Position of 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.
Example
If the array is
[1, 2, 3, 3, 4, 5, 10], for given target
3, return
2.
O(log n)
class Solution { /** * @param nums: The integer array. * @param target: Target to find. * @return: The first position of target. Position starts from 0. */ public int binarySearch(int[] nums, int target) { if(nums == null || nums.length == 0) { return -1; } int start = 0, end = nums.length - 1; while(start + 1 < end) { int mid = start + (end - start) / 2; if(nums[mid] < target) { // 向前逼近 start = mid; } else { end = mid; } } if(nums[start] == target) {//check the first position first return start; } else if(nums[end] == target) { return end; } return -1; } }
相关文章推荐
- 【简单】Lintcode 14:First Position of Target
- [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)
- lintcode-easy-First Position of Target
- LintCode: First Position of Target
- [lintcode 14] First Position of Target
- Last Position of Target
- [lintcode 14] First Position of Target
- [LintCode] 14 First Position of Target
- 简单二分法-First Position of Target
- Binary Search---First Position of Target
- lintcode:First Position of Target
- Lintcode: First Position of Target (Binary Search)
- lintcode:First Position of Target
- last-position-of-target
- Lintcode14 First Position of Target Solution 题解
- First Position of Target
- 二分查找(first-position-of-target)
- #14 First Position of Target
- Lintcode 14 first position of target
- first-position-of-target