前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode第七天

LeetCode第七天

作者头像
10JQKA
发布2018-05-09 15:20:51
6150
发布2018-05-09 15:20:51

==数组 Medium==

40.(162)Find Peak Element
JAVA
代码语言:javascript
复制
//斜率思想,二分法
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
代码语言:javascript
复制
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
  1. 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]; } }
代码语言:javascript
复制
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
代码语言:javascript
复制
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
代码语言:javascript
复制
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
代码语言:javascript
复制
//计算休眠时间,再加上任务时间等于总时间
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;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-01-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ==数组 Medium==
    • 40.(162)Find Peak Element
      • 41.(731)My Calendar II
        • 42.(153)Find Minimum in Rotated Sorted Array
          • 43.(152)Maximum Product Subarray
            • 44.(611)Valid Triangle Number
              • 45.(621)Task Scheduler
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档