您的位置:首页 > 房产 > 建筑 > 今日头条新闻10条军事_凡科互动自助解封没用_西安seo代理_电商平台推广公司

今日头条新闻10条军事_凡科互动自助解封没用_西安seo代理_电商平台推广公司

2025/4/26 14:02:15 来源:https://blog.csdn.net/qq_59463944/article/details/147322613  浏览:    关键词:今日头条新闻10条军事_凡科互动自助解封没用_西安seo代理_电商平台推广公司
今日头条新闻10条军事_凡科互动自助解封没用_西安seo代理_电商平台推广公司

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com