当前位置: 首页> 娱乐> 明星 > 淘宝店铺如何推广_网站建设是_阿里云com域名注册_深圳百度seo整站

淘宝店铺如何推广_网站建设是_阿里云com域名注册_深圳百度seo整站

时间:2025/7/13 0:43:34来源:https://blog.csdn.net/qq_51352130/article/details/142306753 浏览次数:0次
淘宝店铺如何推广_网站建设是_阿里云com域名注册_深圳百度seo整站

文章目录

  • 题目介绍
  • 解法

题目介绍

在这里插入图片描述

解法

思路分析:
在这里插入图片描述

先将数组nums排序;然后用一层for循环,i从下标0的地方开始,同时定一个下标left 定义在i+1的位置上,定义下标right 在数组结尾的位置上,双指针 left和right交替向中间移动,记录对于每个固定指针 i的所有满足 nums[i] + nums[left] + nums[right] == 0 的 left,right 组合:

  1. 当 i > 0且nums[i] == nums[i - 1]时即跳过此元素nums[i]:因为已经将 nums[i - 1] 的所有组合加入到结果中,本次双指针搜索只会得到重复组合。
  2. left,right 分设在数组索引 (i,len(nums)) 两端,当left < right时循环计算s = nums[i] + nums[left] + nums[right],并按照以下规则执行双指针移动:
    1. 当s < 0时,left += 1并跳过所有重复的nums[left];
    2. 当s > 0时,right -= 1并跳过所有重复的nums[right];
    3. 当s == 0时,记录组合[k, left, rightj]至res,执行left += 1和right -= 1并跳过所有重复的nums[left]和nums[right],防止记录到重复组合。

代码如下:

class Solution {public List<List<Integer>> threeSum(int[] nums) {Arrays.sort(nums);List<List<Integer>> ans = new ArrayList<>();int n = nums.length;for (int i = 0; i < n - 2; i++) {int x = nums[i];if (i > 0 && x == nums[i - 1]) continue; // 跳过重复数字int l = i + 1;int r = n - 1;while (l < r) {int sum = x + nums[l] + nums[r]; //三个数之和if (sum > 0) {r--;} else if (sum < 0) {l++;} else {ans.add(List.of(nums[i], nums[l], nums[r]));l++; r--;while (l < r && nums[l] == nums[l - 1]){l++; // 跳过重复数字}         while (r > l && nums[r] == nums[r + 1]){r--;// 跳过重复数字} }}}return ans;}
}

法二:
每次添加的时候先判断一下原列表里是否包含,但是会导致超出时间限制。

class Solution {public List<List<Integer>> threeSum(int[] nums) {Arrays.sort(nums);List<List<Integer>> ans = new ArrayList<>();int n = nums.length;for (int i = 0; i < n - 2; i++) {int x = nums[i];int l = i + 1;int r = n - 1;while (l < r) {int sum = x + nums[l] + nums[r]; //三个数之和if (sum > 0) {r--;} else if (sum < 0) {l++;} else {if( !ans.contains(List.of(x, nums[l], nums[r]))){ans.add(List.of(x, nums[l], nums[r]));}                   l++; r--;}}}return ans;}
}
关键字:淘宝店铺如何推广_网站建设是_阿里云com域名注册_深圳百度seo整站

版权声明:

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

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

责任编辑: