diff --git a/C++/README.md b/C++/README.md new file mode 100644 index 00000000..2fed273e --- /dev/null +++ b/C++/README.md @@ -0,0 +1,5 @@ +#C++版 +----------------- +**下载**:手写代码必备手册(C++版).pdf + +书的内容与Java版一摸一样,不过代码是用C++写的。本书的代码使用 C++ 11 标准。 diff --git a/C++/abstract.tex b/C++/abstract.tex new file mode 100644 index 00000000..5d9d2486 --- /dev/null +++ b/C++/abstract.tex @@ -0,0 +1,25 @@ +\subsubsection{内容简介} +本书的目标读者是准备去北美找工作的码农,也适用于在国内找工作的码农,以及刚接触ACM算法竞赛的新手。 + +本书包含了 LeetCode Online Judge(\myurl{http://leetcode.com/onlinejudge})所有题目的答案, +所有代码经过精心编写,编码规范良好,适合读者反复揣摩,模仿,甚至在纸上默写。 + +全书的代码,使用C++ 11的编写,并在 LeetCode Online Judge 上测试通过。本书中的代码规范,跟在公司中的工程规范略有不同,为了使代码短(方便迅速实现): + +\begindot +\item 所有代码都是单一文件。这是因为一般OJ网站,提交代码的时候只有一个文本框,如果还是 +按照标准做法,比如分为头文件.h和源代码.cpp,无法在网站上提交; + +\item 大量使用 STL,让代码更短,shorter is better; + +\item 不提倡防御式编程。不需要检查malloc()/new 返回的指针是否为NULL;不需要检查内部函数入口 +参数的有效性;使用纯C基于对象编程时,调用对象的成员方法,不需要检查对象自身是否为NULL。 +\myenddot + +本手册假定读者已经学过《数据结构》\footnote{《数据结构》,严蔚敏等著,清华大学出版社, +\myurl{http://book.douban.com/subject/2024655/}}, +《算法》\footnote{《Algorithms》,Robert Sedgewick, Addison-Wesley Professional, \myurl{http://book.douban.com/subject/4854123/}} +这两门课,熟练掌握C++或Java。 + +\subsubsection{GitCafe地址} +本书是开源的,项目地址:\myurl{https://gitcafe.com/soulmachine/LeetCode} diff --git a/C++/chapProgrammingTrick.tex b/C++/chapProgrammingTrick.tex new file mode 100644 index 00000000..e3d97db3 --- /dev/null +++ b/C++/chapProgrammingTrick.tex @@ -0,0 +1,9 @@ +\chapter{编程技巧} +把较大的数组放在main函数外,作为全局变量,这样可以防止栈溢出,因为栈的大小是有限制的。 + +如果能够预估栈,队列的上限,则不要用\fn{std::stack, std::queue},使用数组来模拟,这样速度最快。 + +输入数据一般放在全局变量,且在运行过程中不要修改这些变量。 + +在判断两个浮点数a和b是否相等时,不要用\fn{a==b},应该判断二者之差的绝对值\fn{fabs(a-b)}是否小于某个阀值,例如\fn{1e-9}。 + diff --git a/C++/format.cls b/C++/format.cls new file mode 100644 index 00000000..1e0a4a28 --- /dev/null +++ b/C++/format.cls @@ -0,0 +1,100 @@ +\usepackage[centering,paperwidth=180mm,paperheight=230mm,% +body={390pt,530pt},marginparsep=10pt,marginpar=50pt]{geometry} +\usepackage{color} +\usepackage{enumitem} +\usepackage{fancyvrb} +\usepackage[bottom,perpage,symbol*]{footmisc} +\usepackage{graphicx} +\usepackage[hidelinks]{hyperref} +\usepackage{makeidx} +\usepackage[toc]{multitoc} +\usepackage{pifont} +\usepackage{underscore} +\usepackage{amsmath} + +\DefineFNsymbols*{chinese}{{\ding{172}}{\ding{173}}{\ding{174}}{\ding{175}}% +{\ding{176}}{\ding{177}}{\ding{178}}{\ding{179}}{\ding{180}}{\ding{181}}} +\setfnsymbol{chinese} + +\hypersetup{bookmarksnumbered=true,bookmarksdepth=2} + +\CTEXsetup[number={\thechapter}]{chapter} +\CTEXsetup[format+={\raggedleft}]{chapter} +\CTEXsetup[beforeskip={10pt}]{chapter} +\CTEXsetup[afterskip={30pt}]{chapter} +\def\CTEX@chapter@aftername{\par} % \CTEXsetup[aftername={\par}]{chapter} +\CTEXsetup[format+={\raggedright}]{section} +\CTEXsetup[beforeskip={-3.0ex plus -1ex minus -.2ex}]{section} +\CTEXsetup[afterskip={2.3ex plus .2ex minus 0.2ex}]{section} + +\renewcommand \thefigure{\thechapter-\arabic{figure}} +\renewcommand \thetable{\thechapter-\arabic{table}} + +\newcommand\figcaption[1]{\def\@captype{figure}\caption{#1}} +\newcommand\tabcaption[1]{\def\@captype{table}\caption{#1}} + +\long\def\@caption#1[#2]#3{% + \addcontentsline{\csname ext@#1\endcsname}{#1}% + {\protect\numberline{\csname fnum@#1\endcsname}{ \ignorespaces #2}}% change "the" to "fnum@" + \normalsize + \@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #3}} + +\long\def\@makecaption#1#2{% + \vskip\abovecaptionskip + \sbox\@tempboxa{#1\quad#2}% + \ifdim \wd\@tempboxa >\hsize + #1\quad#2\par + \else + \global \@minipagefalse + \hb@xt@\hsize{\hfil\box\@tempboxa\hfil}% + \fi + \vskip\belowcaptionskip} + +\setlength\abovecaptionskip{0pt} + +%\setmainfont{Times New Roman} +\setmainfont{Linux Libertine O} +%\setmainfont{TeX Gyre Pagella} +\newfontfamily\urlfont{PT Sans Narrow} +%\setmonofont[AutoFakeBold=1.6,AutoFakeSlant=0.17,Mapping=tex-text-tt]{Inconsolata} +\setCJKfamilyfont{zhyou}{YouYuan} + +\newcommand{\fn}[1]{\texttt{#1}} +\newcommand{\sfn}[1]{\texttt{\small #1}} +\newcommand{\kw}[1]{\textsf{#1}} +\newcommand{\myurl}[1]{{\urlfont #1}} +\newcommand{\mpar}[1]{\marginpar[\hfill\kaishu #1]{\kaishu #1}} +\newcommand{\mn}[1]{\texttt{\bs #1}} +\renewcommand{\today}{\the\year-\the\month-\the\day} +\newcommand\bs{\textbackslash} + +\newcommand\begindot{\begin{itemize} +[itemsep=2pt plus 2pt minus 2pt,% +topsep=3pt plus 2pt minus 2pt,% +parsep=0pt plus 2pt minus 2pt]} +\newcommand\myenddot{\end{itemize}} + +\newcommand\beginnum{\begin{enumerate} +[itemsep=2pt plus 2pt minus 2pt,% +topsep=3pt plus 2pt minus 2pt,% +parsep=0pt plus 2pt minus 2pt]} +\newcommand\myendnum{\end{enumerate}} + +\DefineVerbatimEnvironment% + {Code}{Verbatim} + {fontsize=\small,baselinestretch=0.9,xleftmargin=3mm} + +\raggedbottom +%\setlength{\parskip}{1ex plus .5ex minus .5ex} + +\input{verbatim.cls} +\DefineVerbatimEnvironment% + {Codex}{Verbatim} + {fontsize=\small,baselinestretch=0.9,xleftmargin=3mm,% + frame=lines,labelposition=all,framesep=5pt} + +\DefineVerbatimEnvironment% + {Code}{Verbatim} + {fontsize=\small,baselinestretch=0.9,xleftmargin=3mm} + +\makeindex diff --git a/C++/leetcode-cpp.tex b/C++/leetcode-cpp.tex new file mode 100644 index 00000000..8a54bd39 --- /dev/null +++ b/C++/leetcode-cpp.tex @@ -0,0 +1,43 @@ +\documentclass[10pt,adobefonts,fancyhdr,hyperref,UTF8]{ctexbook} + +\usepackage{multirow} +% for \soul 删除线 +\usepackage{ulem} +% 表头斜线 +\usepackage{diagbox} + +\makeatletter +\input{format.cls} +\makeatother + +\begin{document} +\sloppy +\newcommand\BookTitle{LeetCode题解} +\pagestyle{fancy} +\fancyhf{} +\fancyhead[RE]{\normalfont\small\rmfamily\nouppercase{\leftmark}} +\fancyhead[LO]{\normalfont\small\rmfamily\nouppercase{\rightmark}} +\fancyhead[LE,RO]{\thepage} +%\fancyfoot[LE,LO]{\small\normalfont\youyuan\BookTitle} +%\fancyfoot[RE,RO]{\textsf{\small \color{blue} https://gitcafe.com/soulmachine/LeetCode}} + +\makeatletter +\@openrightfalse +\makeatother + +\frontmatter % 开始前言目录,页码用罗马数字 + +\include{title} + +\tableofcontents + +\mainmatter % 开始正文,页码用阿拉伯数字 + +\graphicspath{{diagrams/}} + +\include{chapProgrammingTrick} + +\appendix % 开始附录,章用字母编号 +\printindex + +\end{document} diff --git a/C++/tex-text-tt.map b/C++/tex-text-tt.map new file mode 100644 index 00000000..14277489 --- /dev/null +++ b/C++/tex-text-tt.map @@ -0,0 +1,28 @@ +; TECkit mapping for TeX input conventions <-> Unicode characters + +LHSName "TeX-text" +RHSName "UNICODE" + +pass(Unicode) + +; ligatures from Knuth's original CMR fonts +;U+002D U+002D <> U+2013 ; -- -> en dash +;U+002D U+002D U+002D <> U+2014 ; --- -> em dash + +;U+0027 <> U+2019 ; ' -> right single quote +;U+0027 U+0027 <> U+201D ; '' -> right double quote +;U+0022 > U+201D ; " -> right double quote + +;U+0060 <> U+2018 ; ` -> left single quote +;U+0060 U+0060 <> U+201C ; `` -> left double quote + +;U+0021 U+0060 <> U+00A1 ; !` -> inverted exclam +;U+003F U+0060 <> U+00BF ; ?` -> inverted question + +; additions supported in T1 encoding +;U+002C U+002C <> U+201E ; ,, -> DOUBLE LOW-9 QUOTATION MARK +;U+003C U+003C <> U+00AB ; << -> LEFT POINTING GUILLEMET +;U+003E U+003E <> U+00BB ; >> -> RIGHT POINTING GUILLEMET + +;U+0027 <> U+2019 ; ' -> right single quote +;U+0022 > U+201D ; " -> right double quote diff --git a/C++/tex-text-tt.tec b/C++/tex-text-tt.tec new file mode 100644 index 00000000..c5e48ff0 Binary files /dev/null and b/C++/tex-text-tt.tec differ diff --git a/C++/title.aux b/C++/title.aux new file mode 100644 index 00000000..2bd87dca --- /dev/null +++ b/C++/title.aux @@ -0,0 +1,36 @@ +\relax +\providecommand\hyper@newdestlabel[2]{} +\@writefile{toc}{\contentsline {subsubsection}{内容简介}{i}{section*.1}} +\FN@pp@footnote@aux{1}{i} +\FN@pp@footnote@aux{2}{i} +\@writefile{toc}{\contentsline {subsubsection}{GitCafe地址}{i}{section*.2}} +\FN@pp@footnotehinttrue +\@setckpt{title}{ +\setcounter{page}{2} +\setcounter{equation}{0} +\setcounter{enumi}{0} +\setcounter{enumii}{0} +\setcounter{enumiii}{0} +\setcounter{enumiv}{0} +\setcounter{footnote}{0} +\setcounter{mpfootnote}{0} +\setcounter{CTEX@sectiondepth}{2} +\setcounter{part}{0} +\setcounter{chapter}{0} +\setcounter{section}{0} +\setcounter{subsection}{0} +\setcounter{subsubsection}{0} +\setcounter{paragraph}{0} +\setcounter{subparagraph}{0} +\setcounter{figure}{0} +\setcounter{table}{0} +\setcounter{FancyVerbLine}{0} +\setcounter{pp@next@reset}{3} +\setcounter{@fnserial}{2} +\setcounter{Item}{0} +\setcounter{Hfootnote}{2} +\setcounter{Hy@AnnotLevel}{0} +\setcounter{bookmark@seq@number}{0} +\setcounter{parentequation}{0} +\setcounter{section@level}{3} +} diff --git a/C++/title.tex b/C++/title.tex new file mode 100644 index 00000000..d8d996e1 --- /dev/null +++ b/C++/title.tex @@ -0,0 +1,22 @@ +\thispagestyle{plain} +\begin{center} + {\LARGE\textbf{\BookTitle}} + + \vspace{1em} + {\large 戴方勤 (soulmachine@gmail.com)} + + \vspace{1ex} + \myurl{https://gitcafe.com/soulmachine/LeetCode} + + \vspace{1ex} + 最后更新 \today + + \vspace{1em} + \textbf{\large 版权声明} +\end{center} +\noindent 本作品采用“Creative Commons 署名-非商业性使用-相同方式共享 3.0 Unported许可协议 +(cc by-nc-sa)”进行许可。 +\texttt{\small http://creativecommons.org/licenses/by-nc-sa/3.0/} + +\vspace{1em} +\input{abstract} \ No newline at end of file diff --git a/C++/verbatim.cls b/C++/verbatim.cls new file mode 100644 index 00000000..fa9ce7a6 --- /dev/null +++ b/C++/verbatim.cls @@ -0,0 +1,112 @@ +\def\FV@SetLineWidth{% + \if@FV@ResetMargins\else + \advance\leftmargin\@totalleftmargin + \fi + \advance\leftmargin\FV@XLeftMargin\relax + \advance\rightmargin\FV@XRightMargin\relax + \linewidth\hsize + %\advance\linewidth-\leftmargin + %\advance\linewidth-\rightmargin + \hfuzz\FancyVerbHFuzz\relax} + + +\def\FV@SingleFrameLine#1{% +%% DG/SR modification end + \hbox to\z@{% + %\kern\leftmargin +%% DG/SR modification begin - Jun. 22, 1998 + \ifnum#1=\z@ + \let\FV@Label\FV@LabelBegin + \else + \let\FV@Label\FV@LabelEnd + \fi + \ifx\FV@Label\relax +%% DG/SR modification end + \FancyVerbRuleColor{\vrule \@width\linewidth \@height\FV@FrameRule}% +%% DG/SR modification begin - Jun. 22, 1998 + \else + \ifnum#1=\z@ + \setbox\z@\hbox{\strut\enspace\urlfont\FV@LabelBegin\strut}% + \else + \setbox\z@\hbox{\strut\enspace\urlfont\FV@LabelEnd\strut}% + \fi + \@tempdimb=\dp\z@ + \advance\@tempdimb -.5\ht\z@ + \@tempdimc=\linewidth + \advance\@tempdimc -\wd\z@ + %\divide\@tempdimc\tw@ + \ifnum#1=\z@ % Top line + \ifx\FV@LabelPositionTopLine\relax + \FancyVerbRuleColor{\vrule \@width\linewidth \@height\FV@FrameRule}% + \else + \FV@FrameLineWithLabel + \fi + \else % Bottom line + \ifx\FV@LabelPositionBottomLine\relax + \FancyVerbRuleColor{\vrule \@width\linewidth \@height\FV@FrameRule}% + \else + \FV@FrameLineWithLabel + \fi + \fi + \fi +%% DG/SR modification end + \hss}} + + +%% DG/SR modification begin - May. 19, 1998 +\def\FV@FrameLineWithLabel{% + \ht\z@\@tempdimb\dp\z@\@tempdimb% + \FancyVerbRuleColor{% + \raise 0.5ex\hbox{\vrule \@width\@tempdimc \@height\FV@FrameRule}% + \raise\@tempdimb\box\z@}} +%% DG/SR modification end + + +\def\FV@EndListFrame@Lines{% + \begingroup + %\vskip 0.5ex + \baselineskip\z@skip + \kern\FV@FrameSep\relax +%% DG/SR modification begin - May. 19, 1998 +%% \FV@SingleFrameLine + \FV@SingleFrameLine{\@ne}% +%% DG/SR modification end + \endgroup} + +\newskip\mytopsep +\setlength{\mytopsep}{4pt plus 2pt minus 3pt} + +\def\FV@ListVSpace{% + \@topsepadd\mytopsep + \if@noparlist\advance\@topsepadd\partopsep\fi + \if@inlabel + \vskip\parskip + \else + \if@nobreak + \vskip\parskip + \clubpenalty\@M + \else + \addpenalty\@beginparpenalty + \@topsep\@topsepadd + \advance\@topsep\parskip + \addvspace\@topsep + \fi + \fi + %\showthe \@topsepadd + %\showthe \topsep + %\showthe \partopsep + %\showthe \parskip + \global\@nobreakfalse + \global\@inlabelfalse + \global\@minipagefalse + \global\@newlistfalse} + +\def\FV@EndList{% + \FV@ListProcessLastLine + \FV@EndListFrame + %\showthe \@topsepadd + \@endparenv + \endgroup + \@endpetrue} + +\def\theFancyVerbLine{\sffamily\scriptsize\arabic{FancyVerbLine}} diff --git a/Java/README.md b/Java/README.md new file mode 100644 index 00000000..8886a0b4 --- /dev/null +++ b/Java/README.md @@ -0,0 +1,3 @@ +#Java版 +----------------- +书的内容与C++版一摸一样,不过代码是用Java写的。本书的代码要求 Java 6 以上。 diff --git a/README.md b/README.md new file mode 100644 index 00000000..3ec7197c --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +#LeetCode题解 +----------------- +**下载**:LeetCodet题解(C++版).pdf + +C++ 文件夹下是C++版,内容一摸一样,代码是用C++写的, + +Java 文件夹下是Java版,内容一摸一样,代码是用Java写的 + +##LaTeX模板 +本书使用的是陈硕开源的[模板](https://github.com/chenshuo/typeset)。这个模板制作精良,很有taste,感谢陈硕 :) + +##在Windows下编译 +1. 安装Tex Live 2012 。把bin目录例如`D:\texlive\2012\bin\win32`加入PATH环境变量。 +2. 安装TeXstudio +3. (可选)启动Tex Live Manager,更新所有已安装的软件包。 +4. 安装字体。在Windows下,这个LaTex模板额外使用了5个字体,下载地址 +5. 配置TeXstudio。 + + 启动Texstudio,选择 `Options-->Configure Texstudio-->Commands`,XeLaTex 设置为 `xelatex -synctex=1 -interaction=nonstopmode %.tex`; + + 选择 `Options-->Configure Texstudio-->Build` + + Build & View 由默认的 PDF Chain 改为 Compile & View; + + Default Compiler 由默认的PdfLaTex 修改为 XeLaTex ; + + PDF Viewer 改为 “Internal PDF Viewer(windowed)”,这样预览时会弹出一个独立的窗口,这样比较方便。 + +6. 编译。用TeXstudio打开`leetcode.tex`,点击界面上的绿色箭头就可以开始编译了。 + + 在下方的窗口可以看到TeXstudio正在使用的编译命令是`xelatex -synctex=1 -interaction=nonstopmode "leetcode".tex` + +##如何贡献代码 +编译通过后,就具备了完整的LaTeX编译环境了。 + +本书模板已经写好了,基本上不需要很多LaTeX知识就可以动手了。 + +欢迎给本书添加内容或纠正错误,在自己本地编译成PDF,预览没问题后,就可以发pull request过来了。 \ No newline at end of file