Skip to content

Commit ff47590

Browse files
author
liwentian
committed
fd
1 parent 89f7cb2 commit ff47590

28 files changed

Lines changed: 4392 additions & 41 deletions

‎.DS_Store‎

0 Bytes
Binary file not shown.

‎doc/Summary.md‎

Lines changed: 0 additions & 24 deletions
This file was deleted.

‎ebook/.DS_Store‎

0 Bytes
Binary file not shown.

‎ebook/Graph.aux‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
\relax
2+
\providecommand\hyper@newdestlabel[2]{}
3+
\FN@pp@footnotehinttrue
4+
\@writefile{toc}{\contentsline {chapter}{\numberline {第1章\hspace {0.3em}}Graph}{1}{chapter.1}}
5+
\@writefile{lof}{\addvspace {10\p@ }}
6+
\@writefile{lot}{\addvspace {10\p@ }}
7+
\@writefile{toc}{\contentsline {section}{\numberline {1.1}Alien Dictionary}{1}{section.1.1}}
8+
\@writefile{toc}{\contentsline {subsubsection}{Description}{1}{section*.2}}
9+
\@writefile{toc}{\contentsline {subsubsection}{Solution}{2}{section*.3}}
10+
\FN@pp@footnotehinttrue
11+
\@setckpt{Graph}{
12+
\setcounter{page}{3}
13+
\setcounter{equation}{0}
14+
\setcounter{enumi}{0}
15+
\setcounter{enumii}{0}
16+
\setcounter{enumiii}{0}
17+
\setcounter{enumiv}{0}
18+
\setcounter{footnote}{0}
19+
\setcounter{mpfootnote}{0}
20+
\setcounter{part}{0}
21+
\setcounter{chapter}{1}
22+
\setcounter{section}{1}
23+
\setcounter{subsection}{0}
24+
\setcounter{subsubsection}{0}
25+
\setcounter{paragraph}{0}
26+
\setcounter{subparagraph}{0}
27+
\setcounter{figure}{0}
28+
\setcounter{table}{0}
29+
\setcounter{FancyVerbLine}{54}
30+
\setcounter{pp@next@reset}{1}
31+
\setcounter{@fnserial}{0}
32+
\setcounter{Item}{0}
33+
\setcounter{Hfootnote}{0}
34+
\setcounter{Hy@AnnotLevel}{0}
35+
\setcounter{bookmark@seq@number}{2}
36+
\setcounter{parentequation}{0}
37+
\setcounter{section@level}{3}
38+
}

‎ebook/Graph.tex‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
\chapter{Graph}
2+
3+
\section{Alien Dictionary} %%%%%%%%%%%%%%%%%%%%%%
4+
5+
\subsubsection{Description}
6+
There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of non-empty words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.
7+
8+
\textbf{Example:}
9+
10+
Given the following words in dictionary,
11+
\begin{Code}
12+
[
13+
"wrt",
14+
"wrf",
15+
"er",
16+
"ett",
17+
"rftt"
18+
]
19+
\end{Code}
20+
21+
The correct order is: \code{"wertf"}.
22+
23+
\textbf{Note:}
24+
25+
1. You may assume all letters are in lowercase.
26+
27+
2. You may assume that if a is a prefix of b, then a must appear before b in the given dictionary.
28+
29+
3. If the order is invalid, return an empty string.
30+
31+
4. There may be multiple valid order of letters, return any one of them is fine.
32+
33+
\newpage
34+
35+
\subsubsection{Solution}
36+
37+
\begin{Code}
38+
public String alienOrder(String[] words) {
39+
int[] indegree = new int[26];
40+
Arrays.fill(indegree, -1);
41+
42+
int count = 0;
43+
for (String word : words) {
44+
for (char c : word.toCharArray()) {
45+
if (indegree[c - 'a'] != 0) {
46+
indegree[c - 'a'] = 0;
47+
count++;
48+
}
49+
}
50+
}
51+
HashMap<Character, Set<Character>> map = new HashMap<>();
52+
for (int i = 0; i < words.length - 1; i++) {
53+
String first = words[i], second = words[i + 1];
54+
int len = Math.min(first.length(), second.length());
55+
for (int j = 0; j < len; j++) {
56+
if (first.charAt(j) != second.charAt(j)) {
57+
Set<Character> set = map.get(first.charAt(j));
58+
if (set == null) {
59+
set = new HashSet<Character>();
60+
map.put(first.charAt(j), set);
61+
}
62+
if (set.add(second.charAt(j))) {
63+
indegree[second.charAt(j) - 'a']++;
64+
}
65+
break;
66+
} else {
67+
if (j + 1 >= second.length() && j + 1 < first.length()) { return ""; }
68+
}
69+
}
70+
}
71+
Queue<Character> queue = new LinkedList<Character>();
72+
for (int i = 0; i < indegree.length; i++) {
73+
if (indegree[i] == 0) {
74+
queue.add((char) ('a' + i));
75+
}
76+
}
77+
StringBuilder sb = new StringBuilder();
78+
while (!queue.isEmpty()) {
79+
Character from = queue.poll();
80+
sb.append(from);
81+
Set<Character> set = map.get(from);
82+
if (set != null) {
83+
for (Character to : map.get(from)) {
84+
if (--indegree[to - 'a'] == 0) {
85+
queue.add(to);
86+
}
87+
}
88+
}
89+
}
90+
return sb.length() != count ? "" : sb.toString();
91+
}
92+
\end{Code}

‎ebook/leetcode-backtracking.aux‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
\relax
2+
\providecommand\hyper@newdestlabel[2]{}
3+
\providecommand\HyperFirstAtBeginDocument{\AtBeginDocument}
4+
\HyperFirstAtBeginDocument{\ifx\hyper@anchor\@undefined
5+
\global\let\oldcontentsline\contentsline
6+
\gdef\contentsline#1#2#3#4{\oldcontentsline{#1}{#2}{#3}}
7+
\global\let\oldnewlabel\newlabel
8+
\gdef\newlabel#1#2{\newlabelxx{#1}#2}
9+
\gdef\newlabelxx#1#2#3#4#5#6{\oldnewlabel{#1}{{#2}{#3}}}
10+
\AtEndDocument{\ifx\hyper@anchor\@undefined
11+
\let\contentsline\oldcontentsline
12+
\let\newlabel\oldnewlabel
13+
\fi}
14+
\fi}
15+
\global\let\hyper@last\relax
16+
\gdef\HyperFirstAtBeginDocument#1{#1}
17+
\providecommand*\HyPL@Entry[1]{}
18+
\catcode 95\active
19+
\HyPL@Entry{0<</S/D>>}
20+
\providecommand {\FN@pp@footnotehinttrue }{}
21+
\providecommand {\FN@pp@footnote@aux }[2]{}
22+
\FN@pp@footnotehinttrue
23+
\@input{title.aux}
24+
\HyPL@Entry{2<</S/r>>}
25+
\FN@pp@footnotehinttrue
26+
\FN@pp@footnotehinttrue
27+
\FN@pp@footnotehinttrue
28+
\@input{Backtracking.aux}
29+
\HyPL@Entry{4<</S/D>>}
30+
\FN@pp@footnotehinttrue

‎ebook/leetcode-backtracking.idx‎

Whitespace-only changes.

0 commit comments

Comments
 (0)