标签:
public IList<IList<int>> ThreeSum(int[] nums) { if(nums == null || nums.Length < 3){ return new List<IList<int>>(); } var dic = new Dictionary<string, List<int>>(); var list = nums.OrderBy(x=>x).ToList(); var len = list.Count; for (var i = 0 ;i <= len - 3 ;i++){ var a = list[i]; var start = i+1; var end = len-1; while (start < end) { var b = list[start]; var c = list[end]; if (a+b+c == 0) { var v = new List<int>(){a,b,c}.OrderBy(x=>x).ToList(); var k = string.Join(",",v); if(!dic.ContainsKey(k)){ dic.Add(k,v); } start ++; end --; } else if (a+b+c > 0){ end --; } else{ start ++; } } } var ret = new List<IList<int>>(); foreach(var kv in dic){ ret.Add(kv.Value); } return ret; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/lan_liang/article/details/47258159