Fork me on GitHub

LeetCode第七天

数组 Medium

40.(162)Find Peak Element

JAVA
//斜率思想,二分法
class Solution {
    public int findPeakElement(int[] nums) {
        int l=0,r=nums.length-1;
        while(l<r){      
            int mid = (r+l)/2;
            if(nums[mid]>nums[mid+1])
                r = mid;
            else
                l = mid+1;
        }
        return l;
    }
}

41.(731)My Calendar II

JAVA
public class MyCalendarTwo {
    List<int[]> calendar;
    List<int[]> overlaps;//已经重叠过一次的区间

    MyCalendarTwo() {
        calendar = new ArrayList();
        overlaps = new ArrayList();
    }

    public boolean book(int start, int end) {
        for (int[] iv: overlaps) {
            if (iv[0] < end && start < iv[1]) return false;
        }
        for (int[] iv: calendar) {
            if (iv[0] < end && start < iv[1])
                overlaps.add(new int[]{Math.max(start, iv[0]), Math.min(end, iv[1])});
        }
        calendar.add(new int[]{start, end});
        return true;
    }
}

42.(153)Find Minimum in Rotated Sorted Array

JAVA
class Solution {
    public int findMin(int[] nums) {
        int l = 0;
        int r = nums.length-1;
        while(l<r){
            int mid = (l+r)/2;
            if(nums[mid]<nums[r])
                r = mid;
            else
                l = mid+1;
        }
        return nums[r];
    }
}
class Solution {
    public int findMin(int[] nums) {
       return find(nums,0,nums.length-1);
    }
    
    public int find(int[] nums, int l, int r) {
        if(nums[l] <= nums[r]) {
            return nums[l];
        }
        int mid = (l + r) / 2;
        return Math.min(find(nums,l,mid),find(nums,mid+1,r));
    }
}

43.(152)Maximum Product Subarray

JAVA
class Solution {
    public int maxProduct(int[] nums) {
        if(nums.length == 0){
            return 0;
        }
        int maxPre = nums[0];
        int minPre = nums[0];
        int max = nums[0];
        for(int i =1;i<nums.length;i++){
            int maxHere = Math.max(Math.max(maxPre*nums[i],minPre*nums[i]),nums[i]);
            int minHere = Math.min(Math.min(maxPre*nums[i],minPre*nums[i]),nums[i]);
            max = Math.max(max,maxHere);
            maxPre = maxHere;
            minPre = minHere;
        }
        return max;
    }
}

44.(611)Valid Triangle Number

JAVA
class Solution {
    public int triangleNumber(int[] nums) {
        int count = 0;
        Arrays.sort(nums);
        for(int i =0;i<nums.length-2;i++){
            if(nums[i]==0)
                continue;
            int k = i+2;
            for(int j = i+1;j<nums.length-1;j++){
                while(k<nums.length&&nums[i]+nums[j]>nums[k])
                    k++;
                count += k-j-1;
            }
        }
        return count;
    }
}

45.(621)Task Scheduler

JAVA
//计算休眠时间,再加上任务时间等于总时间
class Solution {
    public int leastInterval(char[] tasks, int n) {
        int[] map = new int[26];
        for(char c : tasks)
            map[c-'A']++;
        Arrays.sort(map);
        int idle = (map[25] -1)*n;
        for(int i=24;i>=0&&map[i]>0;i--){
            idle -= Math.min(map[i],map[25]-1);
        }
        return idle >0 ? tasks.length+idle:tasks.length;
    }
}
posted @ 2018-01-10 00:16  郭耀华  阅读(211)  评论(0编辑  收藏  举报