leetcode: 2Sum/3Sum/3SumClosest/4Sum系列问题
2013-03-19 19:49
399 查看
leetcode(http://leetcode.com/onlinejudge)上有好几道关于数组中几个数据和为target的题目。恰好正在看剑指offer中“和为s的两个数组这章”,据此思想,leetcode上的三道题目都被我解决了。总结一下。
1.twoSum:输入一个递增数组和一个数字s,在数组中查找两个数使得它们的和正好是s。
既然题目中已经提到了“递增数组”,那么肯定不会暴力了。因此肯定有<O(n*n)的办法了。
算法:最初我们找到数组的第一个数字和最后一个数字。当两个数字的和大于输入的数字时,把较大的数字往前移动;当两个数字的和小于数字时,把较小的数字往后移动;当相等时,打完收工。这样扫描的顺序是从数组的两端向数组的中间扫描。
链接:http://zhedahht.blog.163.com/blog/static/2541117420072143251809/
2.threeSum: Given an array S of n integers,
are there elements a, b, c in S such
that a + b + c =
0? Find all unique triplets in the array which gives the sum of zero.
Note:
Elements in a triplet (a,b,c)
must be in non-descending order. (ie, a ? b ? c)
The solution set must not contain duplicate triplets.
有了twoSum的启发,threeSum所有做的事,只需加上排序,和一层循环。经测试AC。
an array S of n integers,
find three integers in S such
that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
思路和threeSum几乎一样,只是查找条件有所变化而已。代码更简洁了。
Given an array S of n integers,
are there elements a, b, c,
and d in S such
that a + b + c + d =
target? Find all unique quadruplets in the array which gives the sum of target.
Note:
Elements in a quadruplet (a,b,c,d)
must be in non-descending order. (ie, a ? b ? c ? d)
The solution set must not contain duplicate quadruplets.
再多两层循环,此时已经是n立方的时间效率了,尝试了一下,居然也AC了。不知道还有没有更高效的算法。
1.twoSum:输入一个递增数组和一个数字s,在数组中查找两个数使得它们的和正好是s。
既然题目中已经提到了“递增数组”,那么肯定不会暴力了。因此肯定有<O(n*n)的办法了。
算法:最初我们找到数组的第一个数字和最后一个数字。当两个数字的和大于输入的数字时,把较大的数字往前移动;当两个数字的和小于数字时,把较小的数字往后移动;当相等时,打完收工。这样扫描的顺序是从数组的两端向数组的中间扫描。
链接:http://zhedahht.blog.163.com/blog/static/2541117420072143251809/
2.threeSum: Given an array S of n integers,
are there elements a, b, c in S such
that a + b + c =
0? Find all unique triplets in the array which gives the sum of zero.
Note:
Elements in a triplet (a,b,c)
must be in non-descending order. (ie, a ? b ? c)
The solution set must not contain duplicate triplets.
有了twoSum的启发,threeSum所有做的事,只需加上排序,和一层循环。经测试AC。
import java.util.ArrayList; import java.util.Arrays; public class ThreeSumSolution2 { private ArrayList<ArrayList<Integer>> list; public ArrayList<ArrayList<Integer>> threeSum(int[] num) { list = new ArrayList<ArrayList<Integer>>(); Arrays.sort(num); int i = 0; for (i = 0; i <= num.length - 3; i++) { if (i != 0 && num[i] == num[i - 1]) { continue; } judgeAndPut(num, i, i + 1, num.length - 1); } return list; } private void judgeAndPut(int[] num, int i, int p, int q) { while (p < q) { if (num[p] + num[q] < -num[i]) { p++; } else if (num[p] + num[q] > -num[i]){ q--; } else if (num[p] + num[q] == -num[i]) { ArrayList<Integer> tmpList = new ArrayList<Integer>(); tmpList.add(num[i]); tmpList.add(num[p]); tmpList.add(num[q]); list.add(tmpList); p++; q--; while (p < q && num[p] == num[p - 1]) { p++; } while (p < q && num[q] == num[q + 1]) { q--; } } } } public static void main(String[] args) { int num[] = {-1,0,1,2,-1,-4}; System.out.println(new ThreeSumSolution2().threeSum(num)); } }3. threeSumClosest: Given
an array S of n integers,
find three integers in S such
that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
思路和threeSum几乎一样,只是查找条件有所变化而已。代码更简洁了。
import java.util.Arrays; public class ThreeSumClosest { private int closest; private boolean needInit; public int threeSumClosest(int[] num, int target) { closest = 0; needInit = true; Arrays.sort(num); int i = 0; for (i = 0; i <= num.length - 3; i++) { if (i != 0 && num[i] == num[i - 1]) { continue; } judgeAndPut(num, i, i + 1, num.length - 1, target); } return closest; } private void judgeAndPut(int[] num, int i, int p, int q, int target) { while (p < q) { int sum = num[i] + num[p] + num[q]; if (needInit || Math.abs(sum - target) < Math.abs(closest - target)) { closest = sum; } needInit = false; if (sum <= target) { p++; while (p < q && num[p] == num[p - 1]) { p++; } } else if (sum > target){ q--; while (p < q && num[q] == num[q + 1]) { q--; } } } } public static void main(String[] args) { int num[] = {0,1,2}; System.out.println(new ThreeSumClosest().threeSumClosest(num, 3)); } }4. fourSum:
Given an array S of n integers,
are there elements a, b, c,
and d in S such
that a + b + c + d =
target? Find all unique quadruplets in the array which gives the sum of target.
Note:
Elements in a quadruplet (a,b,c,d)
must be in non-descending order. (ie, a ? b ? c ? d)
The solution set must not contain duplicate quadruplets.
再多两层循环,此时已经是n立方的时间效率了,尝试了一下,居然也AC了。不知道还有没有更高效的算法。
import java.util.ArrayList; import java.util.Arrays; public class FourSum { private ArrayList<ArrayList<Integer>> list; public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) { list = new ArrayList<ArrayList<Integer>>(); Arrays.sort(num); int i = 0; int j = 0; for (i = 0; i <= num.length - 4; i++) { if (i != 0 && num[i] == num[i - 1]) { continue; } for (j = i + 1; j <= num.length - 3; j++) { if (j != i + 1 && num[j] == num[j - 1]) { continue; } judgeAndPut(num, i, j, j + 1, num.length - 1, target); } } return list; } private void judgeAndPut(int[] num, int i, int j, int p, int q, int target) { while (p < q) { int sum = num[i] + num[j] + num[p] + num[q]; if (sum < target) { p++; } else if (sum > target){ q--; } else if (sum == target) { ArrayList<Integer> tmpList = new ArrayList<Integer>(); tmpList.add(num[i]); tmpList.add(num[j]); tmpList.add(num[p]); tmpList.add(num[q]); list.add(tmpList); p++; q--; while (p < q && num[p] == num[p - 1]) { p++; } while (p < q && num[q] == num[q + 1]) { q--; } } } } public static void main(String[] args) { int num[] = {-1,0,1,0,-2,2}; System.out.println(new FourSum().fourSum(num, 0)); } }
相关文章推荐
- [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence
- [LeetCode系列]3元素最近和问题的O(n^2)解法
- [LeetCode系列]N皇后问题递归解法 -- 位操作方式
- [LeetCode系列]BST有效性确定问题[前序遍历]
- LeetCode 笔记系列 14 N-Queen II [思考的深度问题]
- [LeetCode系列]子集枚举问题[有重复元素]
- [LeetCode] Invert Binary Tree - 二叉树翻转系列问题
- leetcode题目 反转链表系列问题
- “全排列”问题系列(一)[LeetCode] - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题
- leetcode题目 寻找和为SUM的集合系列问题
- [LeetCode系列] 二叉树最大深度求解问题(C++递归解法)
- [LeetCode系列] K节点倒序问题迭代解法
- [LeetCode系列] 变序词查找问题(Anagrams)
- 【Leetcode】Single Number 系列问题
- [LeetCode] Number of 1 Bits & Reverse Integer - 整数问题系列
- 分配糖果问题——Leetcode系列(十五)
- Best Time to Buy and Sell Stock 系列问题(leetcode)
- leetcode Course Schedule系列问题,拓补排序,有向无环图
- [LeetCode系列] 双单链表共同节点搜索问题
- 子集系列(一) 传统subset 问题,例 [LeetCode] Subset, Subset II, Bloomberg 的一道面试题