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. Centos7-两台Centos机器间复制文件

    我又两台Centos机器,一台192.168.1.1:另一台192.168.1.2 现在在将192.168.1.1上的一个文件复制到192.168.1.2.登陆到192.168.1.1然后运行命令 命 ...

  2. Spring-AOP标签scoped-proxy

    <aop:scoped-proxy/>介绍: Spring的Bean是有scope属性的,表示bean的生存周期.scope的值有prototype.singleton.session.r ...

  3. python3 第三章 - 程序的基本结构

    1.编码 默认情况下,Python 3 源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串. 当然你也可以为源码文件指定不同的编码: # -*- coding: cp-1252 -* ...

  4. 基于百度地图SDK和Elasticsearch GEO查询的地理围栏分析系统(2)-查询实现

    在上一篇博客中,我们准备好了数据.现在数据已经以我们需要的格式,存放在Elasticsearch中了. 本文讲述如何在Elasticsearch中进行空间GEO查询和聚合查询,以及如何准备ajax接口 ...

  5. java里程碑之泛型--类型通配符

    1,泛型与数组在子类上面的对比 在整理java泛型的类型通配符之前,我们先来研究下java在数组设计上的一个不合理.我们来看下面代码: public class Test { public stati ...

  6. Java常用类--处理日期

    Date Date类在java.util包中.使用Date类的无参数构造方法创建的对象可以获取本地当前时间.一般来说,也只使用这个.因为date的很多方法都已经不推荐使用了,所以Date的功能大大的消 ...

  7. XML (二)

    1 SAX 在使用DOM解析XML文档的时候,需要读取整个XML文档,在内存中构架代表整个DOM树的Document对象,从而再对XML文档进行操作.此种情况下,如果XML文档特别大,就会消耗计算机的 ...

  8. 【转】sed 学习笔记

    一  .  sed 简介 1  .  功能 sed 是一种流编辑器,所谓流编辑器是指能够对来自文件或者管道的输入流进行基本的文本转换的工具,比方说查找替换删除等. 2  .  最简单的运作机制 sed ...

  9. 关系类型字段 -- Django从入门到精通系列教程

    该系列教程系个人原创,并完整发布在个人官网刘江的博客和教程 所有转载本文者,需在顶部显著位置注明原作者及www.liujiangblog.com官网地址. Python及Django学习QQ群:453 ...

  10. Java解决CSRF问题

    项目地址: https://github.com/morethink/web-security-csrf CSRF是什么? CSRF(Cross-site request forgery),中文名称: ...