leetcode 第四天

2018年1月4日

15.(628)Maximum Product of Three Numbers

JAVA
class Solution {
public int maximumProduct(int[] nums) {
Arrays.sort(nums);
return (nums[0]*nums[1]*nums[nums.length-1])>(nums[nums.length-1]*nums[nums.length-2]*nums[nums.length-3])?(nums[0]*nums[1]*nums[nums.length-1]):(nums[nums.length-1]*nums[nums.length-2]*nums[nums.length-3]);
}
}

16.(628)Maximum Product of Three Numbers

JAVA
class Solution {
public int thirdMax(int[] nums) {
Integer first = null ,second = null,third = null;
for(Integer n : nums){
if(n.equals(first)||n.equals(second)||n.equals(third)) continue;
if(first==null||n>first){
third = second;
second = first;
first = n;
}else if(second == null||n>second){
third = second;
second = n;
}else if(third == null||n>third){
third = n;
} } return third == null?first:third;
} }

17.(643) Maximum Average Subarray I

JAVA
class Solution {
public double findMaxAverage(int[] nums, int k) {
double window = 0;
double maxAvg = 0;
for(int i =0;i<k;i++){
window +=nums[i];
maxAvg = window;
} for(int i = k;i<nums.length;i++){
window = window+nums[i]-nums[i-k];
maxAvg = Math.max(maxAvg,window); } return maxAvg/k;
}
}

18.(448) Find All Numbers Disappeared in an Array

JAVA
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> result = new ArrayList<Integer>();
for(int i =0;i<nums.length;i++){ nums[Math.abs(nums[i])-1] = -1 * Math.abs(nums[Math.abs(nums[i])-1]);
}
for(int i =0;i<nums.length;i++){
if(nums[i]>0)
result.add(i+1);
}
return result;
}
}

19.(485) Max Consecutive Ones

JAVA
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int max = 0;
int times = 0;
for(int i =0;i<nums.length;i++){
if(nums[i]==1){
times++;
max = Math.max(max,times);
}else{
times = 0;
}
}
return max;
}
}

20.(88) Merge Sorted Array

JAVA
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
while(n > 0){
if(m>0)
nums1[n+m-1] = nums1[m-1]>nums2[n-1]?nums1[--m]:nums2[--n];
else
nums1[n-1] = nums2[--n];
}
}
}

21.(605) Can Place Flowers

JAVA
class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int count = 0;
for(int i =0;i<flowerbed.length;i++){
if(flowerbed[i]==0&&(i==0||flowerbed[i-1]==0)&&(i==flowerbed.length-1||flowerbed[i+1]==0)){
count++;
flowerbed[i]=1;
if(count==n)
return true;
}
}
return count >= n;
}
}

LeetCode第四天的更多相关文章

  1. LeetCode:四数之和【18】

    LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c ...

  2. [LeetCode] 4Sum 四数之和

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  3. 【LeetCode】四数之和【排序,固定k1,k2,二分寻找k3和k4】

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...

  4. Java实现 LeetCode 18 四数之和

    18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...

  5. leetcode Database2 (四)

    一.Duplicate Emails Write a SQL query to find all duplicate emails in a table named Person. +----+--- ...

  6. [LeetCode] 18. 四数之和

    题目链接:https://leetcode-cn.com/problems/4sum/ 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个 ...

  7. LeetCode 454.四数相加 II(C++)

    给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0. 为了使问题简单化,所有的 A ...

  8. Leetcode 454.四数相加II

    四数相加II 给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0. 为了使问题简单 ...

  9. LeetCode 18. 四数之和(4Sum)

    题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等? ...

随机推荐

  1. linux_nginx_rewrite

    什么是Nginx的rewrite? 实现URL地址重写,比较复杂的write需要开发来完成,伪静态处理实现是开发的工作, 这rewrite写在location中 指令语法:    rewrite re ...

  2. duilib消息类型

    //定义所有消息类型 ////////////////////////////////////////////////////////////////////////// #define DUI_MS ...

  3. linkin大话设计模式--观察者模式

    linkin大话设计模式--观察者模式 观察者模式定义了对象间的一对多依赖关系,让一个或者多个观察者观察一个对象主题.当主题对象的状态发生改变的时候,系统能通知所有的依赖于此对象的观察者对象,从而能自 ...

  4. sp_getAppLock使用

    sp_getAppLock  获取程序资源锁,简单的说就是调用此函数可以达到我们程序中.NET的lock锁的作用. 作用域是当前数据库下 四个参数: @resource(必填):资源名称,类型nvar ...

  5. Problem C: Pie

    题目链接 http://codeforces.com/gym/100722/attachments/download/3466/20062007-northwestern-european-regio ...

  6. The man Command

    The man command is used to format and display the man pages. The man pages are a user manual that is ...

  7. 强大的API测试工具Hitchhiker v0.9 基于UI的断言测试,回顾2017

    v0.9是Hitchhiker在2017农历年的最后一个版本,而起点正是刚过完2016农历年,农历2018即将到来,一年轮回,今天写点东西稍微回顾下hitchhiker的2017. 先还是说v0.9, ...

  8. HashMap实现分析

    HashMap最基本的实现思想如下图所示,使用数组加链表的组合形式来完成数据的存储. Entry在数组中的位置是由key的hashcode决定的. 向一个数组长度为16,负载因子为0.75的HashM ...

  9. Maven入门知识介绍

    1.1 Maven简介 Apache Maven 是一个软件项目管理工具.基于项目对象模型的概念,Maven可用来管理项目的依赖.编译.文档 等信息. 使用maven管理项目时,项目的依赖的jar包将 ...

  10. selenium headlesschrome下设置最大窗口模式

    做微博登录的时候,用selenium的chrome界面模式,可以用下面方式显示最大窗口: from selenium.webdriver.chrome.options import Options c ...