「每日LeetCode」2023年3月14日

本文最后更新于:2023年3月19日 晚上

一波 XX 之和

两数之和

Category Difficulty Likes Dislikes
algorithms Easy (52.32%) 13641 -

Tags
Companies
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 **target 的那 **两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。

示例 1:
输入:nums = [2,7,11,15], target = 9 输出:[0,1] 解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例 2:
输入:nums = [3,2,4], target = 6 输出:[1,2]
示例 3:
输入:nums = [3,3], target = 6 输出:[0,1]

提示:

  • 2 <= nums.length <= 104
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
  • 只会存在一个有效答案

进阶:你可以想出一个时间复杂度小于 O(n2) 的算法吗?


Discussion | Solution

思路

按题意模拟即可

解答

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
* @lc app=leetcode.cn id=1 lang=javascript
*
* [1] 两数之和
*/

// @lc code=start
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function (nums, target) {
const map = {};
for (let i = 0; i < nums.length; i++) map[nums[i]] = i;

for (let i = 0; i < nums.length; i++) {
const num = nums[i];
const targetNum = target - num;
if (typeof map[targetNum] !== "undefined" && i !== map[targetNum]) {
return [i, map[targetNum]];
}
}
return null;
};
// @lc code=end

三数之和

Category Difficulty Likes Dislikes
algorithms Medium (34.48%) 4425 -

Tags
Companies
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 _a + b + c = _0 ?请你找出所有和为 0 且不重复的三元组。
注意:答案中不可以包含重复的三元组。

示例 1:
输入:nums = [-1,0,1,2,-1,-4] 输出:[[-1,-1,2],[-1,0,1]]
示例 2:
输入:nums = [] 输出:[]
示例 3:
输入:nums = [0] 输出:[]

提示:

  • 0 <= nums.length <= 3000
  • -105 <= nums[i] <= 105

Discussion | Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* @lc app=leetcode.cn id=15 lang=javascript
*
* [15] 三数之和
*/

// @lc code=start
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function (nums) {
if (!nums.length) return [];
nums.sort((a, b) => +a - +b);
if (nums.length < 3 || !nums || nums.at(0) > 0 || nums.at(-1) < 0) return [];
const res = [];
for (let i = 0; i < nums.length - 1; i++) {
let num = nums[i];
if (num > 0) break;

if (i > 0 && nums[i] === nums[i - 1]) continue;
let left = i + 1,
right = nums.length - 1;
while (left < right) {
const leftNum = nums[left],
rightNum = nums[right];
const sum = leftNum + rightNum + num;

if (sum === 0) {
res.push([num, leftNum, rightNum]);
while (left < right && nums[left] === nums[left + 1]) left++;
while (left < right && nums[right] === nums[right - 1]) right--;
left++;
right--;
} else if (sum < 0) {
while (left < right && nums[left] === nums[left + 1]) left++;
left++;
} else {
while (left < right && nums[right] === nums[right - 1]) right--;
right--;
}
}
}
return res;
};
// @lc code=end

四数之和

Category Difficulty Likes Dislikes
algorithms Medium (37.30%) 1539 -

Tags
Companies
给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复):

  • 0 <= a, b, c, d < n
  • a、b、c 和 d 互不相同
  • nums[a] + nums[b] + nums[c] + nums[d] == target

你可以按 任意顺序 返回答案 。

示例 1:
输入:nums = [1,0,-1,0,-2,2], target = 0 输出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
示例 2:
输入:nums = [2,2,2,2,2], target = 8 输出:[[2,2,2,2]]

提示:

  • 1 <= nums.length <= 200
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109

Discussion | Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* @lc app=leetcode.cn id=18 lang=javascript
*
* [18] 四数之和
*/

// @lc code=start
/**
* @param {number[]} nums
* @return {number[][]}
*/

const threeSum = function (nums, target) {
if (!nums || nums.length.length < 3) return [];

const res = [];
for (let i = 0; i < nums.length - 1; i++) {
let num = nums[i];

if (i > 0 && nums[i] === nums[i - 1]) continue;
let left = i + 1,
right = nums.length - 1;
while (left < right) {
const leftNum = nums[left],
rightNum = nums[right];
const sum = leftNum + rightNum + num;

if (sum === target) {
res.push([num, leftNum, rightNum]);
while (left < right && nums[left] === nums[left + 1]) left++;
while (left < right && nums[right] === nums[right - 1]) right--;
left++;
right--;
} else if (sum < target) {
while (left < right && nums[left] === nums[left + 1]) left++;
left++;
} else {
while (left < right && nums[right] === nums[right - 1]) right--;
right--;
}
}
}
return res;
};

var fourSum = function (nums, target) {
if (!nums || nums.length.length < 4) return [];
nums.sort((a, b) => +a - +b);
const res = [];
for (let i = 0; i < nums.length - 1; i++) {
let num = nums[i];

if (i > 0 && nums[i] === nums[i - 1]) continue;

const targetNum = target - num;

const threeRes = threeSum(nums.slice(i + 1), targetNum);

if (!threeRes.length) continue;

for (const item of threeRes) {
res.push([num, ...item]);
}
}
return res;
};
// @lc code=end