242. 有效的字母异位词 - 力扣(LeetCode)
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function (s, t) {
const map = new Map()
for (let i = 0; i < s.length; i++) {
map.set(s[i], map.has(s[i]) ? map.get(s[i]) + 1 : 1)
}
for (let i = 0; i < t.length; i++) {
if (!map.has(t[i])) {
return false
}
map.set(t[i], map.get(t[i]) - 1)
if (map.get(t[i]) === 0) {
map.delete(t[i])
}
}
return map.size === 0
};
349. 两个数组的交集 - 力扣(LeetCode)
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[]}
*/
var intersection = function (nums1, nums2) {
const resSet = new Set()
const set = new Set()
nums1.forEach(item => set.add(item))
nums2.forEach(item => {
if (set.has(item)) {
resSet.add(item)
}
})
return Array.from(resSet)
};
202. 快乐数 - 力扣(LeetCode)
/**
* @param {number} n
* @return {boolean}
*/
var isHappy = function (n) {
const set = new Set()
while (true) {
const sum = String(n).split('').reduce((pre, cur) =>
pre + Number(cur) * Number(cur), 0)
if (sum === 1) {
return true
}
if (set.has(sum)) {
return false
}
set.add(sum)
n = sum
}
};
1. 两数之和 - 力扣(LeetCode)
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function (nums, target) {
const map = new Map()
for (let i = 0; i < nums.length; i++) {
if (map.has(target - nums[i])) {
return [i, map.get(target - nums[i])]
} else {
map.set(nums[i], i)
}
}
};
454. 四数相加 II - 力扣(LeetCode)
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @param {number[]} nums3
* @param {number[]} nums4
* @return {number}
*/
var fourSumCount = function (nums1, nums2, nums3, nums4) {
const map = new Map()
let res = 0
for (let i = 0; i < nums1.length; i++) {
for (let j = 0; j < nums2.length; j++) {
const sum = nums1[i] + nums2[j]
map.set(sum, map.has(sum) ? map.get(sum) + 1 : 1)
}
}
for (let i = 0; i < nums3.length; i++) {
for (let j = 0; j < nums4.length; j++) {
const sum = nums3[i] + nums4[j]
if (map.has(-sum)) {
res += map.get(-sum)
}
}
}
return res
};
383. 赎金信 - 力扣(LeetCode)
/**
* @param {string} ransomNote
* @param {string} magazine
* @return {boolean}
*/
var canConstruct = function (ransomNote, magazine) {
const map = new Map()
for (let i = 0; i < magazine.length; i++) {
map.set(magazine[i], map.has(magazine[i]) ? map.get(magazine[i]) + 1 : 1)
}
for (let i = 0; i < ransomNote.length; i++) {
if (!map.has(ransomNote[i])) {
return false
}
map.set(ransomNote[i], map.get(ransomNote[i]) - 1)
if (map.get(ransomNote[i]) === 0) {
map.delete(ransomNote[i])
}
}
return true
};
15. 三数之和 - 力扣(LeetCode)
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function (nums) {
const res = []
// 1. 排序
nums.sort((a, b) => a - b)
// 2. 多指针
for (let i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
return res
}
if (i !== 0 && nums[i] === nums[i - 1]) {
continue
}
let left = i + 1, right = nums.length - 1
while (left < right) {
const sum = nums[i] + nums[left] + nums[right]
if (sum > 0) {
right--
} else if (sum < 0) {
left++
} else {
res.push([nums[i], nums[left], nums[right]])
while (left < right && nums[left] === nums[left + 1]) {
left++
}
while (left < right && nums[right - 1] === nums[right]) {
right--
}
left++
right--
}
}
}
return res
};
18. 四数之和 - 力扣(LeetCode)
/**
* @param {number[]} nums
* @param {number} target
* @return {number[][]}
*/
var fourSum = function(nums, target) {
const res = []
// 1. 排序
nums.sort((a, b) => a- b)
// 多指针
for (let i = 0; i < nums.length; i++) {
if (i !== 0 && nums[i] === nums[i - 1]) {
continue
}
for (let j = i + 1; j < nums.length; j++) {
if (j !== i + 1 && nums[j] === nums[j - 1]) {
continue
}
let left = j + 1, right = nums.length - 1
while (left < right) {
const sum = nums[i] + nums[j] + nums[left] + nums[right]
if (sum > target) {
right--
} else if (sum < target) {
left++
} else {
res.push([nums[i], nums[j], nums[left], nums[right]])
while(left < right && nums[left] === nums[left + 1]) {
left++
}
while (left < right && nums[right - 1] === nums[right]) {
right--
}
left++
right--
}
}
}
}
return res
};