status
date
slug
tags
category
type
password
icon
本文目錄
 

前言

 

經典回溯法

LeetCode 51. N-Queens

 

LeetCode 139. Word Break

LeetCode 2096. Step-By-Step Directions From a Binary Tree Node to Another

組合與排列

LeetCode 46. Permutations

  • 回溯法模板

LeetCode 78. Subsets

  • 每個元素只能選取一次 ⇒ 指定 iterate 起始值

LeetCode 77. Combinations

LeetCode 90. Subsets II

避免產生重複的組合 ⇒ 對輸入進行排序,並且在 iterate 時將相鄰相同的元素直接忽略。
notion image

LeetCode 40. Combination Sum II

LeetCode 47. Permutations II

  • 存在相同元素,要避免重複解 ⇒ 同時進行曾尋訪判斷以及相鄰元素相等判斷
將尋訪判斷調整成未尋訪過即略過,可將效率提升
notion image
notion image

LeetCode 39. Combination Sum

元素可以被重複選取 ⇒ 將遞迴的起始位置從 i+1 改成 i
notion image
 

小結

  • 輸入元素不重複 且 最多使用一次 ⇒ 經典回溯解法
  •  輸入元素重複 且 最多使用一次 ⇒ 排除相鄰元素
  • 輸入元素不重複 且 可使用無限次 ⇒ 遞迴起始值將 i+1 改成 i
 
混合背包 學習筆記鏈表 隨筆
Loading...
Zixu
Zixu
Welcome to my webstie.
Analytics
Post Count:
224
Latest posts
還好我退了 部隊篇
2025/08/08
泡泡
2025/08/05
還好我退了 新訓篇
2025/08/02
大學畢業心得
2025/07/12
AP325 隨筆
2025/06/07
Lycoris Recoil 莉可麗絲
2025/05/04