leetcode刷题11. 盛最多水的容器
做题连接https://leetcode-cn.com/problems/container-with-most-water/submissions/
本题分为两种方法:
暴力法:
int maxArea(int* height, int heightSize) {
int maxarea=0,s;
for(int i=0;i<heightSize;i++)
for(int j=i+1;j<heightSize;j++)
{
s=(height[i]>height[j])?height[j]:height[i];
maxarea=(s*(j-i)>maxarea)?(s*(j-i)):maxarea;
}
return maxarea;
}
分别比较数组中边缘最小,然后面积较大值返回
头尾指针法:
int maxArea(int* height, int heightSize) {
int maxarea=0,s,i=0,j=heightSize-1;
while(i<j){
s=(height[i]>height[j])?height[j]:height[i];
maxarea=(s*(j-i)>maxarea)?(s*(j-i)):maxarea;
if(height[i]<height[j])
i++;
else
j--;
}
return maxarea;
}
leetcode刷题11. 盛最多水的容器的更多相关文章
- LeetCode随缘刷题之盛最多水的容器
package leetcode.day_01_30; /** * 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点(i,ai) .在坐标内画 n 条垂直线,垂直线 i的两个端 ...
- C#LeetCode刷题之#11-盛最多水的容器(Container With Most Water)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3615 访问. 给定 n 个非负整数 a1,a2,...,an,每 ...
- Java实现 LeetCode 11 盛最多水的容器
11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...
- 力扣Leetcode 11. 盛最多水的容器
盛最多水的容器 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找 ...
- Leetcode题库——11.盛最多水的容器
@author: ZZQ @software: PyCharm @file: maxArea.py @time: 2018/10/11 21:47 说明:给定 n 个非负整数 a1,a2,...,an ...
- 【LeetCode】11. 盛最多水的容器
题目 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两 ...
- LeetCode 11. 盛最多水的容器(Container With Most Water)
题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两 ...
- Leetcode 11.盛最多水的容器 By Python
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
- LeetCode 11 - 盛最多水的容器 - [双指针暴力]
题目链接:https://leetcode-cn.com/problems/container-with-most-water/description/ 给定 n 个非负整数 $a_1,a_2,\cd ...
随机推荐
- Java IO系列之二:NIO基本操作
核心部分 NIO( New Input/ Output) , 引入了一种基于通道和缓冲区的 I/O 方式,NIO 是一种同步非阻塞的 IO 模型.同步是指线程不断轮询 IO 事件是否就绪,非阻塞是指 ...
- Maven 学习总结 (六) 之 版本
版本管理 版本管理是指项目整体版本的演变过程管理.版本控制是指借助版本控制工具(如Subversion)追踪代码的每一个变更. 为了方便团队合作,项目开发过程中,大家应该使用快照版本,快照版本机制促进 ...
- DirectX11--HLSL编译着色器的三种方法
前言 本教程不考虑Effects11(FX11),而是基于原始的HLSL. 目前编译与加载着色器的方法如下: 使用Visual Studio中的HLSL编译器,随项目编译期间一同编译,并生成.cso( ...
- 【1】MySQL大数据量分页查询方法及其优化
---方法1: 直接使用数据库提供的SQL语句---语句样式: MySQL中,可用如下方法: SELECT * FROM 表名称 LIMIT M,N---适应场景: 适用于数据量较少的情况(元组百/千 ...
- 13、Ajax的使用
一.AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. a).AJAX = 异步 JavaScript 和 XML. b).AJAX 是一种用于创建快速动态网页的技术. 通过在后 ...
- Android AVD启动报错:emulator: ERROR: x86_64 emulation currently requires hardware acceleration! Please ensure Intel HAXM is properly installed and usable.
打开Android SDK manager查看安装发现HAXM在windows上无法安装 可以去 http://www.androiddevtools.cn/index.html 下载 Android ...
- 初识Asp.Net WebApi
using System;using System.Collections.Generic;using System.Linq;using System.Net.Http;using System.T ...
- [Kubernetes]容器日志的收集与管理
在开始这篇文章之前,首先要明确一点: Kubernetes 中对容器日志的处理方式,都叫做 cluster-level-logging ,也就是说,这个日志处理系统,与容器, Pod 以及 Node ...
- mybatis调用oracle存储过程的几个参考例子
首先写一个存储过程: create or replace procedure p_syn_equipment_20161205 is sqlstr ); begin --清空表 sqlstr := ' ...
- Nginx:Linux下安装Nginx与配置
准备目录 [root@sijizhen ~]# mkdir /usr/local/nginx [root@sijizhen ~]# cd /usr/local/nginx/ 下载 1.Nginx,在h ...