本文共 789 字,大约阅读时间需要 2 分钟。
3Sum
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:
For example, given array S = {-1 0 1 2 -1 -4}, A solution set is: (-1, 0, 1) (-1, -1, 2)Java代码:
public class Solution { public List
> threeSum(int[] num) { if (num == null) return null; List
> result = new ArrayList
>(); int len = num.length; // Sort Arrays.sort(num); for (int i=0; i l = new ArrayList (); l.add(num[i]); l.add(num[j]); l.add(num[k]); result.add(l); // Bypass second and third elements int K = k; while (k
转载地址:http://anuni.baihongyu.com/