Today in the Leetcode brush problem encountered need to the two-dimensional array of array items to go to weight, the topic link: Three number of the sum, given an array, take out all three number added equals 0 of the non-repeating items
I gave a solution, but time complexity did not pass, and later change:
var threesum = function (nums) { var len=nums.length; var arr=[]; var res=[]; var result=[]; Take out all three sum of 0 items in the array for (Var i=0;i<len;i++) {for (var j=i+1;j<len;j++) {for (Var k=j+1;k<len;k+ +) { if (nums[i]+nums[j]+nums[k]===0) { arr.push (nums[i],nums[j],nums[k]); Res.push (arr); Arr=[] ; }}} Sort Res.map (item=>{ item.sort (function (v1,v2) { if (v1<v2) { return-1; } else{ return 1;}) }) Go to Heavy for (Var i=0;i<res.length;i++) {for (var j=i+1;j<res.length;j++) { if (res[i][0]===res[j][ 0] && res[i][1]===res[j][1] && res[i][2]===res[j][2]) { j=++i; } } Result.push (Res[i]); } Console.log (Result) };
Two-dimensional array item de-weight in JS