mirror of
https://github.com/prdlk/leetcode.git
synced 2026-09-16 23:16:26 +00:00
feat(problems): add medium array problem solutions for 3Sum, Two Sum II, and Subarray Sum Equals K
This commit is contained in:
@@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* 15. 3Sum
|
||||||
|
* Difficulty: Medium
|
||||||
|
* https://leetcode.com/problems/3sum/
|
||||||
|
*
|
||||||
|
* ──────────────────────────────────────────────────
|
||||||
|
*
|
||||||
|
* Given an integer array nums, return all the triplets [nums[i],
|
||||||
|
* nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] +
|
||||||
|
* nums[j] + nums[k] == 0.
|
||||||
|
*
|
||||||
|
* Notice that the solution set must not contain duplicate triplets.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Example 1:
|
||||||
|
*
|
||||||
|
* Input: nums = [-1,0,1,2,-1,-4]
|
||||||
|
* Output: [[-1,-1,2],[-1,0,1]]
|
||||||
|
* Explanation:
|
||||||
|
* nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
|
||||||
|
* nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
|
||||||
|
* nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
|
||||||
|
* The distinct triplets are [-1,0,1] and [-1,-1,2].
|
||||||
|
* Notice that the order of the output and the order of the triplets
|
||||||
|
* does not matter.
|
||||||
|
*
|
||||||
|
* Example 2:
|
||||||
|
*
|
||||||
|
* Input: nums = [0,1,1]
|
||||||
|
* Output: []
|
||||||
|
* Explanation: The only possible triplet does not sum up to 0.
|
||||||
|
*
|
||||||
|
* Example 3:
|
||||||
|
*
|
||||||
|
* Input: nums = [0,0,0]
|
||||||
|
* Output: [[0,0,0]]
|
||||||
|
* Explanation: The only possible triplet sums up to 0.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Constraints:
|
||||||
|
*
|
||||||
|
* • 3 <= nums.length <= 3000
|
||||||
|
*
|
||||||
|
* • -10^5 <= nums[i] <= 10^5
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number[]} nums
|
||||||
|
* @return {number[][]}
|
||||||
|
*/
|
||||||
|
var threeSum = function(nums) {
|
||||||
|
|
||||||
|
};
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
/*
|
||||||
|
* 167. Two Sum II - Input Array Is Sorted
|
||||||
|
* Difficulty: Medium
|
||||||
|
* https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
|
||||||
|
*
|
||||||
|
* ──────────────────────────────────────────────────
|
||||||
|
*
|
||||||
|
* Given a 1-indexed array of integers numbers that is already sorted in
|
||||||
|
* non-decreasing order, find two numbers such that they add up to a
|
||||||
|
* specific target number. Let these two numbers be numbers[index1] and
|
||||||
|
* numbers[index2] where 1 <= index1 < index2 <= numbers.length.
|
||||||
|
*
|
||||||
|
* Return the indices of the two numbers index1 and index2, each
|
||||||
|
* incremented by one, as an integer array [index1, index2] of length 2.
|
||||||
|
*
|
||||||
|
* The tests are generated such that there is exactly one solution. You
|
||||||
|
* may not use the same element twice.
|
||||||
|
*
|
||||||
|
* Your solution must use only constant extra space.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Example 1:
|
||||||
|
*
|
||||||
|
* Input: numbers = [2,7,11,15], target = 9
|
||||||
|
* Output: [1,2]
|
||||||
|
* Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 =
|
||||||
|
* 2. We return [1, 2].
|
||||||
|
*
|
||||||
|
* Example 2:
|
||||||
|
*
|
||||||
|
* Input: numbers = [2,3,4], target = 6
|
||||||
|
* Output: [1,3]
|
||||||
|
* Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 =
|
||||||
|
* 3. We return [1, 3].
|
||||||
|
*
|
||||||
|
* Example 3:
|
||||||
|
*
|
||||||
|
* Input: numbers = [-1,0], target = -1
|
||||||
|
* Output: [1,2]
|
||||||
|
* Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2
|
||||||
|
* = 2. We return [1, 2].
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Constraints:
|
||||||
|
*
|
||||||
|
* • 2 <= numbers.length <= 3 * 10^4
|
||||||
|
*
|
||||||
|
* • -1000 <= numbers[i] <= 1000
|
||||||
|
*
|
||||||
|
* • numbers is sorted in non-decreasing order.
|
||||||
|
*
|
||||||
|
* • -1000 <= target <= 1000
|
||||||
|
*
|
||||||
|
* • The tests are generated such that there is exactly one solution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number[]} numbers
|
||||||
|
* @param {number} target
|
||||||
|
* @return {number[]}
|
||||||
|
*/
|
||||||
|
var twoSum = function(numbers, target) {
|
||||||
|
let i = 0, j = numbers.length - 1;
|
||||||
|
|
||||||
|
while (i < j) {
|
||||||
|
const curr = numbers[i] + numbers[j];
|
||||||
|
if (curr === target){
|
||||||
|
return [i + 1, j + 1];
|
||||||
|
}else{
|
||||||
|
if (curr < target){
|
||||||
|
i++;
|
||||||
|
}else{
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
};
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* 560. Subarray Sum Equals K
|
||||||
|
* Difficulty: Medium
|
||||||
|
* https://leetcode.com/problems/subarray-sum-equals-k/
|
||||||
|
*
|
||||||
|
* ──────────────────────────────────────────────────
|
||||||
|
*
|
||||||
|
* Given an array of integers nums and an integer k, return the total
|
||||||
|
* number of subarrays whose sum equals to k.
|
||||||
|
*
|
||||||
|
* A subarray is a contiguous non-empty sequence of elements within an
|
||||||
|
* array.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Example 1:
|
||||||
|
*
|
||||||
|
* Input: nums = [1,1,1], k = 2
|
||||||
|
* Output: 2
|
||||||
|
*
|
||||||
|
* Example 2:
|
||||||
|
*
|
||||||
|
* Input: nums = [1,2,3], k = 3
|
||||||
|
* Output: 2
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Constraints:
|
||||||
|
*
|
||||||
|
* • 1 <= nums.length <= 2 * 10^4
|
||||||
|
*
|
||||||
|
* • -1000 <= nums[i] <= 1000
|
||||||
|
*
|
||||||
|
* • -10^7 <= k <= 10^7
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number[]} nums
|
||||||
|
* @param {number} k
|
||||||
|
* @return {number}
|
||||||
|
*/
|
||||||
|
var subarraySum = function(nums, k) {
|
||||||
|
const map = new Map();
|
||||||
|
map.set(0, 1);
|
||||||
|
|
||||||
|
let sum = 0;
|
||||||
|
let count = 0;
|
||||||
|
|
||||||
|
for (let n of nums){
|
||||||
|
sum += n;
|
||||||
|
|
||||||
|
if(map.has(sum - k)){
|
||||||
|
count += map.get(sum - k);
|
||||||
|
}
|
||||||
|
|
||||||
|
map.set(sum, (map.get(sum) || 0) + 1);
|
||||||
|
}
|
||||||
|
return count
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user