// 给定 nums = [2, 7, 11, 15], target = 9 // 因为 nums[0] + nums[1] = 2 + 7 = 9 // 所以返回 [0, 1] function towSum(nums, target) { nums.sort(); let l = 0, r = nums.length - 1; var str = ""; while(l <= r) { if(nums[l] + nums[r] == target) { str = "["+l + "," + r+"]"; break; } else if(nums[l] + nums[r] < target) { l++; } else { r --; } } return str; } const nums = [1, 2, 3, 4, 5, 6]; const target = 11; console.log(towSum(nums, target));