Skip to content

Commit fee654e

Browse files
author
liwentian
committed
fd
1 parent 77b13ff commit fee654e

File tree

9 files changed

+180
-11
lines changed

9 files changed

+180
-11
lines changed

ebook/Backtracking.aux

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,18 @@
6262
\@writefile{toc}{\contentsline {section}{\numberline {1.17}Subsets II}{24}{section.1.17}}
6363
\@writefile{toc}{\contentsline {subsubsection}{Description}{24}{section*.39}}
6464
\@writefile{toc}{\contentsline {subsubsection}{Solution}{24}{section*.40}}
65+
\@writefile{toc}{\contentsline {section}{\numberline {1.18}Sudoku Solver}{25}{section.1.18}}
66+
\@writefile{toc}{\contentsline {subsubsection}{Description}{25}{section*.41}}
67+
\@writefile{toc}{\contentsline {subsubsection}{Solution}{25}{section*.42}}
68+
\@writefile{toc}{\contentsline {section}{\numberline {1.19}Combinations}{27}{section.1.19}}
69+
\@writefile{toc}{\contentsline {subsubsection}{Description}{27}{section*.43}}
70+
\@writefile{toc}{\contentsline {subsubsection}{Solution}{27}{section*.44}}
71+
\@writefile{toc}{\contentsline {section}{\numberline {1.20}Restore IP Addresses}{28}{section.1.20}}
72+
\@writefile{toc}{\contentsline {subsubsection}{Description}{28}{section*.45}}
73+
\@writefile{toc}{\contentsline {subsubsection}{Solution}{28}{section*.46}}
6574
\FN@pp@footnotehinttrue
6675
\@setckpt{Backtracking}{
67-
\setcounter{page}{25}
76+
\setcounter{page}{29}
6877
\setcounter{equation}{0}
6978
\setcounter{enumi}{0}
7079
\setcounter{enumii}{0}
@@ -74,20 +83,20 @@
7483
\setcounter{mpfootnote}{0}
7584
\setcounter{part}{0}
7685
\setcounter{chapter}{1}
77-
\setcounter{section}{17}
86+
\setcounter{section}{20}
7887
\setcounter{subsection}{0}
7988
\setcounter{subsubsection}{0}
8089
\setcounter{paragraph}{0}
8190
\setcounter{subparagraph}{0}
8291
\setcounter{figure}{1}
8392
\setcounter{table}{0}
84-
\setcounter{FancyVerbLine}{29}
93+
\setcounter{FancyVerbLine}{1}
8594
\setcounter{pp@next@reset}{1}
8695
\setcounter{@fnserial}{0}
8796
\setcounter{Item}{0}
8897
\setcounter{Hfootnote}{0}
8998
\setcounter{Hy@AnnotLevel}{0}
90-
\setcounter{bookmark@seq@number}{18}
99+
\setcounter{bookmark@seq@number}{21}
91100
\setcounter{parentequation}{0}
92101
\setcounter{section@level}{3}
93102
}

ebook/Backtracking.tex

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,3 +1070,146 @@ \subsubsection{Solution}
10701070
subsetsWithDup(nums, start + 1, list, path);
10711071
}
10721072
\end{Code}
1073+
1074+
\newpage
1075+
1076+
\section{Sudoku Solver} %%%%%%%%%%%%%%%%%%%%%%
1077+
1078+
\subsubsection{Description}
1079+
1080+
Write a program to solve a Sudoku puzzle by filling the empty cells.
1081+
1082+
Empty cells are indicated by the character \fn{'.'}.
1083+
1084+
You may assume that there will be only one unique solution.
1085+
1086+
\begin{center}
1087+
\includegraphics[width=100pt]{sudoku.png}
1088+
\includegraphics[width=100pt]{sudoku2.png}
1089+
\end{center}
1090+
1091+
\subsubsection{Solution}
1092+
1093+
\begin{Code}
1094+
public void solveSudoku(char[][] board) {
1095+
if (board.length == 0) {
1096+
return;
1097+
}
1098+
solve(board);
1099+
}
1100+
1101+
private boolean solve(char[][] board) {
1102+
for (int i = 0; i < board.length; i++) {
1103+
for (int j = 0; j < board[0].length; j++) {
1104+
if (board[i][j] == '.') {
1105+
for (char c = '1'; c <= '9'; c++) {
1106+
if (check(board, i, j, c)) {
1107+
board[i][j] = c;
1108+
1109+
if (solve(board)) {
1110+
return true;
1111+
} else {
1112+
board[i][j] = '.';
1113+
}
1114+
}
1115+
}
1116+
return false;
1117+
}
1118+
}
1119+
}
1120+
return true;
1121+
}
1122+
\end{Code}
1123+
1124+
\newpage
1125+
1126+
\begin{Code}
1127+
private boolean check(char[][] board, int row, int col, char c) {
1128+
for (int i = 0; i < 9; i++) {
1129+
if (board[row][i] == c) {
1130+
return false;
1131+
}
1132+
if (board[i][col] == c) {
1133+
return false;
1134+
}
1135+
1136+
int m = (row / 3) * 3 + i / 3;
1137+
int n = (col / 3) * 3 + i % 3;
1138+
1139+
if (board[m][n] == c) {
1140+
return false;
1141+
}
1142+
}
1143+
return true;
1144+
}
1145+
\end{Code}
1146+
1147+
\newpage
1148+
1149+
\section{Combinations} %%%%%%%%%%%%%%%%%%%%%%
1150+
1151+
\subsubsection{Description}
1152+
1153+
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
1154+
1155+
For example,
1156+
1157+
If n = 4 and k = 2, a solution is:
1158+
\begin{Code}
1159+
[
1160+
[2,4],
1161+
[3,4],
1162+
[2,3],
1163+
[1,2],
1164+
[1,3],
1165+
[1,4],
1166+
]
1167+
\end{Code}
1168+
1169+
\subsubsection{Solution}
1170+
1171+
\begin{Code}
1172+
public List<List<Integer>> combine(int n, int k) {
1173+
List<List<Integer>> result = new ArrayList<List<Integer>>();
1174+
List<Integer> path = new ArrayList<Integer>();
1175+
combine(n, k, result, path, 1);
1176+
return result;
1177+
}
1178+
1179+
private void combine(int n, int k, List<List<Integer>> result, List<Integer> path, int start) {
1180+
if (k == 0) {
1181+
result.add(new ArrayList<Integer>(path));
1182+
return;
1183+
}
1184+
1185+
if (start > n) {
1186+
return;
1187+
}
1188+
1189+
path.add(start);
1190+
combine(n, k - 1, result, path, start + 1);
1191+
path.remove(path.size() - 1);
1192+
1193+
combine(n, k, result, path, start + 1);
1194+
}
1195+
\end{Code}
1196+
1197+
\newpage
1198+
1199+
\section{Restore IP Addresses} %%%%%%%%%%%%%%%%%%%%%%
1200+
1201+
\subsubsection{Description}
1202+
1203+
Given a string containing only digits, restore it by returning all possible valid IP address combinations.
1204+
1205+
For example:
1206+
1207+
Given \code{"25525511135"},
1208+
1209+
return \code{["255.255.11.135", "255.255.111.35"]}. (Order does not matter)
1210+
1211+
\subsubsection{Solution}
1212+
1213+
\begin{Code}
1214+
1215+
\end{Code}

ebook/images/sudoku.png

14.3 KB
Loading

ebook/images/sudoku2.png

25.1 KB
Loading

ebook/leetcode.log

Lines changed: 12 additions & 7 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:43
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
22
entering extended mode
33
restricted \write18 enabled.
44
%&-line parsing enabled.
@@ -1930,7 +1930,12 @@ and I'll forget about whatever was undefined.
19301930
[14] [15] [16] [17] [18]
19311931
File: images/8-queens.png Graphic file (type QTm)
19321932
<use "images/8-queens.png" > [19] [20] [21] [22]
1933-
[23]) [24]
1933+
[23] [24]
1934+
File: images/sudoku.png Graphic file (type QTm)
1935+
<use "images/sudoku.png" >
1936+
File: images/sudoku2.png Graphic file (type QTm)
1937+
<use "images/sudoku2.png" > [25]
1938+
[26] [27]) [28]
19341939
No file leetcode.ind.
19351940
Package atveryend Info: Empty hook `BeforeClearDocument' on input line 42.
19361941
Package atveryend Info: Empty hook `AfterLastShipout' on input line 42.
@@ -1940,12 +1945,12 @@ Package atveryend Info: Empty hook `AtEndAfterFileList' on input line 42.
19401945
Package atveryend Info: Empty hook `AtVeryVeryEnd' on input line 42.
19411946
)
19421947
Here is how much of TeX's memory you used:
1943-
29199 strings out of 493591
1944-
553710 string characters out of 6143547
1945-
576323 words of memory out of 5000000
1946-
32223 multiletter control sequences out of 15000+600000
1948+
29203 strings out of 493591
1949+
553766 string characters out of 6143547
1950+
574513 words of memory out of 5000000
1951+
32225 multiletter control sequences out of 15000+600000
19471952
5562 words of font info for 59 fonts, out of 8000000 for 9000
19481953
1347 hyphenation exceptions out of 8191
19491954
65i,11n,77p,10414b,603s stack positions out of 5000i,500n,10000p,200000b,80000s
19501955

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

ebook/leetcode.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@
1616
\BOOKMARK [1][-]{section.1.15}{\376\377\0001\000.\0001\0005\000\040\000N\000-\000Q\000u\000e\000e\000n\000s\000\040\000I\000I}{chapter.1}% 16
1717
\BOOKMARK [1][-]{section.1.16}{\376\377\0001\000.\0001\0006\000\040\000S\000u\000b\000s\000e\000t\000s}{chapter.1}% 17
1818
\BOOKMARK [1][-]{section.1.17}{\376\377\0001\000.\0001\0007\000\040\000S\000u\000b\000s\000e\000t\000s\000\040\000I\000I}{chapter.1}% 18
19+
\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
20+
\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
21+
\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

ebook/leetcode.pdf

39.9 KB
Binary file not shown.

ebook/leetcode.synctex.gz

7.84 KB
Binary file not shown.

ebook/leetcode.toc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,12 @@
5555
\contentsline {section}{\numberline {1.17}Subsets II}{24}{section.1.17}
5656
\contentsline {subsubsection}{Description}{24}{section*.39}
5757
\contentsline {subsubsection}{Solution}{24}{section*.40}
58+
\contentsline {section}{\numberline {1.18}Sudoku Solver}{25}{section.1.18}
59+
\contentsline {subsubsection}{Description}{25}{section*.41}
60+
\contentsline {subsubsection}{Solution}{25}{section*.42}
61+
\contentsline {section}{\numberline {1.19}Combinations}{27}{section.1.19}
62+
\contentsline {subsubsection}{Description}{27}{section*.43}
63+
\contentsline {subsubsection}{Solution}{27}{section*.44}
64+
\contentsline {section}{\numberline {1.20}Restore IP Addresses}{28}{section.1.20}
65+
\contentsline {subsubsection}{Description}{28}{section*.45}
66+
\contentsline {subsubsection}{Solution}{28}{section*.46}

0 commit comments

Comments
 (0)