博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] 4Sum
阅读量:6773 次
发布时间:2019-06-26

本文共 3073 字,大约阅读时间需要 10 分钟。

Well, this problem has a O(n^3) solution similar to 3Sum. That is, fix two elements nums[i] and nums[j] (i < j) and search in the remaining array for two elements that sum to the target - nums[i] - nums[j]. Since i and j both have O(n) possible values and searching in the remaining array for two elements (just like 3Sum that fixes one and search for two other) has O(n) complexity using two pointers (left and right), the total time complexity is O(n^3).

The code is as follows, which should explain itself.

1     vector
> fourSum(vector
& nums, int target) { 2 sort(nums.begin(), nums.end()); 3 vector
> res; 4 for (int i = 0; i < (int)nums.size() - 3; i++) { 5 for (int j = i + 1; j < (int)nums.size() - 2; j++) { 6 int left = j + 1, right = nums.size() - 1; 7 while (left < right) { 8 int temp = nums[i] + nums[j] + nums[left] + nums[right]; 9 if (temp == target) {10 vector
sol(4);11 sol[0] = nums[i];12 sol[1] = nums[j];13 sol[2] = nums[left];14 sol[3] = nums[right];15 res.push_back(sol);16 while (left < right && nums[left] == sol[2]) left++;17 while (left < right && nums[right] == sol[3]) right--;18 }19 else if (temp < target) left++;20 else right--;21 }22 while (j + 1 < (int)nums.size() - 2 && nums[j + 1] == nums[j]) j++;23 }24 while (i + 1 < (int)nums.size() - 3 && nums[i + 1] == nums[i]) i++;25 }26 return res;27 }

In fact, there is also an O(n^2logn) solution by storing all the possible sum of a pair of elements in nums first. There are such solutions in , and . You may refer to them. One thing to mention is that all these solutions are slower than the above O(n^3) solution on the OJ :)

Personally I like solution 3 and rewrite it below.

1     vector
> fourSum(vector
& nums, int target) { 2 sort(nums.begin(), nums.end()); 3 unordered_map
> > mp; 4 for (int i = 0; i < nums.size(); i++) 5 for (int j = i + 1; j < nums.size(); j++) 6 mp[nums[i] + nums[j]].push_back(make_pair(i, j)); 7 vector
> res; 8 for (int i = 0; i < (int)nums.size() - 3; i++) { 9 if (i && nums[i] == nums[i - 1]) continue;10 for (int j = i + 1; j < (int)nums.size() - 2; j++) {11 if (j > i + 1 && nums[j] == nums[j - 1]) continue;12 int remain = target - nums[i] - nums[j];13 if (mp.find(remain) != mp.end()) {14 for (auto itr = mp[remain].begin(); itr != mp[remain].end(); itr++) {15 int k = (*itr).first, l = (*itr).second;16 if (k > j) {17 vector
ans(4);18 ans[0] = nums[i];19 ans[1] = nums[j];20 ans[2] = nums[k];21 ans[3] = nums[l];22 if (res.empty() || ans != res.back())23 res.push_back(ans);24 }25 }26 }27 }28 }29 return res;30 }

转载于:https://www.cnblogs.com/jcliBlogger/p/4567868.html

你可能感兴趣的文章
我的Linux生涯之开机挂载的一些东西
查看>>
Mybatis入门
查看>>
全球性现象: BYOD
查看>>
一致性hash算法
查看>>
2012年1月1日
查看>>
Java && Hibernate validator 校验
查看>>
系统启动流程
查看>>
Maven scope含义的说明
查看>>
VSAN 第 10 部分 – 实时更改虚拟机存储策略
查看>>
曾国藩家书
查看>>
哈哈!
查看>>
Maven私服不能自动通过内部nexus下载远程仓库的jar
查看>>
抽象类
查看>>
给《Mahout in action》作者的一封信
查看>>
去除inline-block元素间间距的N种方法
查看>>
初到贵网,留下第一个脚印
查看>>
oracle分割字符串后以单列多行展示
查看>>
macOS上flutter开发环境搭建填坑过程(续)
查看>>
struts2_自我总结
查看>>
【MongoDB for Java】Java操作MongoDB
查看>>