René's URL Explorer Experiment


Title: 回溯算法汇总一 · Issue #170 · sisterAn/JavaScript-Algorithms · GitHub

Open Graph Title: 回溯算法汇总一 · Issue #170 · sisterAn/JavaScript-Algorithms

X Title: 回溯算法汇总一 · Issue #170 · sisterAn/JavaScript-Algorithms

Description: 上周发了篇文章,关于一名 95 后女程序员(4 年前端开发),对现状的担忧与对未来的焦虑担忧,文章一经发出,后台就收到很多反馈,大部分的人都表示认同: 因此,我决定把自己的每日进阶记录下来,有时可能 2-3 天一起,每日内容不多,如果你不清楚怎么做,就和我一起吧,如果你有规划,也可以关注「三分钟学前端」,添加我的微信 pzijun_com,将你的每日进阶记录发送到群群里,我们一起督促学习 回溯算法 什么是回溯算法问题? 回溯算法是一种搜索法,试探法,它会在每一步做出选择...

Open Graph Description: 上周发了篇文章,关于一名 95 后女程序员(4 年前端开发),对现状的担忧与对未来的焦虑担忧,文章一经发出,后台就收到很多反馈,大部分的人都表示认同: 因此,我决定把自己的每日进阶记录下来,有时可能 2-3 天一起,每日内容不多,如果你不清楚怎么做,就和我一起吧,如果你有规划,也可以关注「三分钟学前端」,添加我的微信 pzijun_com,将你的每日进阶记录发送到群群里,我们一起督促学习 回...

X Description: 上周发了篇文章,关于一名 95 后女程序员(4 年前端开发),对现状的担忧与对未来的焦虑担忧,文章一经发出,后台就收到很多反馈,大部分的人都表示认同: 因此,我决定把自己的每日进阶记录下来,有时可能 2-3 天一起,每日内容不多,如果你不清楚怎么做,就和我一起吧,如果你有规划,也可以关注「三分钟学前端」,添加我的微信 pzijun_com,将你的每日进阶记录发送到群群里,我们一起督促学习 回...

Opengraph URL: https://github.com/sisterAn/JavaScript-Algorithms/issues/170

X: @github

direct link

Domain: patch-diff.githubusercontent.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"回溯算法汇总一","articleBody":"上周发了篇文章,关于一名 95 后女程序员(4 年前端开发),对现状的担忧与对未来的焦虑担忧,文章一经发出,后台就收到很多反馈,大部分的人都表示认同:\r\n\r\n![WechatIMG28](https://user-images.githubusercontent.com/19721451/162015147-a5240cda-cda5-4231-819a-995c0406a57b.jpeg)\r\n\r\n\r\n因此,我决定把自己的每日进阶记录下来,有时可能 2-3 天一起,每日内容不多,如果你不清楚怎么做,就和我一起吧,如果你有规划,也可以关注「**三分钟学前端**」,添加我的微信 **pzijun_com**,将你的每日进阶记录发送到群群里,我们一起督促学习\r\n\r\n# 回溯算法\r\n\r\n**什么是回溯算法问题?**\r\n\r\n回溯算法是一种搜索法,试探法,它会在每一步做出选择,一旦发现这个选择无法得到期望结果,就回溯回去,重新做出选择。它就是不断的尝试,直到拿到解。因此它类似于一种暴力穷举的算法,此类问题一般都很少考虑时间复杂度问题\r\n\r\n它的这种算法思想,使它通常用于解决广度的搜索问题,即从一组可能的解中,选择一个满足要求的解,或得到所有满足要求的解。因此它常采用深度优先遍历(DFS)求解\r\n\r\n我们最常遇到的问题,例如:\r\n\r\n- 全排列问题\r\n- 括号生成问题\r\n- 组合总和问题\r\n- 树的深度遍历问题\r\n\r\n## 全排列问题\r\n\r\n给定一个 **没有重复** 数字的序列,返回其所有可能的全排列。\r\n\r\n```javascript\r\n输入: [1,2,3]\r\n输出:\r\n[\r\n  [1,2,3],\r\n  [1,3,2],\r\n  [2,1,3],\r\n  [2,3,1],\r\n  [3,1,2],\r\n  [3,2,1]\r\n]\r\n```\r\n\r\n它的核心思路就是类似一个多叉树的形式,我们把问题求解的过程分为多个阶段。每个阶段,我们都会面对一个岔路口,我们先随意选一条路走,当发现这条路走不通的时候(不符合期望的解),就回退到上一个岔路口,另选一种走法继续走。\r\n\r\n![image.png](https://pic.leetcode-cn.com/0bf18f9b86a2542d1f6aa8db6cc45475fce5aa329a07ca02a9357c2ead81eec1-image.png)\r\n\r\n图片来自于:https://pic.leetcode-cn.com/0bf18f9b86a2542d1f6aa8db6cc45475fce5aa329a07ca02a9357c2ead81eec1-image.png\r\n\r\n```javascript\r\nlet permute = function(nums) {\r\n    // 使用一个数组保存所有可能的全排列\r\n    let res = []\r\n    if (nums.length === 0) {\r\n        return res\r\n    }\r\n    let used = {}, path = []\r\n    dfs(nums, nums.length, 0, path, used, res)\r\n    return res\r\n}\r\nlet dfs = function(nums, len, depth, path, used, res) {\r\n    // 所有数都填完了\r\n    if (depth === len) {\r\n        res.push([...path])\r\n        return\r\n    }\r\n    for (let i = 0; i \u003c len; i++) {\r\n        if (!used[i]) {\r\n            // 动态维护数组\r\n            path.push(nums[i])\r\n            used[i] = true\r\n            // 继续递归填下一个数\r\n            dfs(nums, len, depth + 1, path, used, res)\r\n            // 撤销操作\r\n            used[i] = false\r\n            path.pop()\r\n        }\r\n      \r\n    }\r\n}\r\n```\r\n\r\n## 括号生成问题\r\n\r\n数字 `n` 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 **有效的** 括号组合。\r\n\r\n**示例:**\r\n\r\n```javascript\r\n输入:n = 3\r\n输出:[\r\n       \"((()))\",\r\n       \"(()())\",\r\n       \"(())()\",\r\n       \"()(())\",\r\n       \"()()()\"\r\n     ]\r\n```\r\n\r\n对应于本题,最开始是 `''`,我们可以每次试探增加 `(` 或 `)` ,注意:\r\n\r\n- 加入 `(` 的条件是,当前是否还有 `(` 可以选择\r\n- 加入 `)` 的时候,受到 `(` 的限制,如果已选择的结果里的 `(` 小于等于已选择里的 `)` 时,此时是不能选择 `)` 的,例如如果当前是 `()` ,继续选择 `)` 就是 `())` ,是不合法的\r\n\r\n**代码实现:**\r\n\r\n```javascript\r\nconst generateParenthesis = (n) =\u003e {\r\n    const res = []\r\n    const dfs = (path, left, right) =\u003e {\r\n        // 肯定不合法,提前结束\r\n        if (left \u003e n || left \u003c right) return\r\n        // 到达结束条件\r\n        if (left + right === 2 * n) {\r\n            res.push(path)\r\n            return\r\n        }\r\n        // 选择\r\n        dfs(path + '(', left + 1, right)\r\n        dfs(path + ')', left, right + 1)\r\n    }\r\n    dfs('', 0, 0)\r\n    return res\r\n}\r\n```\r\n\r\n## 组合总和问题\r\n\r\n给你一个 无重复元素 的整数数组 `candidates` 和一个目标整数 `target` ,找出 `candidates` 中可以使数字和为目标数 `target` 的 **所有** 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。\r\n\r\n- `candidates` 中的 同一个 数字可以无限制重复被选取 。\r\n- 如果至少一个数字的被选数量不同,则两种组合是不同的。 \r\n\r\n```javascript\r\n输入: candidates = [2,3,5], target = 8\r\n输出: [[2,2,2,2],[2,3,3],[3,5]]\r\n```\r\n\r\n对应于本题,从 `candidates` 选择数,每个数可以选择多次,使用 `index` 记录当前选到第几个数,不可向前选择,防止重复,`count` 为当前满足条件的 `index` 位的数最多可选 `count` 次,由 `0 ... count` 次往后递归\r\n- `paths`:已选数\r\n- `sum`:已选数和\r\n- `index`:当前选到第几个数\r\n\r\n```js\r\nvar combinationSum = function(candidates, target) {\r\n    let res = [], map = new Map()\r\n    let dfs = function(paths, sum, index) {\r\n        if(sum \u003e target || index \u003e candidates.length) return\r\n        if(target === sum) {\r\n            res.push(paths)\r\n            return \r\n        }\r\n        // 计算当前数可选的倍数\r\n        let count = Math.floor((target-sum)/candidates[index])\r\n        // 选择当前数\r\n        for(let i = 0; i \u003c= count; i++) {\r\n            dfs(new Array(i).fill(candidates[index]).concat(paths), sum+i*candidates[index], index+1)\r\n        }\r\n    }\r\n    dfs([], 0, 0)\r\n    return res\r\n};\r\n```\r\n优化:\r\n```javascript\r\nvar combinationSum = function(candidates, target) {\r\n    let res = [], map = new Map()\r\n    let dfs = function(paths, sum, index) {\r\n        if(sum \u003e target || index \u003e candidates.length) return\r\n        if(target === sum) {\r\n            res.push(paths)\r\n            return \r\n        }\r\n        dfs(paths, sum, index+1)\r\n        if(target-sum\u003e=candidates[index]) {\r\n          dfs([...paths, candidates[index]], sum+candidates[index], index)\r\n        }\r\n    }\r\n    dfs([], 0, 0)\r\n    return res\r\n};\r\n```\r\n\r\n\r\n","author":{"url":"https://github.com/sisterAn","@type":"Person","name":"sisterAn"},"datePublished":"2022-04-06T15:49:54.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/170/JavaScript-Algorithms/issues/170"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:6911e6ed-a6f5-2772-b66b-e6fc37f78ebc
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idD50A:209CC7:3116BBE:4342229:6972C31D
html-safe-nonceb052fab02af8d80e3e085ee18a71fa940a0f8929266985216cc51a168ce5f8da
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENTBBOjIwOUNDNzozMTE2QkJFOjQzNDIyMjk6Njk3MkMzMUQiLCJ2aXNpdG9yX2lkIjoiMzI4Mjc5MDcxMDU1MzMyMTI1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac189951672a9bfe96d82d5b3038b2d7530d4b76dc125be7b6a669d9fdee7ad25e
hovercard-subject-tagissue:1194801039
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/sisterAn/JavaScript-Algorithms/170/issue_layout
twitter:imagehttps://opengraph.githubassets.com/1fb8f85d7406765d4a63c6583aa60d691598540d41e1c1c53e89adebf617ed5c/sisterAn/JavaScript-Algorithms/issues/170
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/1fb8f85d7406765d4a63c6583aa60d691598540d41e1c1c53e89adebf617ed5c/sisterAn/JavaScript-Algorithms/issues/170
og:image:alt上周发了篇文章,关于一名 95 后女程序员(4 年前端开发),对现状的担忧与对未来的焦虑担忧,文章一经发出,后台就收到很多反馈,大部分的人都表示认同: 因此,我决定把自己的每日进阶记录下来,有时可能 2-3 天一起,每日内容不多,如果你不清楚怎么做,就和我一起吧,如果你有规划,也可以关注「三分钟学前端」,添加我的微信 pzijun_com,将你的每日进阶记录发送到群群里,我们一起督促学习 回...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamesisterAn
hostnamegithub.com
expected-hostnamegithub.com
Noneaa656e37a6f46b81c2416d9c983f7c54e264ee31be17c0e6c9414b9f9f9c6eb4
turbo-cache-controlno-preview
go-importgithub.com/sisterAn/JavaScript-Algorithms git https://github.com/sisterAn/JavaScript-Algorithms.git
octolytics-dimension-user_id19721451
octolytics-dimension-user_loginsisterAn
octolytics-dimension-repository_id252061924
octolytics-dimension-repository_nwosisterAn/JavaScript-Algorithms
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id252061924
octolytics-dimension-repository_network_root_nwosisterAn/JavaScript-Algorithms
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releaseadd6bd61de5b348d2978a698a5796a7d0438e7be
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms/issues/170#start-of-content
https://patch-diff.githubusercontent.com/
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2FsisterAn%2FJavaScript-Algorithms%2Fissues%2F170
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2FsisterAn%2FJavaScript-Algorithms%2Fissues%2F170
Sign up https://patch-diff.githubusercontent.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=sisterAn%2FJavaScript-Algorithms
Reloadhttps://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms/issues/170
Reloadhttps://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms/issues/170
Reloadhttps://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms/issues/170
sisterAn https://patch-diff.githubusercontent.com/sisterAn
JavaScript-Algorithmshttps://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms
Notifications https://patch-diff.githubusercontent.com/login?return_to=%2FsisterAn%2FJavaScript-Algorithms
Fork 649 https://patch-diff.githubusercontent.com/login?return_to=%2FsisterAn%2FJavaScript-Algorithms
Star 5.7k https://patch-diff.githubusercontent.com/login?return_to=%2FsisterAn%2FJavaScript-Algorithms
Code https://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms
Issues 158 https://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms/issues
Pull requests 0 https://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms/pulls
Actions https://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms/actions
Projects 0 https://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms/projects
Security 0 https://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms/security
Insights https://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms/pulse
Code https://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms
Issues https://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms/issues
Pull requests https://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms/pulls
Actions https://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms/actions
Projects https://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms/projects
Security https://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms/security
Insights https://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms/pulse
New issuehttps://patch-diff.githubusercontent.com/login?return_to=https://github.com/sisterAn/JavaScript-Algorithms/issues/170
New issuehttps://patch-diff.githubusercontent.com/login?return_to=https://github.com/sisterAn/JavaScript-Algorithms/issues/170
回溯算法汇总一https://patch-diff.githubusercontent.com/sisterAn/JavaScript-Algorithms/issues/170#top
https://github.com/sisterAn
https://github.com/sisterAn
sisterAnhttps://github.com/sisterAn
on Apr 6, 2022https://github.com/sisterAn/JavaScript-Algorithms/issues/170#issue-1194801039
https://user-images.githubusercontent.com/19721451/162015147-a5240cda-cda5-4231-819a-995c0406a57b.jpeg
https://camo.githubusercontent.com/09218950b234ea696a16193c1c68474eb07535ba391a0f85712a020fe5c6f0b4/68747470733a2f2f7069632e6c656574636f64652d636e2e636f6d2f306266313866396238366132353432643166366161386462366363343534373566636535616133323961303763613032613933353763326561643831656563312d696d6167652e706e67
https://pic.leetcode-cn.com/0bf18f9b86a2542d1f6aa8db6cc45475fce5aa329a07ca02a9357c2ead81eec1-image.pnghttps://pic.leetcode-cn.com/0bf18f9b86a2542d1f6aa8db6cc45475fce5aa329a07ca02a9357c2ead81eec1-image.png
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.