Skip to content

Commit 42ad4e3

Browse files
author
liwentian
committed
fd
1 parent fee654e commit 42ad4e3

File tree

7 files changed

+214
-9
lines changed

7 files changed

+214
-9
lines changed

ebook/Backtracking.aux

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,15 @@
7171
\@writefile{toc}{\contentsline {section}{\numberline {1.20}Restore IP Addresses}{28}{section.1.20}}
7272
\@writefile{toc}{\contentsline {subsubsection}{Description}{28}{section*.45}}
7373
\@writefile{toc}{\contentsline {subsubsection}{Solution}{28}{section*.46}}
74+
\@writefile{toc}{\contentsline {section}{\numberline {1.21}Word Ladder}{29}{section.1.21}}
75+
\@writefile{toc}{\contentsline {subsubsection}{Description}{29}{section*.47}}
76+
\@writefile{toc}{\contentsline {subsubsection}{Solution I}{30}{section*.48}}
77+
\@writefile{toc}{\contentsline {subsubsection}{Solution II}{31}{section*.49}}
78+
\@writefile{toc}{\contentsline {section}{\numberline {1.22}Word Ladder II}{32}{section.1.22}}
79+
\@writefile{toc}{\contentsline {subsubsection}{Description}{32}{section*.50}}
7480
\FN@pp@footnotehinttrue
7581
\@setckpt{Backtracking}{
76-
\setcounter{page}{29}
82+
\setcounter{page}{33}
7783
\setcounter{equation}{0}
7884
\setcounter{enumi}{0}
7985
\setcounter{enumii}{0}
@@ -83,20 +89,20 @@
8389
\setcounter{mpfootnote}{0}
8490
\setcounter{part}{0}
8591
\setcounter{chapter}{1}
86-
\setcounter{section}{20}
92+
\setcounter{section}{22}
8793
\setcounter{subsection}{0}
8894
\setcounter{subsubsection}{0}
8995
\setcounter{paragraph}{0}
9096
\setcounter{subparagraph}{0}
9197
\setcounter{figure}{1}
9298
\setcounter{table}{0}
93-
\setcounter{FancyVerbLine}{1}
99+
\setcounter{FancyVerbLine}{4}
94100
\setcounter{pp@next@reset}{1}
95101
\setcounter{@fnserial}{0}
96102
\setcounter{Item}{0}
97103
\setcounter{Hfootnote}{0}
98104
\setcounter{Hy@AnnotLevel}{0}
99-
\setcounter{bookmark@seq@number}{21}
105+
\setcounter{bookmark@seq@number}{23}
100106
\setcounter{parentequation}{0}
101107
\setcounter{section@level}{3}
102108
}

ebook/Backtracking.tex

Lines changed: 192 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,4 +1212,195 @@ \subsubsection{Solution}
12121212

12131213
\begin{Code}
12141214

1215-
\end{Code}
1215+
\end{Code}
1216+
1217+
\newpage
1218+
1219+
\section{Word Ladder} %%%%%%%%%%%%%%%%%%%%%%
1220+
1221+
\subsubsection{Description}
1222+
1223+
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
1224+
1225+
Only one letter can be changed at a time.
1226+
1227+
Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
1228+
1229+
For example,
1230+
1231+
Given:
1232+
1233+
beginWord = \code{"hit"}
1234+
1235+
endWord = \code{"cog"}
1236+
1237+
wordList = \code{["hot","dot","dog","lot","log","cog"]}
1238+
1239+
As one shortest transformation is \code{"hit" -> "hot" -> "dot" -> "dog" -> "cog"},
1240+
1241+
return its length 5.
1242+
1243+
\textbf{Note:}
1244+
1245+
Return 0 if there is no such transformation sequence.
1246+
1247+
All words have the same length.
1248+
1249+
All words contain only lowercase alphabetic characters.
1250+
1251+
You may assume no duplicates in the word list.
1252+
1253+
You may assume beginWord and endWord are non-empty and are not the same.
1254+
1255+
\newpage
1256+
1257+
\subsubsection{Solution I}
1258+
1259+
\begin{Code}
1260+
/**
1261+
* 要注意添加节点时要给单词从dict中删掉
1262+
*/
1263+
// 常规的BFS,耗时141ms
1264+
public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
1265+
wordList.remove(beginWord);
1266+
wordList.add(endWord);
1267+
1268+
Queue<String> queue = new LinkedList<>();
1269+
Queue<String> next = new LinkedList<>();
1270+
queue.add(beginWord);
1271+
1272+
int ladder = 1;
1273+
1274+
while (!queue.isEmpty()) {
1275+
String word = queue.poll();
1276+
1277+
StringBuilder sb = new StringBuilder(word);
1278+
for (int i = 0; i < word.length(); i++) {
1279+
char c = word.charAt(i);
1280+
for (int j = 0; j < 26; j++) {
1281+
if (j + 'a' == c) {
1282+
continue;
1283+
}
1284+
sb.setCharAt(i, (char) (j + 'a'));
1285+
String s = sb.toString();
1286+
1287+
if (s.equals(endWord)) {
1288+
return ladder + 1;
1289+
}
1290+
1291+
if (wordList.remove(s)) {
1292+
next.add(s);
1293+
}
1294+
}
1295+
sb.setCharAt(i, c);
1296+
}
1297+
1298+
if (queue.isEmpty()) {
1299+
queue.addAll(next);
1300+
next.clear();
1301+
ladder++;
1302+
}
1303+
}
1304+
1305+
return 0;
1306+
}
1307+
\end{Code}
1308+
1309+
\newpage
1310+
1311+
\subsubsection{Solution II}
1312+
1313+
\begin{Code}
1314+
// 采用双端BFS,耗时28ms
1315+
public int ladderLength2(String beginWord, String endWord, Set<String> wordList) {
1316+
Set<String> beginSet = new HashSet<>();
1317+
beginSet.add(beginWord);
1318+
1319+
Set<String> endSet = new HashSet<String>();
1320+
endSet.add(endWord);
1321+
1322+
int length = 1;
1323+
1324+
while (!beginSet.isEmpty() && !endSet.isEmpty()) {
1325+
if (beginSet.size() > endSet.size()) {
1326+
Set<String> temp = beginSet;
1327+
beginSet = endSet;
1328+
endSet = temp;
1329+
}
1330+
1331+
Set<String> nextSet = new HashSet<String>();
1332+
1333+
for (String word : beginSet) {
1334+
char[] wordArr = word.toCharArray();
1335+
1336+
for (int i = 0; i < wordArr.length; i++) {
1337+
char c = wordArr[i];
1338+
1339+
for (int j = 0; j < 26; j++) {
1340+
if ('a' + j == c) {
1341+
continue;
1342+
}
1343+
wordArr[i] = (char) ('a' + j);
1344+
String s = String.valueOf(wordArr);
1345+
1346+
if (endSet.contains(s)) {
1347+
return length + 1;
1348+
}
1349+
1350+
if (wordList.contains(s)) {
1351+
nextSet.add(s);
1352+
wordList.remove(s);
1353+
}
1354+
}
1355+
wordArr[i] = c;
1356+
}
1357+
}
1358+
1359+
beginSet = nextSet;
1360+
length++;
1361+
}
1362+
1363+
return 0;
1364+
}
1365+
\end{Code}
1366+
1367+
\newpage
1368+
1369+
\section{Word Ladder II} %%%%%%%%%%%%%%%%%%%%%%
1370+
1371+
\subsubsection{Description}
1372+
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:
1373+
1374+
1. Only one letter can be changed at a time
1375+
1376+
2. Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
1377+
1378+
For example,
1379+
1380+
Given:
1381+
1382+
beginWord = \code{"hit"}
1383+
1384+
endWord = \code{"cog"}
1385+
1386+
wordList = \code{["hot","dot","dog","lot","log","cog"]}
1387+
1388+
Return
1389+
\begin{Code}
1390+
[
1391+
["hit","hot","dot","dog","cog"],
1392+
["hit","hot","lot","log","cog"]
1393+
]
1394+
\end{Code}
1395+
1396+
Note:
1397+
1398+
Return an empty list if there is no such transformation sequence.
1399+
1400+
All words have the same length.
1401+
1402+
All words contain only lowercase alphabetic characters.
1403+
1404+
You may assume no duplicates in the word list.
1405+
1406+
You may assume beginWord and endWord are non-empty and are not the same.

ebook/leetcode.log

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
This is XeTeX, Version 3.14159265-2.6-0.99996 (TeX Live 2016) (preloaded format=xelatex 2017.9.4) 5 SEP 2017 17:54
1+
This is XeTeX, Version 3.14159265-2.6-0.99996 (TeX Live 2016) (preloaded format=xelatex 2017.9.4) 5 SEP 2017 18:08
22
entering extended mode
33
restricted \write18 enabled.
44
%&-line parsing enabled.
@@ -1935,7 +1935,7 @@ File: images/sudoku.png Graphic file (type QTm)
19351935
<use "images/sudoku.png" >
19361936
File: images/sudoku2.png Graphic file (type QTm)
19371937
<use "images/sudoku2.png" > [25]
1938-
[26] [27]) [28]
1938+
[26] [27] [28] [29] [30] [31]) [32]
19391939
No file leetcode.ind.
19401940
Package atveryend Info: Empty hook `BeforeClearDocument' on input line 42.
19411941
Package atveryend Info: Empty hook `AfterLastShipout' on input line 42.
@@ -1947,10 +1947,10 @@ Package atveryend Info: Empty hook `AtVeryVeryEnd' on input line 42.
19471947
Here is how much of TeX's memory you used:
19481948
29203 strings out of 493591
19491949
553766 string characters out of 6143547
1950-
574513 words of memory out of 5000000
1950+
579004 words of memory out of 5000000
19511951
32225 multiletter control sequences out of 15000+600000
19521952
5562 words of font info for 59 fonts, out of 8000000 for 9000
19531953
1347 hyphenation exceptions out of 8191
19541954
65i,11n,77p,10414b,603s stack positions out of 5000i,500n,10000p,200000b,80000s
19551955

1956-
Output written on leetcode.pdf (30 pages).
1956+
Output written on leetcode.pdf (34 pages).

ebook/leetcode.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@
1919
\BOOKMARK [1][-]{section.1.18}{\376\377\0001\000.\0001\0008\000\040\000S\000u\000d\000o\000k\000u\000\040\000S\000o\000l\000v\000e\000r}{chapter.1}% 19
2020
\BOOKMARK [1][-]{section.1.19}{\376\377\0001\000.\0001\0009\000\040\000C\000o\000m\000b\000i\000n\000a\000t\000i\000o\000n\000s}{chapter.1}% 20
2121
\BOOKMARK [1][-]{section.1.20}{\376\377\0001\000.\0002\0000\000\040\000R\000e\000s\000t\000o\000r\000e\000\040\000I\000P\000\040\000A\000d\000d\000r\000e\000s\000s\000e\000s}{chapter.1}% 21
22+
\BOOKMARK [1][-]{section.1.21}{\376\377\0001\000.\0002\0001\000\040\000W\000o\000r\000d\000\040\000L\000a\000d\000d\000e\000r}{chapter.1}% 22
23+
\BOOKMARK [1][-]{section.1.22}{\376\377\0001\000.\0002\0002\000\040\000W\000o\000r\000d\000\040\000L\000a\000d\000d\000e\000r\000\040\000I\000I}{chapter.1}% 23

ebook/leetcode.pdf

10.2 KB
Binary file not shown.

ebook/leetcode.synctex.gz

11.7 KB
Binary file not shown.

ebook/leetcode.toc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,9 @@
6464
\contentsline {section}{\numberline {1.20}Restore IP Addresses}{28}{section.1.20}
6565
\contentsline {subsubsection}{Description}{28}{section*.45}
6666
\contentsline {subsubsection}{Solution}{28}{section*.46}
67+
\contentsline {section}{\numberline {1.21}Word Ladder}{29}{section.1.21}
68+
\contentsline {subsubsection}{Description}{29}{section*.47}
69+
\contentsline {subsubsection}{Solution I}{30}{section*.48}
70+
\contentsline {subsubsection}{Solution II}{31}{section*.49}
71+
\contentsline {section}{\numberline {1.22}Word Ladder II}{32}{section.1.22}
72+
\contentsline {subsubsection}{Description}{32}{section*.50}

0 commit comments

Comments
 (0)