首页 > 其他 > 详细

start again - 20200130

时间:2020-01-31 19:05:07      阅读:67      评论:0      收藏:0      [点我收藏+]

https://leetcode.com/problems/binary-search/submissions/

技术分享图片
class Solution {
    public int search(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 - start)/2;
            if(nums[mid] == target){
                return 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;
        
    }
}
View Code

 

https://leetcode.com/problems/peak-index-in-a-mountain-array/submissions/

技术分享图片
class Solution {
    public int peakIndexInMountainArray(int[] A) {
        int start = 0;
        int end = A.length -1;
        
        while(start + 1 < end){
            int mid = start + (end -start)/2;
            
            if (mid+1 > A.length-1){
                return -1;
            }

            if (A[mid] < A[mid+1]){
                start = mid;
            }else{
                end = mid;
            }
        }
        
        if (A[start] < A[end]){
            return end;
        }
        
        return start;
    }
}
View Code

 

https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/submissions/

技术分享图片
/*
 * // This is the custom function interface.
 * // You should not implement it, or speculate about its implementation
 * class CustomFunction {
 *     // Returns f(x, y) for any given positive integers x and y.
 *     // Note that f(x, y) is increasing with respect to both x and y.
 *     // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
 *     public int f(int x, int y);
 * };
 */
class Solution {
    public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
        List<List<Integer>> res = new ArrayList<>();
        
        for (int i = 1; i <= 1000; i++){
            if (customfunction.f(i, 1)>z){
                break;
            }
            
            int start = 1;
            int end = 1000;
            while(start <= end){
                int mid = start + (end - start)/2;
                int temp = customfunction.f(i, mid);
                if(temp == z){
                    List<Integer> list = new ArrayList<>();
                    list.add(i);
                    list.add(mid);
                    res.add(list);
                    break;
                }else if(temp > z){
                    end = mid - 1;
                }else {
                    start = mid + 1;
                }
            }
            
        }
        
        return res;
    }
}
View Code

 

https://leetcode.com/problems/first-bad-version/submissions/

技术分享图片
/* The isBadVersion API is defined in the parent class VersionControl.
      boolean isBadVersion(int version); */

public class Solution extends VersionControl {
    public int firstBadVersion(int n) {
        if (n < 1){
            return - 1;
        }
        
        int start = 1;
        int end = n;
        
        while(start + 1 < end){
            int mid = start + (end - start)/2;
            if(isBadVersion(mid)){
                end = mid;
            }else{
                start = mid;
            }
        }
        
        if (isBadVersion(start)){
            return start;
        }
        
        if(isBadVersion(end)){
            return end;
        }
        
        return -1;
    }
}
View Code

 

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/submissions/

技术分享图片
class Solution {
    public int findMin(int[] nums) {
        if (nums == null || nums.length == 0){
            return -1;
        }
        
        int start = 0;
        int end = nums.length - 1;
        
        while(start + 1 < end){
            int mid = start + (end - start)/2;
            
            if(nums[mid]< nums[nums.length-1]){
                end = mid;
            }
            
            else if (nums[mid] >= nums[nums.length -1]){
                start = mid;
            }
        }
        
        if(nums[start]<nums[end]){
            return nums[start];
        }else{
            return nums[end];
        }
    }
}
View Code

 

https://leetcode.com/problems/find-peak-element/submissions/

技术分享图片
class Solution {
    public int findPeakElement(int[] nums) {
        if(nums == null || nums.length <2){
            return 0;
        }
        
        int start = 0;
        int end = nums.length -1;
        
        while(start +1 < end){
            int mid = start + (end - start)/2;
            if (mid + 1 > nums.length-1){
                break;
            }
            if(nums[mid]>nums[mid+1]){
                end = mid;
            }else{
                start = mid;
            }
        }
        
        return nums[start]<nums[end]?end:start;
    }
}
View Code

 

https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/submissions/

技术分享图片
class Solution {
    public int[] searchRange(int[] nums, int target) {
        int [] res = new int[2];
        res[0] = -1;
        res[1] = -1;
        
        if(nums == null || nums.length == 0){
            return res;
        }
        
        int start = 0;
        int 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){
                end = mid;
            }else{
                start = mid;
            } 
        }
        
        if (nums[end] == target){
            res[0] = end;
        }
        
        if (nums[start] == target){
            res[0] = start;
        }
        
        if (res[0] == -1){
            res[1] = -1;
            return res;
        }
        
        start = 0;
        end = nums.length -1;
        
        while(start + 1 < end){
            int mid = start + (end - start)/2;
            if(nums[mid] == target){
                start = mid;
            }else if (nums[mid] > target){
                end = mid;
            }else{
                start = mid;
            }
        }
        
        if(nums[start] == target){
            res[1] = start;
        }
        
        if(nums[end] == target){
            res[1] = end;
        }
        
        return res;
    }
}
View Code

 

start again - 20200130

原文:https://www.cnblogs.com/lizzyluvcoding/p/12245815.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!