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.pswp.cn/pingmian/78332.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/78332.shtml
英文地址,請注明出處:http://en.pswp.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&…