var threeSum = function(nums) {
nums = nums.sort((a, b) => a - b);
let arr = [];
for(let i = nums.length-1; i>1 && nums[i] + nums[i-1] + nums[i-2] >= 0; i--){
if(nums[i] === nums[i+1] || nums[i] + nums[1] + nums[0] > 0) continue;
const s = 0 - nums[i];
let l = 0, r = i - 1;
//while(l<r && nums[l]+nums[l+1]<=s && nums[r]+nums[r-1]>=s){
while(l<r){
if(nums[l] + nums[r] > s){
do{--r}while(nums[r]===nums[r+1])
}else if(nums[l] + nums[r] < s){
do{++l}while(nums[l]===nums[l-1])
}else{
arr.push([nums[l], nums[r], nums[i]]);
do{--r}while(nums[r]===nums[r+1]);
do{++l}while(nums[l]===nums[l-1])
}
}
}
return arr;
};
// 原始版本
var threeSum = function(nums) {
let result = [];
nums.sort(); // 因为只需要传出元素,不需要下标,因此可以破坏掉原有的数组
for (let i = 0; i < nums.length - 2; i++) { // 起码留3个数
let low = i + 1; // 避免和i取到同一个值
let high = nums.length - 1;
while (low < high) {
const ans = nums[low] + nums[high] + nums[i];
if (ans === 0) {
result.push([nums[low], nums[high], nums[i]]); // 找到了就推入
break;
} else if (ans < 0) {
low++;
} else if (ans > 0) {
high--;
}
}
}
return result;
};
var threeSum = function(nums) {
let result = [];
nums.sort(); // 因为只需要传出元素,不需要下标,因此可以破坏掉原有的数组
for (let i = 0; i < nums.length - 2; i++) { // 起码留3个数
let low = i + 1;
let high = nums.length - 1;
while (low < high) {
const ans = nums[low] + nums[high] + nums[i];
if (ans === 0) {
result.push([nums[low], nums[high], nums[i]]);
break;
} else if (ans < 0) {
while (nums[low] === nums[low + 1]) { // 增加跳过
low++;
}
} else if (ans > 0) {
while (nums[high] === nums[high - 1]) { // 增加跳过
high--;
}
}
}
while (nums[i] === nums[i + 1]) { // i也跳过
i++;
}
}
return result;
};
var threeSum = function(nums) {
...
// 正确排序
nums.sort((a, b) => a - b);
...
};
...
while (low < high) {
const ans = nums[low] + nums[high] + nums[i];
console.log("=test=>", nums[i], nums[low], nums[high], ans); // 打印
console.log("=num=>", i, low, high); // 打印
if (ans === 0) {
result.push([nums[low], nums[high], nums[i]]);
break;
} else if (ans < 0) {
while (nums[low] === nums[low + 1]) {
low++;
}
} else if (ans > 0) {
...
}
...
...
} else if (ans < 0) {
while (nums[low] === nums[low + 1]) {
low++;
}
low++; // 不同值也要变动low指针
} else if (ans > 0) {
...
...
if (ans === 0) {
result.push([nums[low], nums[high], nums[i]]);
while (nums[low] === nums[low + 1]) { // 两个指针一起移动
low++;
}
low++;
while (nums[high] === nums[high - 1]) {
high--;
}
high--;
} else if (ans < 0) {
}
...
var threeSum = function(nums) {
nums.sort((a, b) => a - b);
let result = [];
if (nums[0] > 0 || nums[nums.length - 1] < 0) {
return result;
}
for (
let i = 0;
i < nums.length - 2 && nums[i] + nums[i + 1] + nums[i + 2] <= 0; // 判断
i++
) {
let low = i + 1;
let high = nums.length - 1;
while (low < high) {
const ans = nums[low] + nums[high] + nums[i];
if (ans === 0) {
result.push([nums[low], nums[high], nums[i]]);
while (nums[low] === nums[low + 1]) {
low++;
}
low++;
while (nums[high] === nums[high - 1]) {
high--;
}
high--;
} else if (ans < 0) {
while (nums[low] === nums[low + 1]) {
low++;
}
low++;
} else if (ans > 0) {
while (nums[high] === nums[high - 1]) {
high--;
}
high--;
}
}
while (nums[i] === nums[i + 1]) {
i++;
}
}
return result;
};
var threeSum = function (nums) {
let res = [];
nums.sort((a, b) => a - b);
let size = nums.length;
if (nums[0] <= 0 && nums[size - 1] >= 0) {
let i = 0; // 区别1:自定义i
while (i < size - 2) {
if (nums[i] > 0) break; // 最左侧大于0,无解 // 区别2:break
let first = i + 1;
let last = size - 1;
while (first < last) {
if (nums[i] * nums[last] > 0) break; // 三数同符号,无解
let sum = nums[i] + nums[first] + nums[last];
if (sum === 0) {
res.push([nums[i], nums[first], nums[last]]);
}
if (sum <= 0) {
// 负数过小,first右移
while (nums[first] === nums[++first]) {} // 重复值跳过 //区别3:++
} else {
while (nums[last] === nums[--last]) {} // 重复值跳过
}
}
while (nums[i] === nums[++i]) {} // i重复值跳过
}
}
return res;
};
while (nums[low] === nums[++low]) {}
// 相当于
while (nums[low] === nums[low + 1]) {
low++;
}
low++;