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

数组练习(一)

1.盛最多水的容器https://leetcode.cn/problems/container-with-most-water/

package 盛水问题;import java.util.ArrayList;
import java.util.List;public class Test01 {public static void main(String[] args) {int[] arr = {1,1};System.out.println(new Test01().maxArea(arr));}public int maxArea(int[]  height){// 左int left = 0;// 右int right = height.length - 1;int max = 0;for (;left<right;){int minHeight = Math.min(height[left], height[right]);int width = right-left;int area = minHeight * width;max = Math.max(max,area);if(height[left] < height[right]){left++;}else{right--;}}return max;}
}

. 三数之和https://leetcode.cn/problems/3sum/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ThreeSum {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);
        
        for (int i = 0; i < nums.length - 2; i++) {
     
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            
                      int left = i + 1;
            int right = nums.length - 1;
            
            while (left < right) {
                int sum = nums[i] + nums[left] + nums[right];
                
                if (sum == 0) {
              
                    result.add(Arrays.asList(nums[i], nums[left], nums[right]));
                    
                   
                    while (left < right && nums[left] == nums[left + 1]) {
                        left++;
                    }
         
                    while (left < right && nums[right] == nums[right - 1]) {
                        right--;
                    }
                    
                    // 移动双指针
                    left++;
                    right--;
                } else if (sum < 0) {
           
                    left++;
                } else {
                    right--;
                }
            }
        }
        
        return result;
    }

    public static void main(String[] args) {
        ThreeSum solution = new ThreeSum();
       
        // 测试用例1
        int[] nums1 = {-1, 0, 1, 2, -1, -4};
        System.out.println("测试用例1结果: " + solution.threeSum(nums1));
   
    }
}
 

http://www.xdnf.cn/news/1275625.html

相关文章:

  • vuhub drippingblues靶场攻略
  • #4:MinIO分片上传和集群部署
  • 攻击实验(ARP欺骗、MAC洪范、TCP SYN Flood攻击、DHCP欺骗、DHCP饿死)
  • 安全运维的核心
  • C语言——深入理解指针(二)
  • 【递归、搜索与回溯算法】递归算法
  • Ollama+Deepseek+Docker+RAGFlow打造自己的私人AI知识库
  • 计算机网络:超网即路由聚合一定需要连续的IP地址吗?
  • 秋招春招实习百度笔试百度管培生笔试题库百度非技术岗笔试|笔试解析和攻略|题库分享
  • RabbitMQ面试精讲 Day 19:网络调优与连接池管理
  • Spring Boot 注解详解:@RequestMapping 的多种用法
  • 十、Linux Shell脚本:流程控制语句
  • Day41--动态规划--121. 买卖股票的最佳时机,122. 买卖股票的最佳时机 II,123. 买卖股票的最佳时机 III
  • 网闸技术解析:如何实现对国产数据库(达梦/金仓)的深度支持
  • 我如何从安全运维逆袭成企业CSO
  • WiFi原理与WiFi安全
  • 【软考中级网络工程师】知识点之 IPv6 全解析
  • 基于python高校固定资产管理系统
  • 【在线五子棋对战】十二、http请求处理
  • 【经典算法】二叉树最小深度详解:递归解法与可视化分析
  • 【自用】JavaSE--IO流(二)--缓冲流、转换流、打印流、数据流、序列化流、IO框架
  • Redis 数据类型和单线程模型补充
  • Spring的三层架构及其各个层用到注解详细解释。
  • reuse: for booting my spring project with mvn in Windows command line
  • 基于 InfluxDB 的服务器性能监控系统实战(三)
  • Ubuntu 安装 Elasticsearch
  • Elasticsearch 搜索模板(Search Templates)把“可配置查询”装进 Mustache
  • 人工智能-python-机器学习-决策树与集成学习:决策树分类与随机森林
  • 深入浅出DBSCAN:基于密度的聚类算法详解与Python实战
  • redis集群-本地环境