From 175b71d21fc5e3bdddc21e5dafe8a73980d79016 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sun, 10 Apr 2022 21:59:21 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880406.=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E8=BA=AB=E9=AB=98=E9=87=8D=E5=BB=BA=E9=98=9F=E5=88=97?= =?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=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/0406.根据身高重建队列.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0406.根据身高重建队列.md b/problems/0406.根据身高重建队列.md index b2354d09..ecb05301 100644 --- a/problems/0406.根据身高重建队列.md +++ b/problems/0406.根据身高重建队列.md @@ -290,6 +290,24 @@ var reconstructQueue = function(people) { }; ``` +### TypeScript + +```typescript +function reconstructQueue(people: number[][]): number[][] { + people.sort((a, b) => { + if (a[0] === b[0]) return a[1] - b[1]; + return b[0] - a[0]; + }); + const resArr: number[][] = []; + for (let i = 0, length = people.length; i < length; i++) { + resArr.splice(people[i][1], 0, people[i]); + } + return resArr; +}; +``` + + + -----------------------
From ae38a29068241636052e831d96b1e446dddd979e Mon Sep 17 00:00:00 2001 From: Jamcy123 <1219502823@qq.com> Date: Tue, 12 Apr 2022 16:57:25 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=200503.=E4=B8=8B?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II=20java?= =?UTF-8?q?script=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0503.下一个更大元素II.md | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/problems/0503.下一个更大元素II.md b/problems/0503.下一个更大元素II.md index 36e183e1..d6d3850f 100644 --- a/problems/0503.下一个更大元素II.md +++ b/problems/0503.下一个更大元素II.md @@ -166,20 +166,19 @@ JavaScript: * @return {number[]} */ var nextGreaterElements = function (nums) { - // let map = new Map(); + const len = nums.length; let stack = []; - let res = new Array(nums.length).fill(-1); - for (let i = 0; i < nums.length * 2; i++) { + let res = Array(len).fill(-1); + for (let i = 0; i < len * 2; i++) { while ( stack.length && - nums[i % nums.length] > nums[stack[stack.length - 1]] + nums[i % len] > nums[stack[stack.length - 1]] ) { - let index = stack.pop(); - res[index] = nums[i % nums.length]; + const index = stack.pop(); + res[index] = nums[i % len]; } - stack.push(i % nums.length); + stack.push(i % len); } - return res; }; ``` From 82ab1706f88e390ff2f13478cd7771cc92e76754 Mon Sep 17 00:00:00 2001 From: Jamcy123 <1219502823@qq.com> Date: Tue, 12 Apr 2022 17:09:20 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=B8=80=E3=80=81=E5=A2=9E=E5=8A=A0=E7=89=88=E6=9C=AC=E4=BA=8C?= =?UTF-8?q?=200739.=E6=AF=8F=E6=97=A5=E6=B8=A9=E5=BA=A6=20javascript=20?= =?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/0739.每日温度.md | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index 710f5eb6..5f53e412 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -301,25 +301,22 @@ func dailyTemperatures(num []int) []int { JavaScript: ```javascript -/** - * @param {number[]} temperatures - * @return {number[]} - */ +// 版本一 var dailyTemperatures = function(temperatures) { - let n = temperatures.length; - let res = new Array(n).fill(0); - let stack = []; // 递减栈:用于存储元素右面第一个比他大的元素下标 + const n = temperatures.length; + const res = Array(n).fill(0); + const stack = []; // 递增栈:用于存储元素右面第一个比他大的元素下标 stack.push(0); for (let i = 1; i < n; i++) { // 栈顶元素 - let top = stack[stack.length - 1]; + const top = stack[stack.length - 1]; if (temperatures[i] < temperatures[top]) { stack.push(i); } else if (temperatures[i] === temperatures[top]) { stack.push(i); } else { while (stack.length && temperatures[i] > temperatures[stack[stack.length - 1]]) { - let top = stack.pop(); + const top = stack.pop(); res[top] = i - top; } stack.push(i); @@ -327,6 +324,23 @@ var dailyTemperatures = function(temperatures) { } return res; }; + + +// 版本二 +var dailyTemperatures = function(temperatures) { + const n = temperatures.length; + const res = Array(n).fill(0); + const stack = []; // 递增栈:用于存储元素右面第一个比他大的元素下标 + stack.push(0); + for (let i = 1; i < n; i++) { + while (stack.length && temperatures[i] > temperatures[stack[stack.length - 1]]) { + const top = stack.pop(); + res[top] = i - top; + } + stack.push(i); + } + return res; +}; ``` From be4051893888427b41b172135b21add3fa096579 Mon Sep 17 00:00:00 2001 From: Jamcy123 <1219502823@qq.com> Date: Fri, 22 Apr 2022 15:34:06 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201047.=20=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80?= =?UTF-8?q?=E6=9C=89=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9=20?= =?UTF-8?q?=E5=8F=8C=E6=8C=87=E9=92=88=E8=A7=A3=E6=B3=95=20javascript?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...除字符串中的所有相邻重复项.md | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 54455f62..638c8f4e 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -250,11 +250,9 @@ func removeDuplicates(s string) string { javaScript: +法一:使用栈 + ```js -/** - * @param {string} s - * @return {string} - */ var removeDuplicates = function(s) { const stack = []; for(const x of s) { @@ -267,6 +265,25 @@ var removeDuplicates = function(s) { }; ``` +法二:双指针(模拟栈) + +```js +// 原地解法(双指针模拟栈) +var removeDuplicates = function(s) { + s = [...s]; + let top = -1; // 指向栈顶元素的下标 + for(let i = 0; i < s.length; i++) { + if(top === -1 || s[top] !== s[i]) { // top === -1 即空栈 + s[++top] = s[i]; // 入栈 + } else { + top--; // 推出栈 + } + } + s.length = top + 1; // 栈顶元素下标 + 1 为栈的长度 + return s.join(''); +}; +``` + TypeScript: ```typescript