Skip to content

Commit 66ee9fb

Browse files
committed
重构深搜这章
1 parent 630c8d0 commit 66ee9fb

File tree

2 files changed

+152
-100
lines changed

2 files changed

+152
-100
lines changed

C++/chapBruteforce.tex

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ \subsubsection{代码}
454454
\end{Code}
455455

456456

457-
\subsection{深搜}
457+
\subsection{递归}
458458
本题是求路径本身,求所有解,函数参数需要标记当前走到了哪步,还需要中间结果的引用,最终结果的引用。
459459

460460
扩展节点,每次从左到右,选一个没有出现过的元素。
@@ -530,7 +530,7 @@ \subsection{重新实现next_permutation()}
530530
重新实现\fn{std::next_permutation()},代码与上一题相同。
531531

532532

533-
\subsection{深搜}
533+
\subsection{递归}
534534
递归函数\fn{permute()}的参数\fn{p},是中间结果,它的长度又能标记当前走到了哪一步,用于判断收敛条件。
535535

536536
扩展节点,每次从小到大,选一个没有被用光的元素,直到所有元素被用光。
@@ -629,7 +629,7 @@ \subsubsection{描述}
629629
\end{Code}
630630

631631

632-
\subsection{深搜}
632+
\subsection{递归}
633633
\begin{Code}
634634
// LeetCode, Combinations
635635
// 深搜,递归
@@ -663,7 +663,7 @@ \subsection{迭代}
663663
\begin{Code}
664664
// LeetCode, Combinations
665665
// use prev_permutation()
666-
// 时间复杂度O(n!),空间复杂度O(n)
666+
// 时间复杂度O((n-k)!),空间复杂度O(n)
667667
class Solution {
668668
public:
669669
vector<vector<int> > combine(int n, int k) {
@@ -694,3 +694,96 @@ \subsubsection{相关题目}
694694
\item Permutations, 见 \S \ref{sec:permutations}
695695
\item Permutations II, 见 \S \ref{sec:permutations-ii}
696696
\myenddot
697+
698+
699+
\section{Letter Combinations of a Phone Number } %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
700+
\label{sec:letter-combinations-of-a-phone-number }
701+
702+
703+
\subsubsection{描述}
704+
Given a digit string, return all possible letter combinations that the number could represent.
705+
706+
A mapping of digit to letters (just like on the telephone buttons) is given below.
707+
708+
\begin{center}
709+
\includegraphics{phone-keyboard.png}\\
710+
\figcaption{Phone Keyboard}\label{fig:phone-keyboard}
711+
\end{center}
712+
713+
\textbf{Input:}Digit string \code{"23"}
714+
715+
\textbf{Output:} \code{["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]}.
716+
717+
\textbf{Note:}
718+
Although the above answer is in lexicographical order, your answer could be in any order you want.
719+
720+
721+
\subsubsection{分析}
722+
723+
724+
725+
\subsection{递归}
726+
\begin{Code}
727+
// LeetCode, Letter Combinations of a Phone Number
728+
// 时间复杂度O(3^n),空间复杂度O(n)
729+
class Solution {
730+
public:
731+
const vector<string> keyboard { " ", "", "abc", "def", // '0','1','2',...
732+
"ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
733+
734+
vector<string> letterCombinations (const string &digits) {
735+
vector<string> result;
736+
dfs(digits, 0, "", result);
737+
return result;
738+
}
739+
740+
void dfs(const string &digits, size_t cur, string path,
741+
vector<string> &result) {
742+
if (cur == digits.size()) {
743+
result.push_back(path);
744+
return;
745+
}
746+
for (auto c : keyboard[digits[cur] - '0']) {
747+
dfs(digits, cur + 1, path + c, result);
748+
}
749+
}
750+
};
751+
\end{Code}
752+
753+
754+
\subsection{迭代}
755+
\begin{Code}
756+
// LeetCode, Letter Combinations of a Phone Number
757+
// 时间复杂度O(3^n),空间复杂度O(1)
758+
class Solution {
759+
public:
760+
const vector<string> keyboard { " ", "", "abc", "def", // '0','1','2',...
761+
"ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
762+
763+
vector<string> letterCombinations (const string &digits) {
764+
vector<string> result(1, "");
765+
for (auto d : digits) {
766+
const size_t n = result.size();
767+
const size_t m = keyboard[d - '0'].size();
768+
769+
result.resize(n * m);
770+
for (size_t i = 0; i < m; ++i)
771+
copy(result.begin(), result.begin() + n, result.begin() + n * i);
772+
773+
for (size_t i = 0; i < m; ++i) {
774+
auto begin = result.begin();
775+
for_each(begin + n * i, begin + n * (i+1), [&](string &s) {
776+
s += keyboard[d - '0'][i];
777+
});
778+
}
779+
}
780+
return result;
781+
}
782+
};
783+
\end{Code}
784+
785+
786+
\subsubsection{相关题目}
787+
\begindot
788+
\item
789+
\myenddot

0 commit comments

Comments
 (0)