@@ -1640,7 +1640,7 @@ \subsubsection{分析}
1640
1640
无
1641
1641
1642
1642
1643
- \subsubsection {代码 }
1643
+ \subsubsection {迭代版 }
1644
1644
\begin {Code }
1645
1645
// LeetCode, Candy
1646
1646
// 时间复杂度O(n),空间复杂度O(n)
@@ -1673,6 +1673,34 @@ \subsubsection{代码}
1673
1673
\end {Code }
1674
1674
1675
1675
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
+
1676
1704
\subsubsection {相关题目 }
1677
1705
\begindot
1678
1706
\item 无
@@ -2469,3 +2497,94 @@ \subsubsection{相关题目}
2469
2497
\begindot
2470
2498
\item 无
2471
2499
\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
0 commit comments