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

北京免费网站建设3天网站seo优化成为超级品牌

北京免费网站建设,3天网站seo优化成为超级品牌,广州天河建网站的公司,佛山疫情最新情况今天目录 LeetCode之路——15. 三数之和 分析: 官方题解: LeetCode之路——15. 三数之和 给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i ! j、i ! k 且 j ! k ,同时还满足 nums[i] nums[j] nu…

目录

LeetCode之路——15. 三数之和

分析:

官方题解:


LeetCode之路——15. 三数之和

给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != ji != kj != 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

分析:

借助1. 两数之和的思路,可以让nums[i] =a去遍历数组作为target。同时nums[i+1] =b继续遍历数组,找到nums[j]满足 a + b + nums[j] =0.

1.需要注意元素组去重。

2.需要注意数组边界。

class Solution {public List<List<Integer>> threeSum(int[] nums) {List<List<Integer>> res = new ArrayList<>();if (nums.length < 3) return res;Arrays.sort(nums); // 递增顺序for (int i = 0; i < nums.length - 2; i++) {if (nums[i] > 0) break;int first = nums[i]; // 取得a的值if (i > 0 && nums[i] == nums[i - 1]) continue; // 排序后,需要保证不重复Set<Integer> set = new HashSet<>();for (int j = i + 1; j < nums.length; j++) {int second = nums[j]; // 取得b的值int third = - (first + second);if (set.contains(third)) {res.add(new ArrayList<>(Arrays.asList(first,second,third)));while(j < nums.length - 1 && nums[j] == nums[j + 1]) j++;}set.add(second);}}return res;}
}
  • 时间复杂度:O(N^2)

  • 空间复杂度:O(N)

官方题解:
class Solution {public List<List<Integer>> threeSum(int[] nums) {int n = nums.length;Arrays.sort(nums);List<List<Integer>> ans = new ArrayList<List<Integer>>();// 枚举 afor (int first = 0; first < n; ++first) {// 需要和上一次枚举的数不相同if (first > 0 && nums[first] == nums[first - 1]) {continue;}// c 对应的指针初始指向数组的最右端int third = n - 1;int target = -nums[first];// 枚举 bfor (int second = first + 1; second < n; ++second) {// 需要和上一次枚举的数不相同if (second > first + 1 && nums[second] == nums[second - 1]) {continue;}// 需要保证 b 的指针在 c 的指针的左侧while (second < third && nums[second] + nums[third] > target) {--third;}// 如果指针重合,随着 b 后续的增加// 就不会有满足 a+b+c=0 并且 b<c 的 c 了,可以退出循环if (second == third) {break;}if (nums[second] + nums[third] == target) {List<Integer> list = new ArrayList<Integer>();list.add(nums[first]);list.add(nums[second]);list.add(nums[third]);ans.add(list);}}}return ans;}
}
​
作者:力扣官方题解
链接:https://leetcode.cn/problems/3sum/solutions/284681/san-shu-zhi-he-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 时间复杂度:O(N^2)

  • 空间复杂度:O(N)

http://www.hkea.cn/news/540346/

相关文章:

  • 在线做数据图的网站樱桃bt磁力天堂
  • 网站建设费的税率东莞公司网上推广
  • 上海设计公司排名前十宁波seo搜索优化费用
  • 如皋做网站公司com域名
  • 织梦做企业网站教程网络营销推广方案论文
  • 微信如何添加小程序二十条优化措施全文
  • 网站制作费可以做业务宣传费河北百度推广电话
  • wordpress日主题破解网站排名优化软件有哪些
  • 做公众号app 网站 app济南网站设计
  • 单位网站 单位网页 区别吗福州seo顾问
  • 专业做网站制作的公司百度地图网页版进入
  • 买卖网站域名骗局百度推广登陆
  • 石家庄大型网站设计公司手机怎么建网站
  • 政府网站图解怎么做百度关键词排名靠前
  • 天津做网站印标东莞网络推广排名
  • 设计一个外贸网站需要多少钱沈阳网站推广优化
  • 洗化行业做网站福州百度seo排名
  • 西安app网站开发项目腾讯域名注册官网
  • 网站开发的技术指标如何做网站搜索引擎优化
  • 建网站的要求老铁外链工具
  • wordpress有广告郑州seo优化大师
  • 企业网站推广的实验内容企业宣传网站
  • 如何开发高端市场宁波seo快速优化公司
  • 常用设计资源网站爱网站
  • 企业品牌网站营销关于seo的行业岗位有哪些
  • 群晖怎样做网站惠州网络营销
  • 怎么做网站内部链接的优化品牌营销推广代运营
  • 婚纱照网站制作搜索引擎优化搜索优化
  • 网站建设吉金手指专业13中国营销网站
  • 做销售在哪些网站注册好百度产品