LeetCode 3sum
2014-09-29 11:06
197 查看
public class Solution { public List<List<Integer>> threeSum(int[] num) { List<List<Integer>> results = new ArrayList<List<Integer>>(); if (num == null || num.length < 3) { return results; } Arrays.sort(num); int prev = 0; for (int i = 0; i < num.length; i++) { if (i > 0 && num[i] == prev) { continue; } else { twoSumHelper(results, num, i); prev = num[i]; } } return results; } private void twoSumHelper(List<List<Integer>> results, int[] num, int index) { int firstNum = num[index]; int target = 0 - firstNum; int start = index + 1; int end = num.length - 1; while (start < end) { int secondNum = num[start]; int thirdNum = num[end]; int sum = secondNum + thirdNum; if (sum == target) { List<Integer> result = new ArrayList<Integer>(); result.add(firstNum); result.add(secondNum); result.add(thirdNum); results.add(result); int prevStart = num[start]; int prevEnd = num[end]; while (start < num.length && num[start] == prevStart) start++; while (end > 0 && num[end] == prevEnd) end--; } else if (sum < target) { start++; } else { end--; } } } }
需要注意的是 41 和 42 行的:
while (start < num.length && num[start] == prevStart) start++; while (end > 0 && num[end] == prevEnd) end--;
因为当 start 指针和 end 指针分别左移和右移的时候,需要跳过和之前一样的数字,否则:
Input: | [-2,0,0,2,2] |
Output: | [[-2,0,2],[-2,0,2]] |
Expected: | [[-2,0,2]] |
相关文章推荐
- 3Sum---LeetCode
- Leetcode 15. 3Sum
- [LeetCode]3Sum Closest
- LeetCode中3Sum(a+b+c=0)的解法与证明
- LeetCode : 3Sum Closest
- leetcode c++ 3sum
- Leetcode 3Sum
- LeetCode 15 3Sum(尺取法或哈希)
- leetcode-3Sum Closest
- [LeetCode] 3Sum的O(N^2)解法
- Leetcode 3Sum Closet
- 15. 3Sum Leetcode Python
- 【leetcode】15. 3Sum
- LeetCode 3sum
- leetcode 15. 3Sum
- LeetCode_3Sum Closest
- Leetcode -- 3Sum Closest
- [LeetCode] 3Sum 解题报告
- LeetCode题解——3Sum Closest
- leetcode 15. 3Sum