Roots

1. The realization of binary search criteria

Time complexity: O (logN)

    public int binarySearch(int[] nums,int target){
        int left = 0,right = nums.length - 1;
        while(left <= right){
            int mid = (right - left);
            if(nums[mid] == target){
                return nums[mid];
            }else if(nums[mid] < target){
                left = mid + 1;
            }else{
                right = mid - 1;
            }
            // there is no 
            return - 1 ;
        }    
    }

 

2. prescribing requirements

Achieve int sqrt (int x) function.

Computes and returns the square root of x, where x is a non-negative integer.

Since the return type is an integer, the integer part of the result to retain only the fractional part is rounded down.

Example 1:

Input: 4
Output: 2
Example 2:

Input: 8
Output: 2
Description: 8 is the square root of 2.82842 ...,
  since the return type is an integer, the fractional part is rounded down.

class Solution {
    public int mySqrt(int x) {
        if(x <= 1) return x;
        int l= 1,h = x;
        while(l <= h){
            int m = (h - l) /2 + l;
            int num = x / m;
            if(m == num){
                return m;
            }else if(m < num){
                l = m + 1;
            }else{
                h = m - 1;
            }
        }
        return h;
    }
}

 

3. Given a lowercase letter contains only an ordered array of letters and a target letter target, find an ordered array which is larger than the minimum target letter alphabet.

In comparison, the array letter is circulating ordered. for example:

If the target letter target = 'z' and ordered array of letters = [ 'a', ' b'], returns the answer 'a'.
If the target letter target = 'n' and an ordered array of letters = [ 'm', ' z', 'c', 'f', 'j'], returns the answer 'z'.
 

Example:

输入:
letters = ["c", "f", "j"]
target = "a"
输出: "c"

输入:
letters = ["c", "f", "j"]
target = "c"
输出: "f"

class Solution {
    public char nextGreatestLetter(char[] letters, char target) {
        int l = 0,h = letters.length;
        while(l < h){
            int m = (h -l) / 2+ l;
            char c = letters[m];
            if(c > target){
                h = m;
            }else{
                l = m + 1;
            }
        }
        return l == letters.length ? letters[0] : letters[l];    
    }
}

4. The target start position in the array

Nums Given an array of integers arranged in ascending order, according to the target and one target value. Identify a given target start and end positions in the array.

Your time complexity of the algorithm must be O (log n) level.

If the target is not present in the array, return [-1, -1].

Example 1:

Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Example 2:

Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1, -1]

Different points: 1, different from the assignment of the right border h, two different cycling conditions (Condition 1 cause), 3, and returns the value (condition 2 is not the case since the lead edge detection about equal, and therefore needs to be determined.)

    public int[] searchRange(int[] nums, int target) {
        int l1 = getFirst(nums,target);
        int l2 = getFirst(nums,target + 1);
        if(l1 == nums.length || nums[l1] != target){
            return new int[]{-1,-1};
        }
        return new int[]{l1,l2 -1};
    }
    private int getFirst(int[] nums,int t){
        int l = 0,r = nums.length;
        while(l < r){
            int mid = (r - l) / 2 + l;
            int num = nums[mid];
            if(num >= t){
                r = mid;   
            }else{
                l = mid + 1;
            }
        }
        // need to verify whether the found 
        return L;  
    }

 

Guess you like

Origin www.cnblogs.com/zzytxl/p/12556331.html