2 - Binary Search & LogN Algorithm

462. Total Occurence of Target (本质:查找 target 第一次出现的位置 + 查找 target 最后一次出现的位置)

https://www.lintcode.com/problem/total-occurrence-of-target/description?_from=ladder&&fromId=1

public class Solution {
    /**
     * @param A: A an integer array sorted in ascending order
     * @param target: An integer
     * @return: An integer
     */
    public int totalOccurrence(int[] A, int target) {
        // write your code here
        if(A == null || A.length == 0) return 0;
        int start = findStart(A, target);
        int end = findEnd(A, target);
        return end >= start ? end - start + 1 : 0;
    }
    
    public int findEnd(int[] A, int target) {
        int left = 0;
        int right = A.length - 1;
        while(left + 1 < right) {
            int mid = left + (right - left) / 2;
            if(A[mid] > target) {
                right = mid;
            } else {
                left = mid;
            }
        }
        if(A[right] == target) {
            return right;
        } 
        if(A[left] == target) {
            return left;
        }
        return 0;
    }
    
    
    public int findStart(int[] A, int target) {
        int left = 0;
        int right = A.length - 1;
        while(left + 1 < right) {
            int mid = left + (right - left) / 2;
            if(A[mid] >= target) {
                right = mid;
            } else {
                left = mid;
            }
        }
        if(A[left] == target) {
            return left;
        }
        if(A[right] == target) {
            return right;
        }
        return A.length;
    }
}

459. Closest Number in Sorted Array (本质:从排好序的数组中查找一个元素,第一个大于 target 的元素的下标 )

https://www.lintcode.com/problem/closest-number-in-sorted-array/description?_from=ladder&&fromId=1

public class Solution {
    /**
     * @param A: an integer array sorted in ascending order
     * @param target: An integer
     * @return: an integer
     */
    public int closestNumber(int[] A, int target) {
        // write your code here
        if(A == null || A.length == 0) return -1;
        int index = findFirstGreater(A, target);
        if(index == 0) return 0;
        if(index == A.length) return A.length - 1;
        if(A[index] - target < target - A[index - 1]) {
            return index;
        } else {
            return index - 1;
        }
    }
    
    public int findFirstGreater(int[] A, int target) {
        int start = 0;
        int end = A.length - 1;
        while(start + 1 < end) {
            int mid = start + (end - start) / 2;
            if(A[mid] <= target) {
                start = mid;
            } else {
                end = mid;
            }
        }
        if(A[start] > target) {
            return start;
        }
        if(A[end] > target) {
            return end;
        }
        return A.length;
    }
}

235. Prime Factorization 

https://www.lintcode.com/problem/prime-factorization/description?_from=ladder&&fromId=1

这道题不像平时遇到的那种:从一个排好序的数组中找一个目标元素。

这个题是把 num 二分。

二分:i * i <= num;

public class Solution {
    /**
     * @param num: An integer
     * @return: an integer array
     */
    public List<Integer> primeFactorization(int num) {
        // write your code here
        List<Integer> result = new LinkedList<>();
        for(int i = 2; i * i <= num; i++) {
            while(num % i == 0) {
                num /= i;
                result.add(i);
            }
        }
        if(num != 1) {
            result.add(num);
        }
        return result;
    }
}

猜你喜欢

转载自www.cnblogs.com/jenna/p/10700253.html
今日推荐