Skip to content

Commit 2371817

Browse files
author
liwentian
committed
fd
1 parent 8f121a3 commit 2371817

File tree

9 files changed

+78
-73
lines changed

9 files changed

+78
-73
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@
314314
|508|[Most Frequent Subtree Sum](https://leetcode.com/problems/most-frequent-subtree-sum/submissions/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/MostFrequentSubtreeSum.java)||
315315
|513|[Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/FindBottomLeftTreeValue.java)|100||
316316
|515|[Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/FindLargestValueInEachTreeRow.java)|100||
317+
|526|[Beautiful Arrangement](https://leetcode.com/problems/beautiful-arrangement/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/BeautifulArrangement.java)|80||
317318
|535|[Encode and Decode TinyURL](https://leetcode.com/problems/encode-and-decode-tinyurl/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/EncodeAndDecodeTinyURL.java)|100||
318319
|637|[Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/#/description)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/AverageOfLevelsInBinaryTree.java)|100||
319320
|652|[Find Duplicate Subtrees](https://leetcode.com/problems/find-duplicate-subtrees/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/FindDuplicateSubtrees.java)|70|开始还没思路|

ebook/Backtracking.aux

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,26 +118,23 @@
118118
\@writefile{toc}{\contentsline {section}{\numberline {1.35}Word Squares}{49}{section.1.35}}
119119
\@writefile{toc}{\contentsline {subsubsection}{Description}{49}{section*.77}}
120120
\@writefile{toc}{\contentsline {subsubsection}{Solution}{50}{section*.78}}
121-
\@writefile{toc}{\contentsline {section}{\numberline {1.36}Minimum Unique Word Abbreviation}{51}{section.1.36}}
121+
\@writefile{toc}{\contentsline {section}{\numberline {1.36}Beautiful Arrangement}{51}{section.1.36}}
122122
\@writefile{toc}{\contentsline {subsubsection}{Description}{51}{section*.79}}
123123
\@writefile{toc}{\contentsline {subsubsection}{Solution}{51}{section*.80}}
124-
\@writefile{toc}{\contentsline {section}{\numberline {1.37}Beautiful Arrangement}{52}{section.1.37}}
124+
\@writefile{toc}{\contentsline {section}{\numberline {1.37}Beautiful Arrangement II}{52}{section.1.37}}
125125
\@writefile{toc}{\contentsline {subsubsection}{Description}{52}{section*.81}}
126126
\@writefile{toc}{\contentsline {subsubsection}{Solution}{52}{section*.82}}
127-
\@writefile{toc}{\contentsline {section}{\numberline {1.38}Beautiful Arrangement II}{53}{section.1.38}}
127+
\@writefile{toc}{\contentsline {section}{\numberline {1.38}Flip Game}{53}{section.1.38}}
128128
\@writefile{toc}{\contentsline {subsubsection}{Description}{53}{section*.83}}
129129
\@writefile{toc}{\contentsline {subsubsection}{Solution}{53}{section*.84}}
130-
\@writefile{toc}{\contentsline {section}{\numberline {1.39}Flip Game}{54}{section.1.39}}
130+
\@writefile{toc}{\contentsline {section}{\numberline {1.39}Flip Game II}{54}{section.1.39}}
131131
\@writefile{toc}{\contentsline {subsubsection}{Description}{54}{section*.85}}
132132
\@writefile{toc}{\contentsline {subsubsection}{Solution}{54}{section*.86}}
133-
\@writefile{toc}{\contentsline {section}{\numberline {1.40}Flip Game II}{55}{section.1.40}}
133+
\@writefile{toc}{\contentsline {section}{\numberline {1.40}Android Unlock Patterns}{55}{section.1.40}}
134134
\@writefile{toc}{\contentsline {subsubsection}{Description}{55}{section*.87}}
135135
\@writefile{toc}{\contentsline {subsubsection}{Solution}{55}{section*.88}}
136-
\@writefile{toc}{\contentsline {section}{\numberline {1.41}Android Unlock Patterns}{56}{section.1.41}}
137-
\@writefile{toc}{\contentsline {subsubsection}{Description}{56}{section*.89}}
138-
\@writefile{toc}{\contentsline {subsubsection}{Solution}{56}{section*.90}}
139136
\@setckpt{Backtracking}{
140-
\setcounter{page}{57}
137+
\setcounter{page}{56}
141138
\setcounter{equation}{0}
142139
\setcounter{enumi}{0}
143140
\setcounter{enumii}{0}
@@ -147,7 +144,7 @@
147144
\setcounter{mpfootnote}{0}
148145
\setcounter{part}{0}
149146
\setcounter{chapter}{1}
150-
\setcounter{section}{41}
147+
\setcounter{section}{40}
151148
\setcounter{subsection}{0}
152149
\setcounter{subsubsection}{0}
153150
\setcounter{paragraph}{0}
@@ -160,7 +157,7 @@
160157
\setcounter{Item}{0}
161158
\setcounter{Hfootnote}{0}
162159
\setcounter{Hy@AnnotLevel}{0}
163-
\setcounter{bookmark@seq@number}{42}
160+
\setcounter{bookmark@seq@number}{41}
164161
\setcounter{parentequation}{0}
165162
\setcounter{section@level}{3}
166163
}

ebook/Backtracking.tex

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,37 +2235,6 @@ \subsubsection{Solution}
22352235

22362236
\newpage
22372237

2238-
\section{Minimum Unique Word Abbreviation} %%%%%%%%%%%%%%%%%%%%%%
2239-
2240-
\subsubsection{Description}
2241-
A string such as \code{"word"} contains the following abbreviations:
2242-
2243-
\code{["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]}
2244-
2245-
Given a target string and a set of strings in a dictionary, find an abbreviation of this target string with the smallest possible length such that it does not conflict with abbreviations of the strings in the dictionary.
2246-
2247-
Each number or letter in the abbreviation is considered length = 1. For example, the abbreviation \code{"a32bc"} has length = 4.
2248-
2249-
\textbf{Note:}
2250-
2251-
In the case of multiple answers as shown in the second example below, you may return any one of them.
2252-
2253-
Assume length of target string = m, and dictionary size = n. You may assume that m ≤ 21, n ≤ 1000, and log2(n) + m ≤ 20.
2254-
2255-
\textbf{Examples:}
2256-
2257-
\code{"apple"}, \code{["blade"] -> "a4"} (because \code{"5"} or \code{"4e"} conflicts with \code{"blade"})
2258-
2259-
\code{"apple"}, \code{["plain", "amber", "blade"] -> "1p3"} (other valid answers include \code{"ap3", "a3e", "2p2", "3le", "3l1"}).
2260-
2261-
\subsubsection{Solution}
2262-
2263-
\begin{Code}
2264-
2265-
\end{Code}
2266-
2267-
\newpage
2268-
22692238
\section{Beautiful Arrangement} %%%%%%%%%%%%%%%%%%%%%%
22702239
\subsubsection{Description}
22712240

@@ -2303,6 +2272,26 @@ \subsubsection{Description}
23032272

23042273
\subsubsection{Solution}
23052274
\begin{Code}
2275+
public int countArrangement(int N) {
2276+
int[] count = new int[1];
2277+
helper(N, 1, new boolean[N + 1], count);
2278+
return count[0];
2279+
}
2280+
2281+
private void helper(int N, int k, boolean[] visited, int[] count) {
2282+
if (k > N) {
2283+
count[0]++;
2284+
return;
2285+
}
2286+
2287+
for (int i = 1; i <= N; i++) {
2288+
if (!visited[i] && (i % k == 0 || k % i == 0)) {
2289+
visited[i] = true;
2290+
helper(N, k + 1, visited, count);
2291+
visited[i] = false;
2292+
}
2293+
}
2294+
}
23062295
\end{Code}
23072296

23082297
\newpage

ebook/leetcode.log

Lines changed: 7 additions & 17 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) 6 SEP 2017 10:26
1+
This is XeTeX, Version 3.14159265-2.6-0.99996 (TeX Live 2016) (preloaded format=xelatex 2017.9.4) 6 SEP 2017 21:10
22
entering extended mode
33
restricted \write18 enabled.
44
%&-line parsing enabled.
@@ -1969,36 +1969,26 @@ File: images/watch.jpg Graphic file (type QTm)
19691969
.
19701970
\FV@Error ...ncyVerb Error:^^J\space \space #1^^J}
19711971

1972-
l.2306 \end{Code}
1972+
l.2329 \end{Code}
19731973

19741974
This error message was generated by an \errmessage
19751975
command, so I can't give any explicit help.
19761976
Pretend that you're Hercule Poirot: Examine all clues,
19771977
and deduce the truth by order and method.
19781978

1979-
[52]
1980-
! FancyVerb Error:
1981-
Empty verbatim environment
1982-
.
1983-
\FV@Error ...ncyVerb Error:^^J\space \space #1^^J}
1984-
1985-
l.2340 \end{Code}
1986-
1987-
(That was another \errmessage.)
1988-
1989-
[53] [54] [55]
1979+
[52] [53] [54]
19901980
File: images/unlock.png Graphic file (type QTm)
19911981
<use "images/unlock.png" >
19921982
! FancyVerb Error:
19931983
Empty verbatim environment
19941984
.
19951985
\FV@Error ...ncyVerb Error:^^J\space \space #1^^J}
19961986

1997-
l.2440 \end{Code}
1987+
l.2429 \end{Code}
19981988

19991989
(That was another \errmessage.)
20001990

2001-
[56])
1991+
[55])
20021992
No file leetcode.ind.
20031993
Package atveryend Info: Empty hook `BeforeClearDocument' on input line 44.
20041994
Package atveryend Info: Empty hook `AfterLastShipout' on input line 44.
@@ -2010,10 +2000,10 @@ Package atveryend Info: Empty hook `AtVeryVeryEnd' on input line 44.
20102000
Here is how much of TeX's memory you used:
20112001
29222 strings out of 493591
20122002
554821 string characters out of 6143547
2013-
593944 words of memory out of 5000000
2003+
596680 words of memory out of 5000000
20142004
32241 multiletter control sequences out of 15000+600000
20152005
5562 words of font info for 59 fonts, out of 8000000 for 9000
20162006
1347 hyphenation exceptions out of 8191
20172007
65i,11n,77p,10414b,588s stack positions out of 5000i,500n,10000p,200000b,80000s
20182008

2019-
Output written on leetcode.pdf (60 pages).
2009+
Output written on leetcode.pdf (59 pages).

ebook/leetcode.out

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@
3434
\BOOKMARK [1][-]{section.1.33}{\376\377\0001\000.\0003\0003\000\040\000F\000a\000c\000t\000o\000r\000\040\000C\000o\000m\000b\000i\000n\000a\000t\000i\000o\000n\000s}{chapter.1}% 34
3535
\BOOKMARK [1][-]{section.1.34}{\376\377\0001\000.\0003\0004\000\040\000V\000a\000l\000i\000d\000\040\000W\000o\000r\000d\000\040\000S\000q\000u\000a\000r\000e}{chapter.1}% 35
3636
\BOOKMARK [1][-]{section.1.35}{\376\377\0001\000.\0003\0005\000\040\000W\000o\000r\000d\000\040\000S\000q\000u\000a\000r\000e\000s}{chapter.1}% 36
37-
\BOOKMARK [1][-]{section.1.36}{\376\377\0001\000.\0003\0006\000\040\000M\000i\000n\000i\000m\000u\000m\000\040\000U\000n\000i\000q\000u\000e\000\040\000W\000o\000r\000d\000\040\000A\000b\000b\000r\000e\000v\000i\000a\000t\000i\000o\000n}{chapter.1}% 37
38-
\BOOKMARK [1][-]{section.1.37}{\376\377\0001\000.\0003\0007\000\040\000B\000e\000a\000u\000t\000i\000f\000u\000l\000\040\000A\000r\000r\000a\000n\000g\000e\000m\000e\000n\000t}{chapter.1}% 38
39-
\BOOKMARK [1][-]{section.1.38}{\376\377\0001\000.\0003\0008\000\040\000B\000e\000a\000u\000t\000i\000f\000u\000l\000\040\000A\000r\000r\000a\000n\000g\000e\000m\000e\000n\000t\000\040\000I\000I}{chapter.1}% 39
40-
\BOOKMARK [1][-]{section.1.39}{\376\377\0001\000.\0003\0009\000\040\000F\000l\000i\000p\000\040\000G\000a\000m\000e}{chapter.1}% 40
41-
\BOOKMARK [1][-]{section.1.40}{\376\377\0001\000.\0004\0000\000\040\000F\000l\000i\000p\000\040\000G\000a\000m\000e\000\040\000I\000I}{chapter.1}% 41
42-
\BOOKMARK [1][-]{section.1.41}{\376\377\0001\000.\0004\0001\000\040\000A\000n\000d\000r\000o\000i\000d\000\040\000U\000n\000l\000o\000c\000k\000\040\000P\000a\000t\000t\000e\000r\000n\000s}{chapter.1}% 42
37+
\BOOKMARK [1][-]{section.1.36}{\376\377\0001\000.\0003\0006\000\040\000B\000e\000a\000u\000t\000i\000f\000u\000l\000\040\000A\000r\000r\000a\000n\000g\000e\000m\000e\000n\000t}{chapter.1}% 37
38+
\BOOKMARK [1][-]{section.1.37}{\376\377\0001\000.\0003\0007\000\040\000B\000e\000a\000u\000t\000i\000f\000u\000l\000\040\000A\000r\000r\000a\000n\000g\000e\000m\000e\000n\000t\000\040\000I\000I}{chapter.1}% 38
39+
\BOOKMARK [1][-]{section.1.38}{\376\377\0001\000.\0003\0008\000\040\000F\000l\000i\000p\000\040\000G\000a\000m\000e}{chapter.1}% 39
40+
\BOOKMARK [1][-]{section.1.39}{\376\377\0001\000.\0003\0009\000\040\000F\000l\000i\000p\000\040\000G\000a\000m\000e\000\040\000I\000I}{chapter.1}% 40
41+
\BOOKMARK [1][-]{section.1.40}{\376\377\0001\000.\0004\0000\000\040\000A\000n\000d\000r\000o\000i\000d\000\040\000U\000n\000l\000o\000c\000k\000\040\000P\000a\000t\000t\000e\000r\000n\000s}{chapter.1}% 41

ebook/leetcode.pdf

-1.07 KB
Binary file not shown.

ebook/leetcode.synctex.gz

-602 Bytes
Binary file not shown.

ebook/leetcode.toc

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,18 @@
111111
\contentsline {section}{\numberline {1.35}Word Squares}{49}{section.1.35}
112112
\contentsline {subsubsection}{Description}{49}{section*.77}
113113
\contentsline {subsubsection}{Solution}{50}{section*.78}
114-
\contentsline {section}{\numberline {1.36}Minimum Unique Word Abbreviation}{51}{section.1.36}
114+
\contentsline {section}{\numberline {1.36}Beautiful Arrangement}{51}{section.1.36}
115115
\contentsline {subsubsection}{Description}{51}{section*.79}
116116
\contentsline {subsubsection}{Solution}{51}{section*.80}
117-
\contentsline {section}{\numberline {1.37}Beautiful Arrangement}{52}{section.1.37}
117+
\contentsline {section}{\numberline {1.37}Beautiful Arrangement II}{52}{section.1.37}
118118
\contentsline {subsubsection}{Description}{52}{section*.81}
119119
\contentsline {subsubsection}{Solution}{52}{section*.82}
120-
\contentsline {section}{\numberline {1.38}Beautiful Arrangement II}{53}{section.1.38}
120+
\contentsline {section}{\numberline {1.38}Flip Game}{53}{section.1.38}
121121
\contentsline {subsubsection}{Description}{53}{section*.83}
122122
\contentsline {subsubsection}{Solution}{53}{section*.84}
123-
\contentsline {section}{\numberline {1.39}Flip Game}{54}{section.1.39}
123+
\contentsline {section}{\numberline {1.39}Flip Game II}{54}{section.1.39}
124124
\contentsline {subsubsection}{Description}{54}{section*.85}
125125
\contentsline {subsubsection}{Solution}{54}{section*.86}
126-
\contentsline {section}{\numberline {1.40}Flip Game II}{55}{section.1.40}
126+
\contentsline {section}{\numberline {1.40}Android Unlock Patterns}{55}{section.1.40}
127127
\contentsline {subsubsection}{Description}{55}{section*.87}
128128
\contentsline {subsubsection}{Solution}{55}{section*.88}
129-
\contentsline {section}{\numberline {1.41}Android Unlock Patterns}{56}{section.1.41}
130-
\contentsline {subsubsection}{Description}{56}{section*.89}
131-
\contentsline {subsubsection}{Solution}{56}{section*.90}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.inuker.solution;
2+
3+
/**
4+
* Created by liwentian on 2017/9/6.
5+
*/
6+
7+
/**
8+
* https://leetcode.com/articles/beautiful-arrangement/
9+
*/
10+
public class BeautifulArrangement {
11+
12+
public int countArrangement(int N) {
13+
int[] count = new int[1];
14+
helper(N, 1, new boolean[N + 1], count);
15+
return count[0];
16+
}
17+
18+
private void helper(int N, int k, boolean[] visited, int[] count) {
19+
if (k > N) {
20+
count[0]++;
21+
return;
22+
}
23+
24+
for (int i = 1; i <= N; i++) {
25+
if (!visited[i] && (i % k == 0 || k % i == 0)) {
26+
visited[i] = true;
27+
helper(N, k + 1, visited, count);
28+
visited[i] = false;
29+
}
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)