/*
* @lc app=leetcode.cn id=18 lang=javascript
*
* [18] 四数之和
*/
// @lc code=start
/**
* @param {number[]} nums
* @param {number} target
* @return {number[][]}
*/
var fourSum = function (nums, target) {
const result = [];
nums.sort((a, b) => {
return a - b;
});
for (let i = 0; i < nums.length - 3; i++) {
for (let j = i + 1; j < nums.length - 2; j++) {
let low = j + 1;
let high = nums.length - 1;
while (low < high) {
const sum = nums[i] + nums[j] + nums[low] + nums[high];
if (sum === target) {
result.push([nums[i], nums[j], nums[low], nums[high]]);
while (nums[low] === nums[low + 1]) low++;
while (nums[high] === nums[high - 1]) high--;
low++;
high--;
} else if (sum < target) {
low++;
} else {
high--;
}
}
while (nums[j] === nums[j + 1]) j++;
}
while (nums[i] === nums[i + 1]) i++;
}
return result;
};
// @lc code=end
[18] 四数之和
最新推荐文章于 2024-06-06 11:58:07 发布