Skip to content

Commit 10388fd

Browse files
committed
Candy递归版,Linked List Cycle, Linked List Cycle II
1 parent ade70c3 commit 10388fd

File tree

3 files changed

+121
-2
lines changed

3 files changed

+121
-2
lines changed

C++/LeetCodet题解(C++版).pdf

5.65 KB
Binary file not shown.

C++/chapLinearList.tex

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1640,7 +1640,7 @@ \subsubsection{分析}
16401640
16411641

16421642

1643-
\subsubsection{代码}
1643+
\subsubsection{迭代版}
16441644
\begin{Code}
16451645
// LeetCode, Candy
16461646
// 时间复杂度O(n),空间复杂度O(n)
@@ -1673,6 +1673,34 @@ \subsubsection{代码}
16731673
\end{Code}
16741674

16751675

1676+
\subsubsection{递归版}
1677+
\begin{Code}
1678+
// LeetCode, Candy
1679+
// 备忘录法,时间复杂度O(n),空间复杂度O(n)
1680+
// @author fancymouse (http://weibo.com/u/1928162822)
1681+
class Solution {
1682+
public:
1683+
int candy(const vector<int>& ratings) {
1684+
vector<int> f(ratings.size());
1685+
int sum = 0;
1686+
for (int i = 0; i < ratings.size(); ++i)
1687+
sum += solve(ratings, f, i);
1688+
return sum;
1689+
}
1690+
int solve(const vector<int>& ratings, vector<int>& f, int i) {
1691+
if (f[i] == 0) {
1692+
f[i] = 1;
1693+
if (i > 0 && ratings[i] > ratings[i - 1])
1694+
f[i] = max(f[i], solve(ratings, f, i - 1) + 1);
1695+
if (i < ratings.size() - 1 && ratings[i] > ratings[i + 1])
1696+
f[i] = max(f[i], solve(ratings, f, i + 1) + 1);
1697+
}
1698+
return f[i];
1699+
}
1700+
};
1701+
\end{Code}
1702+
1703+
16761704
\subsubsection{相关题目}
16771705
\begindot
16781706
\item
@@ -2469,3 +2497,94 @@ \subsubsection{相关题目}
24692497
\begindot
24702498
\item
24712499
\myenddot
2500+
2501+
2502+
\subsection{Linked List Cycle}
2503+
\label{sec:Linked-List-Cycle}
2504+
2505+
2506+
\subsubsection{描述}
2507+
Given a linked list, determine if it has a cycle in it.
2508+
2509+
Follow up:
2510+
Can you solve it without using extra space?
2511+
2512+
2513+
\subsubsection{分析}
2514+
最容易想到的方法是,用一个哈希表\fn{unordered_map<int, bool> visited},记录每个元素是否被访问过,一旦出现某个元素被重复访问,说明存在环。空间复杂度$O(n)$,时间复杂度$O(N)$
2515+
2516+
最好的方法是时间复杂度O(n),空间复杂度O(1)的。设置两个指针,一个快一个慢,快的指针每次走两步,慢的指针每次走一步,如果快指针和慢指针相遇,则说明有环。参考\myurl{ http://leetcode.com/2010/09/detecting-loop-in-singly-linked-list.html}
2517+
2518+
2519+
\subsubsection{代码}
2520+
\begin{Code}
2521+
//LeetCode, Linked List Cycle
2522+
// 时间复杂度O(n),空间复杂度O(1)
2523+
class Solution {
2524+
public:
2525+
bool hasCycle(ListNode *head) {
2526+
// 设置两个指针,一个快一个慢
2527+
ListNode *slow = head, *fast = head;
2528+
while (fast && fast->next) {
2529+
slow = slow->next;
2530+
fast = fast->next->next;
2531+
if (slow == fast) return true;
2532+
}
2533+
return false;
2534+
}
2535+
};
2536+
\end{Code}
2537+
2538+
2539+
\subsubsection{相关题目}
2540+
\begindot
2541+
\item Linked List Cycle II, 见 \S \ref{sec:Linked-List-Cycle-II}
2542+
\myenddot
2543+
2544+
2545+
\subsection{Linked List Cycle II}
2546+
\label{sec:Linked-List-Cycle-II}
2547+
2548+
2549+
\subsubsection{描述}
2550+
Given a linked list, return the node where the cycle begins. If there is no cycle, return \fn{null}.
2551+
2552+
Follow up:
2553+
Can you solve it without using extra space?
2554+
2555+
2556+
\subsubsection{分析}
2557+
\fn{slow}和\fn{fast}两个指针相遇后,另外设一个指针\fn{slow2},从\fn{head}开始,两个慢指针每次前进一步,它俩一定会在环的开始处相遇。
2558+
2559+
2560+
\subsubsection{代码}
2561+
\begin{Code}
2562+
//LeetCode, Linked List Cycle II
2563+
// 时间复杂度O(n),空间复杂度O(1)
2564+
class Solution {
2565+
public:
2566+
ListNode *detectCycle(ListNode *head) {
2567+
ListNode *slow = head, *fast = head;
2568+
while (fast && fast->next) {
2569+
slow = slow->next;
2570+
fast = fast->next->next;
2571+
if (slow == fast) {
2572+
ListNode *slow2 = head;
2573+
2574+
while (slow2 != slow) {
2575+
slow2 = slow2->next;
2576+
slow = slow->next;
2577+
}
2578+
return slow2;
2579+
}
2580+
}
2581+
return nullptr;
2582+
}
2583+
};
2584+
\end{Code}
2585+
2586+
2587+
\subsubsection{相关题目}
2588+
\begindot
2589+
\item Linked List Cycle, 见 \S \ref{sec:Linked-List-Cycle}
2590+
\myenddot

C++/chapString.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ \subsubsection{纵向扫描}
680680
681681
for (int idx = 0; idx < strs[0].size(); ++idx) { // 纵向扫描
682682
for (int i = 1; i < strs.size(); ++i) {
683-
if (strs[i][idx] != strs[0][idx]) return strs[0].substr(0,idx);;
683+
if (strs[i][idx] != strs[0][idx]) return strs[0].substr(0,idx);
684684
}
685685
}
686686
return strs[0];

0 commit comments

Comments
 (0)