From f9079ddc18206cd8a16eeb8085808b65ecab6d6e Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Tue, 5 Apr 2022 10:56:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880455.=E5=88=86?= =?UTF-8?q?=E5=8F=91=E9=A5=BC=E5=B9=B2.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0455.分发饼干.md | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index 210b492d..f012df68 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -209,7 +209,50 @@ var findContentChildren = function(g, s) { ``` +### TypeScript + +```typescript +// 大饼干尽量喂胃口大的 +function findContentChildren(g: number[], s: number[]): number { + g.sort((a, b) => a - b); + s.sort((a, b) => a - b); + const childLength: number = g.length, + cookieLength: number = s.length; + let curChild: number = childLength - 1, + curCookie: number = cookieLength - 1; + let resCount: number = 0; + while (curChild >= 0 && curCookie >= 0) { + if (g[curChild] <= s[curCookie]) { + curCookie--; + resCount++; + } + curChild--; + } + return resCount; +}; +``` + +```typescript +// 小饼干先喂饱小胃口的 +function findContentChildren(g: number[], s: number[]): number { + g.sort((a, b) => a - b); + s.sort((a, b) => a - b); + const childLength: number = g.length, + cookieLength: number = s.length; + let curChild: number = 0, + curCookie: number = 0; + while (curChild < childLength && curCookie < cookieLength) { + if (g[curChild] <= s[curCookie]) { + curChild++; + } + curCookie++; + } + return curChild; +}; +``` + ### C + ```c int cmp(int* a, int* b) { return *a - *b;