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;
}
}
LeetCode第七天的更多相关文章
- C#版 - Leetcode 504. 七进制数 - 题解
C#版 - Leetcode 504. 七进制数 - 题解 Leetcode 504. Base 7 在线提交: https://leetcode.com/problems/base-7/ 题目描述 ...
- [递归回溯] LeetCode 504七进制数(摸鱼版)
LeetCode 七进制数 前言: 这个就没什么好说的了 题目:略 步入正题 进位制转换 10 -n 余数加倒叙 没什么好讲的直接上七进制代码 偷个懒 10进位制转7 class Solution { ...
- leetcode第七题--Reverse Integer
Problem: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- Java实现 LeetCode 504 七进制数
504. 七进制数 给定一个整数,将其转化为7进制,并以字符串形式输出. 示例 1: 输入: 100 输出: "202" 示例 2: 输入: -7 输出: "-10&qu ...
- leetcode第七题Reverse Integer (java)
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...
- LeetCode第七题
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you ...
- 领扣(LeetCode)七进制数 个人题解
给定一个整数,将其转化为7进制,并以字符串形式输出. 示例 1: 输入: 100 输出: "202" 示例 2: 输入: -7 输出: "-10" 注意: 输入 ...
- LeetCode 第七题--整数反转
1. 题目 2.思路 1. 题目 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123输出: 321 示例 2: 输入: -123输出: -321示例 ...
- [LeetCode] Base 7 基数七
Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...
随机推荐
- Intellij-@Override报错
1.设置 File >> Project Structure >> Project 中设置Project language level如下: 2. 设置 File > ...
- arduino与DS1302时钟调试失败的分析
前两天测试了时钟模块,但是,一直失败,能读取时间,但是设置不了时间,所以.这次我查了很多资料,发现好像是信号线的问题,线太长,数据收到干扰, 资料如下http://www.51hei.com/bbs/ ...
- BSA Network Shell系列-nsh命令
nsh nsh命令软链接到zsh,直接运行nsh可进入Network Shell,所有的Network Shell命令都需要运行nsh进入Network Shell执行 1 使用cd命令访问远程主机和 ...
- Servlet--取得session,application内置对象
在前面的博客里面,使用Servlet取得了request,response,config对象,实际上通过Servlet程序也可以取得session,application等内置对象. 1,通过Http ...
- org.json.JSONObject的optXXX方法
JSONObject有很多optXXX方法,比如optBoolean,optString,optInt.... 他们的意思是:如果这个JSONObject对象,如果有返回这个属性的值,如果没有返回一个 ...
- android view控件的显示和隐藏动画效果
// 显示动画 mShowAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO ...
- 号外号外!解决github+hexo+yilia评论插件的问题!!!
先走一波效果图! 本人网站--http://www.wenzheng.club/ ps:效果还是不错的,支持QQ微信登录,支持表情,甚至gif动图评论! 插件采用韩国服务器的来必力评论插件--h ...
- c# 基础任务1
1.winform系统全局异常布局处理. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); ...
- vs2012编译在win7 32位电脑和win xp电脑上运行的win32程序遇到的问题记录
一.win7 32位电脑: vs2012编译的64位程序是没有问题的.但编译的32位程序在别的电脑(虚拟机模拟)出错: 感觉很无语,vs这么牛逼的东西,在设计时候都不考虑这些吗? 在自己电脑C:\Wi ...
- javascript中的字符串对象和数组对象
1.javascript的对象的概念 在javascript中,除了null和undefined以处,其他的数据类型都被定义成了对象 也可以用创建对象的方法定义变量,string,math,array ...