From fc04e9ec1ee596adf438224e82633142871b676b Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Sat, 22 May 2021 10:16:13 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0242.=E6=9C=89=E6=95=88?= =?UTF-8?q?=E7=9A=84=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8DJavaScrip?= =?UTF-8?q?t=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0242.有效的字母异位词.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 10939b0f..8aa0d171 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -140,6 +140,23 @@ func isAnagram(s string, t string) bool { } ``` +javaScript: + +```js +var isAnagram = function(s, t) { + const resSet = new Array(25).fill(0); + const base = "a".charCodeAt(); + for(const i of s) { + resSet[i.charCodeAt() - base]++; + } + for(const i of t) { + resSet[i.charCodeAt() - base]--; + // if(val < 0) return false; + } + return resSet.every(i => !i); +}; +``` + ## 相关题目 * 383.赎金信 From 139761a07729b450bad3ac9b4ee50ff1337c575e Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Sat, 22 May 2021 10:30:01 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0349.=20=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86JavaScript?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0349.两个数组的交集.md | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index b5116ee1..93d2c2d4 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -113,6 +113,34 @@ Python: Go: +javaScript: + +```js +/** + * @param {number[]} nums1 + * @param {number[]} nums2 + * @return {number[]} + */ +var intersection = function(nums1, nums2) { + // 根据数组大小交换操作的数组 + if(nums1.length < nums2.length) { + const _ = nums1; + nums1 = nums2; + nums2 = _; + } + const nums1Set = new Set(nums1); + const resSet = new Set(); + // for(const n of nums2) { + // nums1Set.has(n) && resSet.add(n); + // } + // 循环 比 迭代器快 + for(let i = nums2.length - 1; i >= 0; i--) { + nums1Set.has(nums2[i]) && resSet.add(nums2[i]); + } + return Array.from(resSet); +}; +``` + From 0a2679ea0be39e55b1ef3c7569e7db1740d79df7 Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Sat, 22 May 2021 11:23:48 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AC=AC454=E9=A2=98.?= =?UTF-8?q?=E5=9B=9B=E6=95=B0=E7=9B=B8=E5=8A=A0IIJavaScript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0454.四数相加II.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index ad928a3f..28db6a50 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -155,6 +155,38 @@ class Solution(object): Go: +javaScript: + +```js +/** + * @param {number[]} nums1 + * @param {number[]} nums2 + * @param {number[]} nums3 + * @param {number[]} nums4 + * @return {number} + */ +var fourSumCount = function(nums1, nums2, nums3, nums4) { + const twoSumMap = new Map(); + let count = 0; + + for(const n1 of nums1) { + for(const n2 of nums2) { + const sum = n1 + n2; + twoSumMap.set(sum, (twoSumMap.get(sum) || 0) + 1) + } + } + + for(const n3 of nums3) { + for(const n4 of nums4) { + const sum = n3 + n4; + count += (twoSumMap.get(0 - sum) || 0) + } + } + + return count; +}; +``` + From 7699a818b42a4cf517553b0cd99b1a3a8ed07f0a Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Sat, 22 May 2021 11:33:52 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0383.=20=E8=B5=8E=E9=87=91?= =?UTF-8?q?=E4=BF=A1JavaScript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0383.赎金信.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 527945fa..7ff28601 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -136,7 +136,7 @@ class Solution { ``` Python: -``` +```py class Solution(object): def canConstruct(self, ransomNote, magazine): """ @@ -167,6 +167,28 @@ class Solution(object): Go: +javaScript: + +```js +/** + * @param {string} ransomNote + * @param {string} magazine + * @return {boolean} + */ +var canConstruct = function(ransomNote, magazine) { + const strArr = new Array(25).fill(0), + base = "a".charCodeAt(); + for(const s of magazine) { + strArr[s.charCodeAt() - base]++; + } + for(const s of ransomNote) { + const index = s.charCodeAt() - base; + if(!strArr[index]) return false; + strArr[index]--; + } + return true; +}; +``` From 3be6a9dd2bb44a54721460f503a6606644902d98 Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Sat, 22 May 2021 13:38:18 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AC=AC15=E9=A2=98.=20?= =?UTF-8?q?=E4=B8=89=E6=95=B0=E4=B9=8B=E5=92=8CJavaScript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0015.三数之和.md | 53 +++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 96dc1ac3..8ee1d07b 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -256,6 +256,59 @@ func threeSum(nums []int)[][]int{ } ``` +javaScript: + +```js +/** + * @param {number[]} nums + * @return {number[][]} + */ + +// 循环内不考虑去重 +var threeSum = function(nums) { + const len = nums.length; + if(len < 3) return []; + nums.sort((a, b) => a - b); + const resSet = new Set(); + for(let i = 0; i < len - 2; i++) { + if(nums[i] > 0) break; + let l = i + 1, r = len - 1; + while(l < r) { + const sum = nums[i] + nums[l] + nums[r]; + if(sum < 0) { l++; continue }; + if(sum > 0) { r--; continue }; + resSet.add(`${nums[i]},${nums[l]},${nums[r]}`); + l++; + r--; + } + } + return Array.from(resSet).map(i => i.split(",")); +}; + +// 去重优化 +var threeSum = function(nums) { + const len = nums.length; + if(len < 3) return []; + nums.sort((a, b) => a - b); + const res = []; + for(let i = 0; i < len - 2; i++) { + if(nums[i] > 0) break; + // a去重 + if(i > 0 && nums[i] === nums[i - 1]) continue; + let l = i + 1, r = len - 1; + while(l < r) { + const sum = nums[i] + nums[l] + nums[r]; + if(sum < 0) { l++; continue }; + if(sum > 0) { r--; continue }; + res.push([nums[i], nums[l], nums[r]]) + // b c 去重 + while(l < r && nums[l] === nums[++l]); + while(l < r && nums[r] === nums[--r]); + } + } + return res; +}; +``` From 2b6f7484de2b731e8b03580c45f1681f8fddff5e Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Sat, 22 May 2021 22:40:23 +0800 Subject: [PATCH 6/7] =?UTF-8?q?update242.=E6=9C=89=E6=95=88=E7=9A=84?= =?UTF-8?q?=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0242.有效的字母异位词.md | 12 +++++++++--- problems/0383.赎金信.md | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 8aa0d171..0c75bbf9 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -143,17 +143,23 @@ func isAnagram(s string, t string) bool { javaScript: ```js +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ var isAnagram = function(s, t) { - const resSet = new Array(25).fill(0); + if(s.length !== t.length) return false; + const resSet = new Array(26).fill(0); const base = "a".charCodeAt(); for(const i of s) { resSet[i.charCodeAt() - base]++; } for(const i of t) { + if(!resSet[i.charCodeAt() - base]) return false; resSet[i.charCodeAt() - base]--; - // if(val < 0) return false; } - return resSet.every(i => !i); + return true; }; ``` diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 7ff28601..755910f9 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -176,7 +176,7 @@ javaScript: * @return {boolean} */ var canConstruct = function(ransomNote, magazine) { - const strArr = new Array(25).fill(0), + const strArr = new Array(26).fill(0), base = "a".charCodeAt(); for(const s of magazine) { strArr[s.charCodeAt() - base]++; From 957a284c60ef4c34241982df5f76dacf61134d74 Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Sat, 22 May 2021 23:37:30 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AC=AC18=E9=A2=98.=20?= =?UTF-8?q?=E5=9B=9B=E6=95=B0=E4=B9=8B=E5=92=8CjavaScript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0018.四数之和.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0018.四数之和.md b/problems/0018.四数之和.md index ff441bf7..067386bb 100644 --- a/problems/0018.四数之和.md +++ b/problems/0018.四数之和.md @@ -169,7 +169,39 @@ Python: Go: +javaScript: +```js +/** + * @param {number[]} nums + * @param {number} target + * @return {number[][]} + */ +var fourSum = function(nums, target) { + const len = nums.length; + if(len < 4) return []; + nums.sort((a, b) => a - b); + const res = []; + for(let i = 0; i < len - 3; i++) { + // 去重i + if(i > 0 && nums[i] === nums[i - 1]) continue; + for(let j = i + 1; j < len - 2; j++) { + // 去重j + if(j > i + 1 && nums[j] === nums[j - 1]) continue; + let l = j + 1, r = len - 1; + while(l < r) { + const sum = nums[i] + nums[j] + nums[l] + nums[r]; + if(sum < target) { l++; continue} + if(sum > target) { r--; continue} + res.push([nums[i], nums[j], nums[l], nums[r]]); + while(l < r && nums[l] === nums[++l]); + while(l < r && nums[r] === nums[--r]); + } + } + } + return res; +}; +``` -----------------------