Skip to content

Commit 0fb372f

Browse files
committed
添加 Longest Consecutive Sequence, Valid Palindrome,以及王顺达的一个代码
1 parent ca0b530 commit 0fb372f

File tree

5 files changed

+146
-1
lines changed

5 files changed

+146
-1
lines changed

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

6.73 KB
Binary file not shown.

C++/chapImplement.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ \subsubsection{代码}
286286

287287
\begin{Code}
288288
// LeetCode, Multiply Strings
289+
// @author 连城(http://weibo.com/lianchengzju)
289290
// 一个字符对应一个int
290291
typedef vector<int> bigint;
291292

C++/chapLinearList.tex

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,72 @@ \subsubsection{相关题目}
314314
\myenddot
315315

316316

317+
\subsection{Longest Consecutive Sequence} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
318+
\label{sec:longest-consecutive-sequence}
319+
320+
321+
\subsubsection{描述}
322+
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
323+
324+
For example,
325+
Given \code{[100, 4, 200, 1, 3, 2]},
326+
The longest consecutive elements sequence is \code{[1, 2, 3, 4]}. Return its length: 4.
327+
328+
Your algorithm should run in $O(n)$ complexity.
329+
330+
331+
\subsubsection{分析}
332+
如果允许$O(n \log n)$的复杂度,那么可以先排序,可是本题要求$O(n)$
333+
334+
由于序列里的元素是无序的,又要求$O(n)$,首先要想到用哈希表。
335+
336+
用一个哈希表 \fn{unordered_map<int, bool> used}记录每个元素是否使用,对每个元素,以该元素为中心,往左右扩张,直到不连续为止,记录下最长的长度。
337+
338+
339+
\subsubsection{代码}
340+
\begin{Code}
341+
// Leet Code, Longest Consecutive Sequence
342+
class Solution {
343+
public:
344+
int longestConsecutive(vector<int> const& num) {
345+
unordered_map<int, bool> used;
346+
347+
for (auto i : num) used[i] = false;
348+
349+
int longest = 0;
350+
351+
for (auto i : num) {
352+
if (used[i]) continue;
353+
354+
int length = 1;
355+
356+
used[i] = true;
357+
358+
for (int j = i + 1; used.find(j) != used.end(); ++j) {
359+
used[j] = true;
360+
++length;
361+
}
362+
363+
for (int j = i - 1; used.find(j) != used.end(); --j) {
364+
used[j] = true;
365+
++length;
366+
}
367+
368+
longest = max(longest, length);
369+
}
370+
371+
return longest;
372+
}
373+
};
374+
\end{Code}
375+
376+
377+
\subsubsection{相关题目}
378+
\begindot
379+
\item
380+
\myenddot
381+
382+
317383
\section{单链表} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
318384

319385
单链表节点的定义如下:

C++/chapString.tex

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,63 @@
11
\chapter{字符串}
22

33

4+
\section{Valid Palindrome} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5+
\label{sec:valid-palindrome}
6+
7+
8+
\subsubsection{描述}
9+
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
10+
11+
For example,\\
12+
\code{"A man, a plan, a canal: Panama"} is a palindrome.\\
13+
\code{"race a car"} is not a palindrome.
14+
15+
Note:
16+
Have you consider that the string might be empty? This is a good question to ask during an interview.
17+
18+
For the purpose of this problem, we define empty string as valid palindrome.
19+
20+
21+
\subsubsection{分析}
22+
23+
24+
25+
\subsubsection{代码}
26+
\begin{Code}
27+
// Leet Code, Valid Palindrome
28+
class Solution {
29+
public:
30+
bool isPalindrome(string s) {
31+
transform(s.begin(), s.end(), s.begin(), ::tolower);
32+
auto left = s.begin(), right = prev(s.end());
33+
while (left < right) {
34+
if (!::isalnum(*left)) {
35+
++left;
36+
continue;
37+
}
38+
if (!::isalnum(*right)) {
39+
--right;
40+
continue;
41+
}
42+
if (*left == *right) {
43+
++left;
44+
--right;
45+
} else {
46+
return false;
47+
}
48+
}
49+
return true;
50+
}
51+
};
52+
\end{Code}
53+
54+
55+
\subsubsection{相关题目}
56+
\begindot
57+
\item
58+
\myenddot
59+
60+
461
\section{Implement strStr()} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
562
\label{sec:strstr}
663

C++/chapTree.tex

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,28 @@ \subsubsection{分析}
656656
\subsubsection{代码}
657657
递归版
658658
\begin{Code}
659-
// LeetCode, Flatten Binary Tree to Linked List,递归版
659+
// LeetCode, Flatten Binary Tree to Linked List
660+
// 递归版1,作者:王顺达,http://weibo.com/u/1234984145
661+
class Solution {
662+
public:
663+
void flatten(TreeNode *root) {
664+
flatten(root, NULL);
665+
}
666+
private:
667+
// 把root所代表树变成链表后,tail跟在该链表后面
668+
TreeNode *flatten(TreeNode *root, TreeNode *tail) {
669+
if (NULL == root) return tail;
670+
671+
root->right = flatten(root->left, flatten(root->right, tail));
672+
root->left = NULL;
673+
return root;
674+
}
675+
};
676+
\end{Code}
677+
678+
\begin{Code}
679+
// LeetCode, Flatten Binary Tree to Linked List
680+
// 递归版2
660681
class Solution {
661682
public:
662683
void flatten(TreeNode *root) {

0 commit comments

Comments
 (0)