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. [转]解决error: "net.ipv4.netfilter.ip_conntrack_max" is an unknown key错误

    今天在新买的vps上执行sysctl -p,报下面的错误:net.ipv4.ip_forward = 0net.ipv4.conf.default.rp_filter = 1net.ipv4.conf ...

  2. 云计算之路-阿里云上:部分服务器未及时续费造成docker swarm集群故障

    非常非常抱歉,由于我们的疏忽 —— docker swarm 集群中的 2 台服务器没有及时续费,造成在夜里0点被自动关机,从而引发整个 docker swarm 集群故障,造成今天凌晨 0:30 ~ ...

  3. Maven 常用配置

    pom.xml基础配置: <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEn ...

  4. 重置CentOS 7的Root密码

    centos7与centos6有很多修改,不一样了,打算写几篇关于日常用到的改动 修改root密码 centos7的用户模式跟6有所不同 1 - 在启动grub菜单,选择编辑选项启动 2 - 按键盘e ...

  5. 【转】Linux从入门到精通——运维工程师成长路线图——CTO马哥Linux视频教学

    加油! http://edu.51cto.com/roadmap/view/id-2.html#6853467-sqq-1-36881-57ccc7d95ea58df839decd91bd220170

  6. 【转】布同:如何循序渐进学习Python语言

    大家都知道Python语言是一种新兴的编程语言.1989年,Python就由Guido van Rossum发明.Python一直发展态势很好. 原因有几点:1.跨平台性好.Linux.Windows ...

  7. 安装Java和Pycharm的步骤

    [root@nhserver1 usr]# java -versionjava version "1.7.0_25"OpenJDK Runtime Environment (rhe ...

  8. 我的java之路week2类的无参、带参方法

    2.1语法 public 返回值类型 方法名(){ //方法体 } 2.2方法的调用语法 对象名.方法名 计算平均分和总成绩 public class Score { /** * 创建类 ScoreC ...

  9. 微信浏览器返回刷新,监听微信浏览器返回事件,网页防复制,移动端禁止图片长按和vivo手机点击img标签放大图片

    以下代码都经过iphone7,华为MT7 ,谷歌浏览器,微信开发者工具,PC端微信验证.如有bug,还请在评论区留言. demo链接:https://pan.baidu.com/s/1c35mbjM ...

  10. 浅谈ASP.NET配置文件加密

    在刚刚完成的一个ASP.NET项目中,遇到了这么一个问题,项目部署到生产环境中时,领导要求项目中的配置文件(如web.config,app.config)中不能出现敏感字符,如:数据库连接,等等. 第 ...