Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Samoy
js-for-30-days
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
js-for-30-days
/
src
/
functiontransform
/
memoize.ts
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
37 lines (32 loc) · 1.36 KB
main
Breadcrumbs
js-for-30-days
/
src
/
functiontransform
/
memoize.ts
Copy path
Top
File metadata and controls
Code
Blame
37 lines (32 loc) · 1.36 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
type Fn = (...params: number[]) => number
/**
* 请你编写一个函数 fn,它接收另一个函数作为输入,并返回该函数的 记忆化 后的结果。
*
* 记忆函数 是一个对于相同的输入永远不会被调用两次的函数。相反,它将返回一个缓存值。
*
* 你可以假设有 3 个可能的输入函数:sum 、fib 和 factorial 。
*
* sum 接收两个整型参数 a 和 b ,并返回 a + b 。假设如果参数 (b, a) 已经缓存了值,其中 a != b,它不能用于参数 (a, b)。例如,如果参数是 (3, 2) 和 (2, 3),则应进行两个单独的调用。
* fib 接收一个整型参数 n ,如果 n <= 1 则返回 1,否则返回 fib (n - 1) + fib (n - 2)。
* factorial 接收一个整型参数 n ,如果 n <= 1 则返回 1 ,否则返回 factorial(n - 1) * n 。
* @link https://leetcode.cn/problems/memoize/description/?envType=study-plan-v2&envId=30-days-of-javascript
* @param fn
*/
let callCount = 0;
function memoize(fn: Fn): Fn {
const cache = new Map<string, number>()
return function (...args) {
const key = JSON.stringify(args)
if (cache.has(key)) {
return cache.get(key)
}
callCount++
const ans = fn(...args)
cache.set(key, ans)
return ans
}
}
function reset() {
callCount = 0
}
export {memoize, callCount, reset}
You can’t perform that action at this time.