Skip to content

Commit 9fb8b62

Browse files
author
liwentian
committed
fd
1 parent 6a7afd2 commit 9fb8b62

File tree

6 files changed

+98
-9
lines changed

6 files changed

+98
-9
lines changed

ebook/Backtracking.aux

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@
2828
\@writefile{toc}{\contentsline {section}{\numberline {1.7}Word Search II}{8}{section.1.7}}
2929
\@writefile{toc}{\contentsline {subsubsection}{Description}{8}{section*.13}}
3030
\@writefile{toc}{\contentsline {subsubsection}{Solution}{8}{section*.14}}
31+
\@writefile{toc}{\contentsline {section}{\numberline {1.8}Word Break}{10}{section.1.8}}
32+
\@writefile{toc}{\contentsline {subsubsection}{Description}{10}{section*.15}}
33+
\@writefile{toc}{\contentsline {subsubsection}{Solution}{10}{section*.16}}
34+
\@writefile{toc}{\contentsline {section}{\numberline {1.9}Word Break II}{11}{section.1.9}}
35+
\@writefile{toc}{\contentsline {subsubsection}{Description}{11}{section*.17}}
36+
\@writefile{toc}{\contentsline {subsubsection}{Solution}{11}{section*.18}}
3137
\FN@pp@footnotehinttrue
3238
\@setckpt{Backtracking}{
33-
\setcounter{page}{10}
39+
\setcounter{page}{12}
3440
\setcounter{equation}{0}
3541
\setcounter{enumi}{0}
3642
\setcounter{enumii}{0}
@@ -40,20 +46,20 @@
4046
\setcounter{mpfootnote}{0}
4147
\setcounter{part}{0}
4248
\setcounter{chapter}{1}
43-
\setcounter{section}{7}
49+
\setcounter{section}{9}
4450
\setcounter{subsection}{0}
4551
\setcounter{subsubsection}{0}
4652
\setcounter{paragraph}{0}
4753
\setcounter{subparagraph}{0}
4854
\setcounter{figure}{1}
4955
\setcounter{table}{0}
50-
\setcounter{FancyVerbLine}{41}
56+
\setcounter{FancyVerbLine}{25}
5157
\setcounter{pp@next@reset}{1}
5258
\setcounter{@fnserial}{0}
5359
\setcounter{Item}{0}
5460
\setcounter{Hfootnote}{0}
5561
\setcounter{Hy@AnnotLevel}{0}
56-
\setcounter{bookmark@seq@number}{8}
62+
\setcounter{bookmark@seq@number}{10}
5763
\setcounter{parentequation}{0}
5864
\setcounter{section@level}{3}
5965
}

ebook/Backtracking.tex

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,4 +402,85 @@ \subsubsection{Solution}
402402
dfs(set, board, i, j - 1, trie);
403403
board[i][j] ^= '#';
404404
}
405-
\end{Code}
405+
\end{Code}
406+
407+
\newpage
408+
409+
\section{Word Break}
410+
411+
\subsubsection{Description}
412+
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.
413+
414+
For example, given
415+
s = \code{"leetcode"},
416+
dict = \code{["leet", "code"]}.
417+
418+
Return true because \code{"leetcode"} can be segmented as \code{"leet code"}.
419+
420+
\subsubsection{Solution}
421+
422+
\begin{Code}
423+
public boolean wordBreak(String s, List<String> wordDict) {
424+
int n = s.length();
425+
426+
boolean[] dp = new boolean[n + 1];
427+
dp[0] = true;
428+
429+
for (int i = 1; i <= s.length(); i++) {
430+
for (String word : wordDict) {
431+
int j = i - word.length();
432+
if (j >= 0 && dp[j] && s.substring(j, i).equals(word)) {
433+
dp[i] = true;
434+
break;
435+
}
436+
}
437+
}
438+
439+
return dp[n];
440+
}
441+
\end{Code}
442+
443+
\newpage
444+
445+
\section{Word Break II}
446+
447+
\subsubsection{Description}
448+
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. You may assume the dictionary does not contain duplicate words.
449+
450+
Return all such possible sentences.
451+
452+
For example, given
453+
s = \code{"catsanddog"},
454+
dict = \code{["cat", "cats", "and", "sand", "dog"]}.
455+
456+
A solution is \code{["cats and dog", "cat sand dog"]}.
457+
458+
\subsubsection{Solution}
459+
460+
\begin{Code}
461+
public List<String> wordBreak(String s, List<String> wordDict) {
462+
HashMap<String, List<String>> cache = new HashMap<>();
463+
cache.put("", Arrays.asList(""));
464+
return dfs(s, new HashSet<String>(wordDict), cache);
465+
}
466+
467+
private List<String> dfs(String s, HashSet<String> wordDict, HashMap<String, List<String>> cache) {
468+
if (cache.containsKey(s)) {
469+
return cache.get(s);
470+
}
471+
List<String> result = new LinkedList<>();
472+
for (int i = 0; i < s.length(); i++) {
473+
String t = s.substring(i);
474+
if (wordDict.contains(t)) {
475+
List<String> list = dfs(s.substring(0, i), wordDict, cache);
476+
if (list != null) {
477+
for (String ss : list) {
478+
result.add((ss.length() > 0 ? ss + " " : "") + t);
479+
}
480+
}
481+
}
482+
}
483+
cache.put(s, result);
484+
return result;
485+
}
486+
\end{Code}

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 16:03
1+
This is XeTeX, Version 3.14159265-2.6-0.99996 (TeX Live 2016) (preloaded format=xelatex 2017.9.4) 5 SEP 2017 16:09
22
entering extended mode
33
restricted \write18 enabled.
44
%&-line parsing enabled.
@@ -1906,7 +1906,7 @@ File: images/phone.png Graphic file (type QTm)
19061906
. with NFSS spec.: <->"Latin Modern Mono/BI/OT:script=latn;language=DFLT;"
19071907
. - 'bold italic small caps' (bx/itsc) with NFSS spec.:
19081908
.................................................
1909-
[3] [4] [5] [6] [7] [8]) [9]
1909+
[3] [4] [5] [6] [7] [8] [9] [10]) [11]
19101910
No file leetcode.ind.
19111911
Package atveryend Info: Empty hook `BeforeClearDocument' on input line 42.
19121912
Package atveryend Info: Empty hook `AfterLastShipout' on input line 42.
@@ -1918,10 +1918,10 @@ Package atveryend Info: Empty hook `AtVeryVeryEnd' on input line 42.
19181918
Here is how much of TeX's memory you used:
19191919
29160 strings out of 493591
19201920
551638 string characters out of 6143547
1921-
568723 words of memory out of 5000000
1921+
569017 words of memory out of 5000000
19221922
32193 multiletter control sequences out of 15000+600000
19231923
5076 words of font info for 53 fonts, out of 8000000 for 9000
19241924
1347 hyphenation exceptions out of 8191
19251925
65i,11n,77p,10414b,588s stack positions out of 5000i,500n,10000p,200000b,80000s
19261926

1927-
Output written on leetcode.pdf (9 pages).
1927+
Output written on leetcode.pdf (11 pages).

ebook/leetcode.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
\BOOKMARK [1][-]{section.1.5}{\376\377\0001\000.\0005\000\040\000P\000e\000r\000m\000u\000t\000a\000t\000i\000o\000n\000s}{chapter.1}% 6
77
\BOOKMARK [1][-]{section.1.6}{\376\377\0001\000.\0006\000\040\000W\000o\000r\000d\000\040\000S\000e\000a\000r\000c\000h}{chapter.1}% 7
88
\BOOKMARK [1][-]{section.1.7}{\376\377\0001\000.\0007\000\040\000W\000o\000r\000d\000\040\000S\000e\000a\000r\000c\000h\000\040\000I\000I}{chapter.1}% 8
9+
\BOOKMARK [1][-]{section.1.8}{\376\377\0001\000.\0008\000\040\000W\000o\000r\000d\000\040\000B\000r\000e\000a\000k}{chapter.1}% 9
10+
\BOOKMARK [1][-]{section.1.9}{\376\377\0001\000.\0009\000\040\000W\000o\000r\000d\000\040\000B\000r\000e\000a\000k\000\040\000I\000I}{chapter.1}% 10

ebook/leetcode.pdf

3.91 KB
Binary file not shown.

ebook/leetcode.synctex.gz

5.11 KB
Binary file not shown.

0 commit comments

Comments
 (0)