From 282cdc2b44b755098d223d0f9c8bd36bf0e85959 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Mon, 23 May 2022 11:18:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=881365.=E6=9C=89?= =?UTF-8?q?=E5=A4=9A=E5=B0=91=E5=B0=8F=E4=BA=8E=E5=BD=93=E5=89=8D=E6=95=B0?= =?UTF-8?q?=E5=AD=97=E7=9A=84=E6=95=B0=E5=AD=97.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...65.有多少小于当前数字的数字.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/problems/1365.有多少小于当前数字的数字.md b/problems/1365.有多少小于当前数字的数字.md index 78fa84c0..ce1e77df 100644 --- a/problems/1365.有多少小于当前数字的数字.md +++ b/problems/1365.有多少小于当前数字的数字.md @@ -217,6 +217,46 @@ var smallerNumbersThanCurrent = function(nums) { }; ``` +TypeScript: + +> 暴力法: + +```typescript +function smallerNumbersThanCurrent(nums: number[]): number[] { + const length: number = nums.length; + const resArr: number[] = []; + for (let i = 0; i < length; i++) { + let count: number = 0; + for (let j = 0; j < length; j++) { + if (nums[j] < nums[i]) { + count++; + } + } + resArr[i] = count; + } + return resArr; +}; +``` + +> 排序+hash + +```typescript +function smallerNumbersThanCurrent(nums: number[]): number[] { + const length: number = nums.length; + const sortedArr: number[] = [...nums]; + sortedArr.sort((a, b) => a - b); + const hashMap: Map = new Map(); + for (let i = length - 1; i >= 0; i--) { + hashMap.set(sortedArr[i], i); + } + const resArr: number[] = []; + for (let i = 0; i < length; i++) { + resArr[i] = hashMap.get(nums[i]); + } + return resArr; +}; +``` + -----------------------