From e894f44b607b0a3433cae8284a9e2c0c85f86645 Mon Sep 17 00:00:00 2001 From: Steve <841532108@qq.com> Date: Fri, 31 Dec 2021 19:23:47 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880027.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=85=83=E7=B4=A0.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20typescript=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 693684f3..99990302 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -197,7 +197,23 @@ var removeElement = (nums, val) => { }; ``` +TypeScript: + +```typescript +function removeElement(nums: number[], val: number): number { + let slowIndex: number = 0, fastIndex: number = 0; + while (fastIndex < nums.length) { + if (nums[fastIndex] !== val) { + nums[slowIndex++] = nums[fastIndex]; + } + fastIndex++; + } + return slowIndex; +}; +``` + Ruby: + ```ruby def remove_element(nums, val) i = 0 From 2e16036f1abcd6a42b4f0dc66f402fd0ddf0cd78 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 31 Dec 2021 20:16:20 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880977.=E6=9C=89?= =?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9.md?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=E6=B7=BB=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/0977.有序数组的平方.md | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index 0085de0b..b11fa7ef 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -221,6 +221,35 @@ var sortedSquares = function(nums) { }; ``` +Typescript: + +双指针法: + +```typescript +function sortedSquares(nums: number[]): number[] { + let left: number = 0, right: number = nums.length - 1; + let resArr: number[] = new Array(nums.length); + let resArrIndex: number = resArr.length - 1; + while (left <= right) { + if (Math.abs(nums[left]) < Math.abs(nums[right])) { + resArr[resArrIndex] = nums[right--] ** 2; + } else { + resArr[resArrIndex] = nums[left++] ** 2; + } + resArrIndex--; + } + return resArr; +}; +``` + +骚操作法(暴力思路): + +```typescript +function sortedSquares(nums: number[]): number[] { + return nums.map(i => i * i).sort((a, b) => a - b); +}; +``` + Swift: ```swift