国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LintCode] First Position of Target

chuyao / 2367人閱讀

Problem

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.

challenge

If the count of numbers is bigger than 2^32, can your code work properly?

Note
while (start + 1 < end)

+1 guaranteed that there always exists mid.
In the for loop, the else branch is actually when num[mid] >= target, why? Because this ensures that the mid pointer goes to the former ones if target is right in the middle.

Solution
class Solution {
    public int binarySearch(int[] nums, int target) {
        //write your code here
        if (nums == null || nums.length < 1) {
            return -1;
        }
        int len = nums.length;
        int start = 0, end = len - 1;
        while (start + 1 < end) {
            //for the challenge: avoid overflow
            int mid = start + (end -  start) / 2;
            if (nums[mid] < target) {
                start = mid;
            }
            else {
                end = mid;
            }
        }
        //after the while loop, only num[start] and num[end] left.
        //so just discuss them
        if (nums[start] == target) {
            return start;
        }
        if (nums[end] == target) {
            return end;
        }
        return -1;
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/65482.html

相關文章

  • [LintCode/LeetCode] Search for a Range [左右邊界法/一次循環

    摘要:首先,建立二元結果數組,起點,終點。二分法求左邊界當中點小于,移向中點,否則移向中點先判斷起點,再判斷終點是否等于,如果是,賦值給。 Problem Given a sorted array of n integers, find the starting and ending position of a given target value. If the target is not...

    zhangrxiang 評論0 收藏0
  • [LintCode] strStr [KMP & brute force]

    Problem For a given source string and a target string, you should output the first index(from 0) of target string in source string. If target does not exist in source, just return -1. Note 我終于找到了比較好的K...

    Donald 評論0 收藏0
  • [LintCode/LeetCode] Jump Game I & II

    摘要:建立動規數組,表示從起點處到達該點的可能性。循環結束后,數組對所有點作為終點的可能性都進行了賦值。和的不同在于找到最少的步數。此時的一定是滿足條件的最小的,所以一定是最優解。 Jump Game Problem Given an array of non-negative integers, you are initially positioned at the first index...

    rose 評論0 收藏0
  • [LintCode] Buy Fruits

    Problem Xiao Ming is going to help companies buy fruit. Give a codeList, which is loaded with the fruit he bought. Give a shoppingCart, which is loaded with target fruit. We need to check if the order...

    邱勇 評論0 收藏0
  • [LintCode] Implement Trie

    Problem Implement a trie with insert, search, and startsWith methods. Example insert(lintcode) search(code) // return false startsWith(lint) // return true startsWith(linterror) // return false insert...

    chengjianhua 評論0 收藏0

發表評論

0條評論

chuyao

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<