博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode第七天
阅读量:6976 次
发布时间:2019-06-27

本文共 2412 字,大约阅读时间需要 8 分钟。

==数组 Medium==

40.(162)Find Peak Element

1192699-20180109200139504-460523001.png

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

41.(731)My Calendar II

1192699-20180109214819426-1038164976.png

JAVA
public class MyCalendarTwo {    List
calendar; List
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

1192699-20180109220638894-1202801946.png

JAVA
class Solution {    public int findMin(int[] nums) {        int l = 0;        int r = nums.length-1;        while(l
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

1192699-20180109223954941-1352765099.png

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

44.(611)Valid Triangle Number

1192699-20180109232104004-581323638.png

JAVA
class Solution {    public int triangleNumber(int[] nums) {        int count = 0;        Arrays.sort(nums);        for(int i =0;i
nums[k]) k++; count += k-j-1; } } return count; }}

45.(621)Task Scheduler

1192699-20180110001448254-1050741634.png

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;    }}

转载于:https://www.cnblogs.com/guoyaohua/p/8254275.html

你可能感兴趣的文章
CORS跨域请求限制-options预检请求
查看>>
第四章 软件架构演化
查看>>
Vsftp给每个用户建立单独的配置文件
查看>>
MySQL——修改root密码的4种方法(以windows为例)
查看>>
sublime搜索和替换--多文件搜索替换
查看>>
(七):处理MFC
查看>>
【算法拾遗】大数相加(不开辟额外空间)
查看>>
Python正则表达式精讲
查看>>
Python——Beautiful Soup 4.2.0 中文文档
查看>>
Python字符串拼接、截取及替换方法总结
查看>>
3.Session安装配置
查看>>
MXNet学习:预测结果-识别单张图片
查看>>
JAVA微信开发-新手接入指南
查看>>
作业一
查看>>
virsh命令来创建虚拟机
查看>>
7.1.11、each 指针复位
查看>>
DPDK-----初识
查看>>
说说 PWA 和微信小程序--Progressive Web App
查看>>
kill命令"-1"这个参数到底是杀进程还是reload?(转)
查看>>
struts2 result type=(chain、dispatcher、redirect、redirect-action)
查看>>