当前位置: 首页 > news >正文

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;}};}

相关文章:

  • C++入侵检测与网络攻防之暴力破解
  • 项目笔记1:通用 Service的常见方法
  • 通讯录完善版本(详细讲解+源码)
  • 什么是财务管理系统?一文看清其功能及作用!
  • 【AI落地应用实战】借助 Amazon Q 实现内容分发网络(CDN)CDK 构建的全流程实践
  • 腾讯一面面经:总结一下
  • 玉米产量遥感估产系统的开发实践(持续迭代与更新)
  • 《人月神话》50周年遇到AI-那些乐趣和苦恼(01-03)
  • CF-Hero:自动绕过CDN找真实ip地址
  • 计算机组成原理第二章 数据的表示和运算——2.1数制与编码
  • 当智驾成标配,车企暗战升级|2025上海车展
  • 软件技术专业
  • 云服务器和独立服务器的区别在哪
  • 问答页面支持拖拽和复制粘贴文件,MaxKB企业级AI助手v1.10.6 LTS版本发布
  • 算能BM1684升级为BM1688: tpu_mlir转换模型_SDK更新_代码修改_问题排查_代码调试
  • 【MySQL】3分钟解决MySQL深度分页问题
  • 一种专用车辆智能配电模块的设计解析:技术革新与未来展望
  • C#并行编程极大提升集合处理速度,再也没人敢说你程序性能差了!
  • 【信息系统项目管理师】高分论文:论成本管理与采购管理(信用管理系统)
  • 高校学子走进万物纵横:体验边缘计算前沿技术,共探产业创新未来
  • 《深化养老服务改革发展的大湾区探索》新书将于今年6月出版
  • 本周看啥|在电影院里听民谣,听摇滚,燥起来吧
  • 2025年全国贸易摩擦应对工作会议在京召开
  • 《2025职场人阅读报告》:超半数会因AI改变阅读方向
  • 鸿蒙智行八大车型亮相上海车展,余承东拉上三家车企老总“直播推销”
  • 商务部谈中欧汽车谈判进展