[lintcode 14] First Position of Target

For a given sorted array (ascending order) and a target number, 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.

分析:

题中给出一个有序数组,每个元素不一定唯一,找到第一次出现target的位置。

这道题处理的关键点就是当nums[mid] == target的时候,不是将mid返回,

而是把end移动的mid点,继续向前查找看有无相同的元素。

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.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) {
                end = mid;
            }else if (nums[mid] < target) {
                start = mid;
            }else {
                end = mid;
            }
        }
        if (nums[start] == target) {
            return start;
        }
        if (nums[end] == target) {
            return end;
        }
        return -1;
    }
}
时间: 2024-10-10 01:00:01

[lintcode 14] First Position of Target的相关文章

Lintcode14 First Position of Target Solution 题解

[题目描述] For a given sorted array (ascending order) and a target number, 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. 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一

First Position of Target

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) { //write your code here if (nums == null || n

lintcode-easy-First Position of Target

For a given sorted array (ascending order) and atarget number, find the first index of this number inO(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 targ

LintCode 14. 二分查找

题目:给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. 样例 在数组 [1, 2, 3, 3, 4, 5, 10] 中二分查找3,返回2. 挑战 如果数组中的整数个数超过了2^32,你的算法是否会出错? 解:标准的二分查找 注意,最后一个else即找到target,但是target的值在数组中有多个,应返回下标最小的一个(else里的while实现这一效果). class

Last Position of Target

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) { // Write your code here if (nums == null || nums.length == 0)

LintCode Search Insert Position

找出指定target的位置(没有此数时为按顺序应当位置). public class Solution { /** * param A : an integer sorted array * param target : an integer to be inserted * return : an integer */ public int searchInsert(int[] A, int target) { if(A == null) return -1; if(A.length == 0

[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Array

[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Array 本题难度: Medium/Medium Topic: Binary Search Description Given a sorted array of n integers, find the starting and ending position of a given target va

LintCode Problems Link Table

Practice Makes Perfect! # Problem Link Tag Difficulty 1 A + B problem Bitwise Operation Easy 2 Trailing Zeros Math Easy 3 Digit Counts   Medium 4 Ugly Number II   Medium 5 Kth Largest Element   Medium 6 Merge Two Sorted Arrays   Easy 7 Binary Tree Se

二分查找 - 20180906

14. First Position of Target 基本题 1 public int binarySearch(int[] nums, int target) { 2 // write your code here 3 if(nums==null || nums.length==0){ 4 return -1; 5 } 6 7 int start =0; 8 int end = nums.length-1; 9 while(start+1<end){ 10 int mid = start