LeetCode -- Flora -- edit 2025-04-25

1.盛最多水的容器

11. 盛最多水的容器

已解答

中等

相关标签

相关企业

提示

给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。

找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

返回容器可以储存的最大水量。

说明:你不能倾斜容器。

示例 1:

输入:[1,8,6,2,5,4,8,3,7]
输出:49 
解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。

示例 2:

输入:height = [1,1]
输出:1

提示:

  • n == height.length
  • 2 <= n <= 105
  • 0 <= height[i] <= 104
public static void main(String[] args) {int[] nums = {1,8,6,2,5,4,8,3,7};int maxArea = maxArea3(nums);System.out.println(maxArea);}public static int maxArea3(int[] height) {int left = 0, right = height.length - 1;//左指针,右指针int res = 0;//maxAreawhile(left < right){//左指针<右指针int w = right - left;//xint h = Math.min(height[left], height[right]);//yres = Math.max(res, w * h);while(left < right && height[left] <= h) left++;//移动左指针,寻找比当前y值更大的y值while(left < right && height[right] <= h) right--;//移动右指针,寻找比当前y值更大的y值}return res;}//1-my(2)public static int maxArea2(int[] height) {int maxArea = 0;for (int i = 0; i < height.length; i++) {//左指针for (int j = height.length-1; j >= 0 && j>=i ; j--) {//右指针int x = j - i;int y = Math.min(height[i],height[j]);int area = x * y;maxArea = Math.max(maxArea,area);if (height[i] < height[j]){break;}}}return maxArea;}//1-mypublic static int maxArea(int[] height) {int maxArea = 0;for (int i = height.length-1; i >= 0 ; i--) {//x轴大小for (int j = 0; j < height.length; j++) {//indexif (j+i<height.length) {int left = height[j];int right = height[j + i];int min = Math.min(left, right);int area = min * i;maxArea = Math.max(maxArea,area);}}}return maxArea;}

2.三数之和

15. 三数之和

给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != ji != k 且 j != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请你返回所有和为 0 且不重复的三元组。

注意:答案中不可以包含重复的三元组。

示例 1:

输入:nums = [-1,0,1,2,-1,-4]
输出:[[-1,-1,2],[-1,0,1]]
解释:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0 。
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0 。
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0 。
不同的三元组是 [-1,0,1] 和 [-1,-1,2] 。
注意,输出的顺序和三元组的顺序并不重要。

示例 2:

输入:nums = [0,1,1]
输出:[]
解释:唯一可能的三元组和不为 0 。

示例 3:

输入:nums = [0,0,0]
输出:[[0,0,0]]
解释:唯一可能的三元组和为 0 。

提示:

  • 3 <= nums.length <= 3000
  • -105 <= nums[i] <= 105

my(1-3)

    public static List<List<Integer>> threeSum3(int[] nums) {Arrays.sort(nums);Set<List<Integer>> temp = new HashSet<>();for (int i = 0; i < nums.length && nums[i] <= 0 ; i++) {for (int j = i+1; j < nums.length; j++) {for (int k = j+1; k < nums.length; k++) {if (i!=j && j!=k && k!= i && nums[i]+ nums[j]+nums[k]==0 ){List<Integer> inner = new ArrayList<>();inner.add(nums[i]);inner.add(nums[j]);inner.add(nums[k]);temp.add(inner);}}}}return new ArrayList<>(temp);}public static List<List<Integer>> threeSum2(int[] nums) {Set<List<Integer>> temp = new HashSet<>();for (int i = 0; i < nums.length; i++) {for (int j = 0; j < nums.length; j++) {for (int k = 0; k < nums.length; k++) {if (i!=j && j!=k && k!= i && nums[i]+ nums[j]+nums[k]==0 ){List<Integer> inner = new ArrayList<>();inner.add(nums[i]);inner.add(nums[j]);inner.add(nums[k]);inner = inner.stream().sorted().collect(Collectors.toList());temp.add(inner);}}}}return new ArrayList<>(temp);}public static List<List<Integer>> threeSum(int[] nums) {//请你返回所有和为 0 且不重复的三元组 的下标组合List<List<Integer>> res = new ArrayList<>();Map<Integer,List<Integer>> map = new HashMap<>();for (int left = 0; left < nums.length; left++) {for (int right = nums.length - 1; right >=0; right--) {int temp = 0 - nums[left] - nums[right];List<Integer> mapOrDefault = map.getOrDefault(temp, new ArrayList<>());if (mapOrDefault.size()==0){mapOrDefault.add(left);mapOrDefault.add(right);map.put(temp,mapOrDefault);}}}for (int i = 0; i < nums.length; i++) {List<Integer> mapOrDefault = map.getOrDefault(nums[i], new ArrayList<>());if (mapOrDefault.size()==2){mapOrDefault.add(i);int size = mapOrDefault.stream().collect(Collectors.toSet()).size();if (size==3) {res.add(mapOrDefault);}}}return res;}

others-1

    public static void main(String[] args) {int[] nums1 = {-1,0,1,-2,0,2,-2,-1,0};int[] nums2 = {0,0,0};int[] nums = {-1,0,1,2,-1,-4,-2,-3,3,0,4};List<List<Integer>> threeSum = findTriplets(nums);System.out.println(threeSum);}public static List<List<Integer>> findTriplets(int[] nums) {Set<List<Integer>> result = new HashSet<>(); // 用于去重Arrays.sort(nums); // 先排序数组,方便去重和双指针for (int i = 0; i < nums.length - 2; i++) {if (i > 0 && nums[i] == nums[i - 1]) continue; // 跳过重复的 iint left = i + 1;int right = nums.length - 1;while (left < right) {//i!=j && j!=k && k!= iint sum = nums[i] + nums[left] + nums[right];if (sum == 0) { // 三数之和为 0,nums[i]+ nums[j]+nums[k]==0List<Integer> inner = new ArrayList<>();inner.add(nums[i]);inner.add(nums[left]);inner.add(nums[right]);result.add(inner); // 自动去重left++;right--;} else if (sum < 0) {left++;} else {right--;}}}return new ArrayList<>(result);}

others-2

    public static List<List<Integer>> findTriplets2(int[] nums) {return new AbstractList<List<Integer>>() {// Declare List of List as a class variableprivate List<List<Integer>> list;// Implement get method of AbstractList to retrieve an element from the listpublic List<Integer> get(int index) {// Call initialize() methodinitialize();// Return the element from the list at the specified indexreturn list.get(index);}// Implement size method of AbstractList to get the size of the listpublic int size() {// Call initialize() methodinitialize();// Return the size of the listreturn list.size();}// Method to initialize the listprivate void initialize() {// Check if the list is already initializedif (list != null)return;// Sort the given arrayArrays.sort(nums);// Create a new ArrayListlist = new ArrayList<>();// Declare required variablesint l, h, sum;// Loop through the arrayfor (int i = 0; i < nums.length; i++) {// Skip the duplicatesif (i != 0 && nums[i] == nums[i - 1])continue;// Initialize l and h pointersl = i + 1;h = nums.length - 1;// Loop until l is less than hwhile (l < h) {// Calculate the sum of three elementssum = nums[i] + nums[l] + nums[h];// If sum is zero, add the triple to the list and update pointersif (sum == 0) {list.add(getTriple(nums[i], nums[l], nums[h]));l++;h--;while (l < h && nums[l] == nums[l - 1])l++;while (l < h && nums[h] == nums[h + 1])h--;} else if (sum < 0) {// If sum is less than zero, increment ll++;} else {// If sum is greater than zero, decrement hh--;}}}}};}private static List<Integer> getTriple(int i, int j, int k){return new AbstractList<Integer>() {private int[] data;// Constructor to initialize the triple with three integers// Method to initialize the listprivate void initialize(int i, int j, int k) {if (data != null)return;data = new int[] { i, j, k };}// Implement get method of AbstractList to retrieve an element from the triplepublic Integer get(int index) {// Call initialize() methodinitialize(i, j, k);return data[index];}// Implement size method of AbstractList to get the size of the triplepublic int size() {// Call initialize() methodinitialize(i, j, k);return 3;}};}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/78332.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

有关图的类型的题目以及知识点(2)

1、具有5个顶点的有向完全图有20条弧。 2、若一个有向图用邻接矩阵表示&#xff0c;则第个结点的入度就是&#xff1a;第i列的非零元素的个数。 3、有向图的邻接矩阵可以是对称的&#xff0c;也可以是不对称的。 4、设N个顶点E条边的图用邻接表存储&#xff0c;则求每个顶点…

正则表达式的捕获组

是正则表达式中的一个重要概念&#xff0c;用于提取字符串中的特定部分 捕获组是通过正则表达式中的圆括号 () 定义的&#xff0c;它的作用是&#xff1a; 划分和标记&#xff1a;将正则表达式的一部分划分为逻辑单元。 提取数据&#xff1a;从字符串中提取符合组内模式的内容…

deepseek-cli开源的强大命令行界面,用于与 DeepSeek 的 AI 模型进行交互

一、软件介绍 文末提供程序和源码下载 deepseek-cli一个强大的命令行界面&#xff0c;用于与 DeepSeek 的 AI 模型进行交互。 二、Features 特征 Multiple Model Support 多模型支持 DeepSeek-V3 (deepseek-chat) DeepSeek-R1 &#xff08;deepseek-reasoner&#xff09;Dee…

Java—— 五道算法水题

第一题 需求&#xff1a; 包装类&#xff1a;键盘录入一些1~100之间的整数&#xff0c;并添加到集合中。直到集合中所有数据和超过200为止 代码实现&#xff1a; import java.util.ArrayList; import java.util.Scanner;public class Test1 {public static void main(String[]…

安全编排自动化与响应(SOAR):从事件响应到智能编排的技术实践

安全编排自动化与响应&#xff08;SOAR&#xff09;&#xff1a;从事件响应到智能编排的技术实践 在网络安全威胁复杂度指数级增长的今天&#xff0c;人工处理安全事件的效率已难以应对高频攻击&#xff08;如日均万级的恶意IP扫描&#xff09;。安全编排自动化与响应&#xf…

网络原理 - 9

目录 数据链路层 以太网 以太网帧格式 MAC 地址 DNS&#xff08;Domain Name System&#xff09; 完&#xff01; 数据链路层 这里的内容也是简单了解&#xff0c;除非是做交换机开发&#xff0c;一般程序员不需要涉及~~ 以太网 ”以太网“不是一种具体的网络&#xf…

unity bug

发现一个奇怪的bug&#xff0c;就是某些unity版本打包apk时候不允许StreamingAssets里面有中文文件或者中文路径。比如下图这面这俩都是不行的。 解决方案&#xff1a;中文改为英文即可。 一般报错信息如下&#xff1a; > Configure project :launcher WARNING:The option s…

【Linux网络】打造初级网络计算器 - 从协议设计到服务实现

&#x1f4e2;博客主页&#xff1a;https://blog.csdn.net/2301_779549673 &#x1f4e2;博客仓库&#xff1a;https://gitee.com/JohnKingW/linux_test/tree/master/lesson &#x1f4e2;欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; 如有错误敬请指正&#xff01; &…

计算机视觉——对比YOLOv12、YOLOv11、和基于Darknet的YOLOv7的微调对比

概述 目标检测领域取得了巨大进步&#xff0c;其中 YOLOv12、YOLOv11 和基于 Darknet 的 YOLOv7 在实时检测方面表现出色。尽管这些模型在通用目标检测数据集上表现卓越&#xff0c;但在 HRSC2016-MS&#xff08;高分辨率舰船数据集&#xff09; 上对 YOLOv12 进行微调时&…

‌MySQL 事务隔离级别详解

‌ 以下是 MySQL 支持的四种事务隔离级别及其特性&#xff0c;按并发安全性从低到高排列&#xff1a; ‌1. 读未提交 (Read Uncommitted)‌ ‌问题‌&#xff1a; ‌脏读 (Dirty Read)‌&#xff1a;事务可读取其他事务未提交的数据。‌不可重复读 (Non-repeatable Read)‌&am…

如何解决IDE项目启动报错 error:0308010C:digital envelope routines::unsupported 问题

如何解决IDE项目启动报错 error:0308010C:digital envelope routines::unsupported 问题 在现代软件开发过程中&#xff0c;开发人员通常使用集成开发环境&#xff08;IDE&#xff09;如IntelliJ IDEA、Visual Studio Code&#xff08;VSCode&#xff09;等进行Node.js项目开发…

2025最新Facefusion3.1.2使用Docker部署,保姆级教程,无需配置环境

Docker部署Facefusion 环境 windows10 Facefusion3.1.2 安装 拉取源代码 git clone https://github.com/facefusion/facefusion-docker.git 此处如果拉不下来&#xff0c;需要科学上网&#xff0c;不会的可以找我。 运行容器 将Dockerfile.cpu文件中的的From python:3.…

docker容器监控自动恢复

关于实现对docker容器监控以及自动恢复&#xff0c;这里介绍两种实现方案。 方案1&#xff1a; 实现思路&#xff1a; 找到&#xff08;根据正则表达式&#xff09;所有待监控的docker容器&#xff0c;此处筛选逻辑根据docker运行状态找到已停止&#xff08;Exit&#xff09;类…

HackMyVM - Chromee靶机

HackMyVM - chromee靶机https://mp.weixin.qq.com/s/hF09_24PRXpx_lmB6dzWVg

Cursor中调用本地大语言模型

引言 随着大语言模型(LLM)技术的快速发展&#xff0c;越来越多的开发者希望在本地环境中运行这些强大的AI模型&#xff0c;以获得更好的隐私保护、更低的延迟以及不依赖网络连接的使用体验。Cursor作为一款面向开发者的AI增强编辑器&#xff0c;提供了与本地大语言模型集成的功…

青少年CTF-贪吃蛇

题目描述&#xff1a; 进入赛题页面&#xff1a; 按F12&#xff0c;查看源代码&#xff0c; 可以看到是当分数大于或等于10000时&#xff0c;获得flag&#xff0c;值已经给出&#xff0c;直接引用就可以&#xff0c;check_score.php?score${score}&#xff0c;这里将${score}换…

亚马逊测评老砍单?了解过全新自养号系统吗?

以全球电商巨头亚马逊为例&#xff0c;其风控技术的进化堪称一部永不停歇的“升级史”。然而&#xff0c;令人遗憾的是&#xff0c;不少卖家和测评服务商却依旧沉浸在过去的“舒适区”&#xff0c;过度依赖指纹浏览器、luminati等传统技术手段。这些曾经行之有效的工具&#xf…

module.noParse(跳过指定文件的依赖解析)

1. 说明 module.noParse 是 Webpack 的一个配置项&#xff0c;用于跳过对指定模块的解析。通过忽略某些文件的依赖分析&#xff0c;可以提升构建速度&#xff0c;尤其适用于处理大型、独立的第三方库 2. 使用配置 webpakc.config.js const path require(path); module.exp…

什么是爬虫?——从技术原理到现实应用的全面解析 V

什么是爬虫?——从技术原理到现实应用的全面解析 V 二十一、云原生爬虫架构设计 21.1 无服务器爬虫(AWS Lambda) # lambda_function.py import boto3 import requests from bs4 import BeautifulSoups3 = boto3.client(s3)def lambda_handler(event, context):# 抓取目标…

Web渗透之系统入侵与提权维权

渗透测试步骤 信息收集 搜集一些IP地址以及对应的端口开放情况&#xff0c;看看是否有80、3306、22等等端口开放&#xff0c;以及操作系统和版本号&#xff0c;同时也要扫描可能存在的漏洞 漏洞利用 建立据点 漏洞利用成功后&#xff0c;通常会在目标机上获得一个webshell&…