数组 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第七天的更多相关文章

  1. C#版 - Leetcode 504. 七进制数 - 题解

    C#版 - Leetcode 504. 七进制数 - 题解 Leetcode 504. Base 7 在线提交: https://leetcode.com/problems/base-7/ 题目描述 ...

  2. [递归回溯] LeetCode 504七进制数(摸鱼版)

    LeetCode 七进制数 前言: 这个就没什么好说的了 题目:略 步入正题 进位制转换 10 -n 余数加倒叙 没什么好讲的直接上七进制代码 偷个懒 10进位制转7 class Solution { ...

  3. leetcode第七题--Reverse Integer

    Problem: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

  4. Java实现 LeetCode 504 七进制数

    504. 七进制数 给定一个整数,将其转化为7进制,并以字符串形式输出. 示例 1: 输入: 100 输出: "202" 示例 2: 输入: -7 输出: "-10&qu ...

  5. leetcode第七题Reverse Integer (java)

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...

  6. LeetCode第七题

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you ...

  7. 领扣(LeetCode)七进制数 个人题解

    给定一个整数,将其转化为7进制,并以字符串形式输出. 示例 1: 输入: 100 输出: "202" 示例 2: 输入: -7 输出: "-10" 注意: 输入 ...

  8. LeetCode 第七题--整数反转

    1. 题目 2.思路 1. 题目 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123输出: 321 示例 2: 输入: -123输出: -321示例 ...

  9. [LeetCode] Base 7 基数七

    Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...

随机推荐

  1. _3_body_标签

    创:20_3_2017修:5_4_2017 什么是div标签? div 双 -- div标签没有任何默认属性 -可以任意写入样式和内容,和 水一样,水自然平凡而又最多 什么是h1标签? h1 标题(双 ...

  2. elasticsearch的集中常见操作

    1.引入dependency <dependency> <groupId>org.springframework.data</groupId> <artifa ...

  3. SQL 优化经验总结34条(转)

    (1) 选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE 的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table)将被最 ...

  4. secureCRT sftp使用

    sftp-- help 可用命令: cd 路径 更改远程目录到"路径" lcd 路径 更改本地目录到"路径" chgrp group path 将文件" ...

  5. 【转】软件开发工具介绍之 6.Web开发工具

    [本文转自http://www.cnblogs.com/dusonchen/archive/2011/02/09/1739087.html ] 1.EditPlus 无论是编写xhtml页面,还是cs ...

  6. Unity AssetBundle 游戏资源分类及关系

    --刚刚做完一个xlua的的热更项目,对AssetBundle资源分类总结一下.纯理论,闲谈知识,要是有建议,尽管提 ,不掺杂代码. --这里说说,AB是如何打包,如果下载,如何加载. 1.关键词理解 ...

  7. awk说明书(转)

    ref:http://blog.chinaunix.net/uid-429659-id-122573.html awk使用手册 作者:awk使用手册什么是awk? 你可能对UNIX比较熟悉,但你可能对 ...

  8. SpringBoot整合Redis、ApachSolr和SpringSession

    SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...

  9. 开发快速定位需求(Coding之前的工作)

    自我总结,求高人指点,欢迎拍砖! 目的:快速定位feature需求,避免浪费不必要的时间 需求目的:它要用来解决什么问题?(客户需求,bug fixed,学习新技术) 需求对象:它针对的对象是谁?(明 ...

  10. Nutch2.2.1在MyEclipse中的安装(window7环境)

    在https://svn.apache.org/repos/asf/nutch/branches/branch-2.2.1/网址里面可以找到Nutch2.2.1版本的资源文件. 1. 在MyEclip ...