using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num int整型一维数组 * @return int整型二维数组 */ public List<List<int>> threeSum (List<int> num) { num.Sort(); List<List<int>> res = new List<List<int>>(); for(int i = 0; i < num.Count - 2; ){ int left = i + 1; int right = num.Count - 1; while(left < right){ int cur = num[i] + num[left] + num[right]; if(cur > 0) right--; else if(cur < 0) left++; else{ List<int> newres = new List<int>{num[i], num[left], num[right]}; newres.Sort(); res.Add(newres); int newleft = left + 1; while(newleft < right && num[newleft] == num[left]) newleft++; left = newleft; } } int newi = i + 1; while(newi < num.Count - 2 && num[newi] == num[i]) newi++; i = newi; } return res; } }