LeetCode第四天
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第四天的更多相关文章
- LeetCode:四数之和【18】
LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c ...
- [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 ...
- 【LeetCode】四数之和【排序,固定k1,k2,二分寻找k3和k4】
给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...
- Java实现 LeetCode 18 四数之和
18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...
- leetcode Database2 (四)
一.Duplicate Emails Write a SQL query to find all duplicate emails in a table named Person. +----+--- ...
- [LeetCode] 18. 四数之和
题目链接:https://leetcode-cn.com/problems/4sum/ 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个 ...
- LeetCode 454.四数相加 II(C++)
给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0. 为了使问题简单化,所有的 A ...
- Leetcode 454.四数相加II
四数相加II 给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0. 为了使问题简单 ...
- LeetCode 18. 四数之和(4Sum)
题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等? ...
随机推荐
- 利用vanitygen生成比特币个性地址的教程
比特币怎么生成地址?大家都知道比特币地址都是以1开头的一串乱码,很多朋友会新建过一排地址然后在里面挑选,下面小编为大家分享下比特币生成个性地址方法. 其实就是使用vanitygen软件来生成个性化 ...
- Azure CLI对ASM,ARM资源的基本操作
本文主要介绍Windows Azure CLI对ASM及ARM资源的基本操作 1.在windows的CMD或Powershell环境下,输入命令:azure,可以查看到当前操作的模式为ASM还是ARM ...
- Django_MTV视图
WEB服务请求流程? 用户请求通过url,url带着用户的用户信息封装成request,然后服务器收到url请求,激活获取url中带来的request,服务器处理request逻辑后,返回封装好的re ...
- glReadPixel 读取数据错误问题
glReadPixel 读取数据错误问题 问题:在Android上使用 glReadPixel 读取当前渲染数据,在若干机型(华为P9以及魅族某魅蓝手机)上读取数据失败,glGetError() 没有 ...
- 【转】Matlab中的括号()[] {}
Matlab中经常会用到括号去引用某Array或者是cell的内容,但三者有什么具体区别呢?] []
- iozone测试磁盘性能
什么是iozone? iozone是一个文件系统的benchmark工具,可以测试不同的操作系统中文件系统的读写性能. 可以测试 Read, write, re-read,re-write, read ...
- oracle pl/sql中的循环及if语句
for循环 /* for循环打印1到10 */ set serveroutput on; declare begin .. loop dbms_output.put_line(i); end loop ...
- [DeeplearningAI笔记]改善深层神经网络1.1_1.3深度学习使用层面_偏差/方差/欠拟合/过拟合/训练集/验证集/测试集
觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.1 训练/开发/测试集 对于一个数据集而言,可以将一个数据集分为三个部分,一部分作为训练集,一部分作为简单交叉验证集(dev)有时候也成为验 ...
- Redis .Net 基本类型使用之南
前言 最近需要使用redis,看了一些文档,也在博客园里面看了很多文章,这里就记录下Redis常用类型的操作. String string是redis基本类型,一般通过Get,Set 命令进行操作,这 ...
- python学习交流 - 匿名函数
匿名函数 匿名函数是一个很有特色的定义函数的功能.在实际使用的过程,用户有时不得不为一些一行代码即可实现的功能来定义一个函数,例如像map, max, filter等内置函数的key参数只能接收可调用 ...