Skip to content

Commit 1db1868

Browse files
author
liwentian
committed
fd
1 parent 2371817 commit 1db1868

File tree

12 files changed

+186
-86
lines changed

12 files changed

+186
-86
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@
235235
|286|[Walls and Gates](https://leetcode.com/problems/walls-and-gates/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/WallsAndGates.java)|70|多看两遍|
236236
|287|[Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/FindTheDuplicateNumber.java)||
237237
|288|[Unique Word Abbreviation](https://leetcode.com/problems/unique-word-abbreviation/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/ValidWordAbbr.java)|70|这题不难,多做两遍,题目意思容易理解错|
238+
|293|[Flip Game](https://leetcode.com/problems/flip-game/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/FlipGame.java)|90||
239+
|294|[Flip Game II](https://leetcode.com/problems/flip-game-ii/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/FlipGameII.java)|90||
238240
|295|[Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/FindMedianFromDataStream.java)||
239241
|297|[Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/)|[Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/Codec.java)|80|好题目,多做做,递归的非递归的|
240242
|298|[Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/BinaryTreeLongestConsecutiveSequence.java)|70|多做三遍,尽可能简洁|
@@ -271,6 +273,7 @@
271273
|345|[Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/ReverseVowelsOfaString.java)|100||
272274
|346|[Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/MovingAverage.java)|95|
273275
|347|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/TopKFrequentElements.java)|85|
276+
|351|[Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/AndroidUnlockPatterns.java)|70|这题多做几遍|
274277
|357|[Count Numbers with Unique Digits](https://leetcode.com/problems/count-numbers-with-unique-digits/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/CountNumberWithUniqueDigits.java)||
275278
|358|[Rearrange String k Distance Apart](https://leetcode.com/problems/rearrange-string-k-distance-apart/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/RearrangeStringKDistanceApart.java)||
276279
|359|[Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/LoggerRateLimiter.java)|100|这题如果不考虑空间消耗的话其实很简单|

ebook/Backtracking.aux

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,15 @@
121121
\@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 II}{52}{section.1.37}}
124+
\@writefile{toc}{\contentsline {section}{\numberline {1.37}Flip Game}{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}Flip Game}{53}{section.1.38}}
127+
\@writefile{toc}{\contentsline {section}{\numberline {1.38}Flip Game II}{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 II}{54}{section.1.39}}
130+
\@writefile{toc}{\contentsline {section}{\numberline {1.39}Android Unlock Patterns}{54}{section.1.39}}
131131
\@writefile{toc}{\contentsline {subsubsection}{Description}{54}{section*.85}}
132-
\@writefile{toc}{\contentsline {subsubsection}{Solution}{54}{section*.86}}
133-
\@writefile{toc}{\contentsline {section}{\numberline {1.40}Android Unlock Patterns}{55}{section.1.40}}
134-
\@writefile{toc}{\contentsline {subsubsection}{Description}{55}{section*.87}}
135-
\@writefile{toc}{\contentsline {subsubsection}{Solution}{55}{section*.88}}
132+
\@writefile{toc}{\contentsline {subsubsection}{Solution}{55}{section*.86}}
136133
\@setckpt{Backtracking}{
137134
\setcounter{page}{56}
138135
\setcounter{equation}{0}
@@ -144,20 +141,20 @@
144141
\setcounter{mpfootnote}{0}
145142
\setcounter{part}{0}
146143
\setcounter{chapter}{1}
147-
\setcounter{section}{40}
144+
\setcounter{section}{39}
148145
\setcounter{subsection}{0}
149146
\setcounter{subsubsection}{0}
150147
\setcounter{paragraph}{0}
151148
\setcounter{subparagraph}{0}
152149
\setcounter{figure}{1}
153150
\setcounter{table}{0}
154-
\setcounter{FancyVerbLine}{3}
151+
\setcounter{FancyVerbLine}{33}
155152
\setcounter{pp@next@reset}{1}
156153
\setcounter{@fnserial}{0}
157154
\setcounter{Item}{0}
158155
\setcounter{Hfootnote}{0}
159156
\setcounter{Hy@AnnotLevel}{0}
160-
\setcounter{bookmark@seq@number}{41}
157+
\setcounter{bookmark@seq@number}{40}
161158
\setcounter{parentequation}{0}
162159
\setcounter{section@level}{3}
163160
}

ebook/Backtracking.tex

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2296,40 +2296,6 @@ \subsubsection{Solution}
22962296

22972297
\newpage
22982298

2299-
\section{Beautiful Arrangement II} %%%%%%%%%%%%%%%%%%%%%%
2300-
\subsubsection{Description}
2301-
Given two integers n and k, you need to construct a list which contains n different positive integers ranging from 1 to n and obeys the following requirement:
2302-
2303-
Suppose this list is \code{[a1, a2, a3, ... , an]}, then the list \code{[|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|]} has exactly k distinct integers.
2304-
2305-
If there are multiple answers, print any of them.
2306-
2307-
\textbf{Example 1:}
2308-
2309-
\textbf{Input:} n = 3, k = 1
2310-
2311-
\textbf{Output:} \code{[1, 2, 3]}
2312-
2313-
\textbf{Explanation:}The \code{[1, 2, 3]} has three different positive integers ranging from 1 to 3, and the \code{[1, 1]} has exactly 1 distinct integer: 1.
2314-
2315-
\textbf{Example 2:}
2316-
2317-
\textbf{Input:} n = 3, k = 2
2318-
2319-
\textbf{Output:} \code{[1, 3, 2]}
2320-
2321-
\textbf{Explanation:} The \code{[1, 3, 2]} has three different positive integers ranging from 1 to 3, and the \code{[2, 1]} has exactly 2 distinct integers: 1 and 2.
2322-
2323-
\textbf{Note:}
2324-
2325-
The n and k are in the range 1 <= k < n <= 104.
2326-
2327-
\subsubsection{Solution}
2328-
\begin{Code}
2329-
\end{Code}
2330-
2331-
\newpage
2332-
23332299
\section{Flip Game} %%%%%%%%%%%%%%%%%%%%%%
23342300
\subsubsection{Description}
23352301

@@ -2352,7 +2318,19 @@ \subsubsection{Description}
23522318
\subsubsection{Solution}
23532319

23542320
\begin{Code}
2355-
2321+
public List<String> generatePossibleNextMoves(String s) {
2322+
List<String> result = new LinkedList<>();
2323+
for (int i = 0; i < s.length(); ) {
2324+
int index = s.indexOf("++", i);
2325+
if (index >= i) {
2326+
result.add(s.substring(0, index) + "--" + s.substring(index + 2));
2327+
i = index + 1;
2328+
} else {
2329+
break;
2330+
}
2331+
}
2332+
return result;
2333+
}
23562334
\end{Code}
23572335

23582336
\newpage
@@ -2373,7 +2351,21 @@ \subsubsection{Description}
23732351
\subsubsection{Solution}
23742352

23752353
\begin{Code}
2376-
2354+
public boolean canWin(String s) {
2355+
for (int i = 0; i < s.length(); ) {
2356+
int index = s.indexOf("++", i);
2357+
if (index >= i) {
2358+
String t = s.substring(0, index) + "--" + s.substring(index + 2);
2359+
if (!canWin(t)) {
2360+
return true;
2361+
}
2362+
i = index + 1;
2363+
} else {
2364+
break;
2365+
}
2366+
}
2367+
return false;
2368+
}
23772369
\end{Code}
23782370

23792371
\newpage
@@ -2424,7 +2416,42 @@ \subsubsection{Description}
24242416

24252417
Given m = 1, n = 1, return 9.
24262418

2419+
\newpage
2420+
24272421
\subsubsection{Solution}
24282422
\begin{Code}
2423+
int DFS(boolean vis[], int[][] skip, int cur, int remain) {
2424+
if(remain < 0) return 0;
2425+
if(remain == 0) return 1;
2426+
vis[cur] = true;
2427+
int rst = 0;
2428+
for(int i = 1; i <= 9; ++i) {
2429+
// If vis[i] is not visited and (two numbers are adjacent or skip number is already visited)
2430+
if(!vis[i] && (skip[cur][i] == 0 || (vis[skip[cur][i]]))) {
2431+
rst += DFS(vis, skip, i, remain - 1);
2432+
}
2433+
}
2434+
vis[cur] = false;
2435+
return rst;
2436+
}
2437+
2438+
public int numberOfPatterns(int m, int n) {
2439+
// Skip array represents number to skip between two pairs
2440+
int skip[][] = new int[10][10];
2441+
skip[1][3] = skip[3][1] = 2;
2442+
skip[1][7] = skip[7][1] = 4;
2443+
skip[3][9] = skip[9][3] = 6;
2444+
skip[7][9] = skip[9][7] = 8;
2445+
skip[1][9] = skip[9][1] = skip[2][8] = skip[8][2] = skip[3][7] = skip[7][3] = skip[4][6] = skip[6][4] = 5;
2446+
boolean vis[] = new boolean[10];
2447+
int rst = 0;
2448+
// DFS search each length from m to n
2449+
for(int i = m; i <= n; ++i) {
2450+
rst += DFS(vis, skip, 1, i - 1) * 4; // 1, 3, 7, 9 are symmetric
2451+
rst += DFS(vis, skip, 2, i - 1) * 4; // 2, 4, 6, 8 are symmetric
2452+
rst += DFS(vis, skip, 5, i - 1); // 5
2453+
}
2454+
return rst;
2455+
}
24292456
\end{Code}
24302457
\newpage

ebook/leetcode.log

Lines changed: 4 additions & 27 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 21:10
1+
This is XeTeX, Version 3.14159265-2.6-0.99996 (TeX Live 2016) (preloaded format=xelatex 2017.9.4) 6 SEP 2017 22:08
22
entering extended mode
33
restricted \write18 enabled.
44
%&-line parsing enabled.
@@ -1963,32 +1963,9 @@ File: images/sudoku2.png Graphic file (type QTm)
19631963
File: images/watch.jpg Graphic file (type QTm)
19641964

19651965
<use "images/watch.jpg" > [42] [43] [44] [45] [46] [47] [48] [49] [50]
1966-
[51]
1967-
! FancyVerb Error:
1968-
Empty verbatim environment
1969-
.
1970-
\FV@Error ...ncyVerb Error:^^J\space \space #1^^J}
1971-
1972-
l.2329 \end{Code}
1973-
1974-
This error message was generated by an \errmessage
1975-
command, so I can't give any explicit help.
1976-
Pretend that you're Hercule Poirot: Examine all clues,
1977-
and deduce the truth by order and method.
1978-
1979-
[52] [53] [54]
1966+
[51] [52] [53]
19801967
File: images/unlock.png Graphic file (type QTm)
1981-
<use "images/unlock.png" >
1982-
! FancyVerb Error:
1983-
Empty verbatim environment
1984-
.
1985-
\FV@Error ...ncyVerb Error:^^J\space \space #1^^J}
1986-
1987-
l.2429 \end{Code}
1988-
1989-
(That was another \errmessage.)
1990-
1991-
[55])
1968+
<use "images/unlock.png" > [54] [55])
19921969
No file leetcode.ind.
19931970
Package atveryend Info: Empty hook `BeforeClearDocument' on input line 44.
19941971
Package atveryend Info: Empty hook `AfterLastShipout' on input line 44.
@@ -2000,7 +1977,7 @@ Package atveryend Info: Empty hook `AtVeryVeryEnd' on input line 44.
20001977
Here is how much of TeX's memory you used:
20011978
29222 strings out of 493591
20021979
554821 string characters out of 6143547
2003-
596680 words of memory out of 5000000
1980+
592460 words of memory out of 5000000
20041981
32241 multiletter control sequences out of 15000+600000
20051982
5562 words of font info for 59 fonts, out of 8000000 for 9000
20061983
1347 hyphenation exceptions out of 8191

ebook/leetcode.out

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
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
3737
\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
38+
\BOOKMARK [1][-]{section.1.37}{\376\377\0001\000.\0003\0007\000\040\000F\000l\000i\000p\000\040\000G\000a\000m\000e}{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\000\040\000I\000I}{chapter.1}% 39
40+
\BOOKMARK [1][-]{section.1.39}{\376\377\0001\000.\0003\0009\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}% 40

ebook/leetcode.pdf

275 Bytes
Binary file not shown.

ebook/leetcode.synctex.gz

2.93 KB
Binary file not shown.

ebook/leetcode.toc

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,12 @@
114114
\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 II}{52}{section.1.37}
117+
\contentsline {section}{\numberline {1.37}Flip Game}{52}{section.1.37}
118118
\contentsline {subsubsection}{Description}{52}{section*.81}
119119
\contentsline {subsubsection}{Solution}{52}{section*.82}
120-
\contentsline {section}{\numberline {1.38}Flip Game}{53}{section.1.38}
120+
\contentsline {section}{\numberline {1.38}Flip Game II}{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 II}{54}{section.1.39}
123+
\contentsline {section}{\numberline {1.39}Android Unlock Patterns}{54}{section.1.39}
124124
\contentsline {subsubsection}{Description}{54}{section*.85}
125-
\contentsline {subsubsection}{Solution}{54}{section*.86}
126-
\contentsline {section}{\numberline {1.40}Android Unlock Patterns}{55}{section.1.40}
127-
\contentsline {subsubsection}{Description}{55}{section*.87}
128-
\contentsline {subsubsection}{Solution}{55}{section*.88}
125+
\contentsline {subsubsection}{Solution}{55}{section*.86}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.inuker.solution;
2+
3+
/**
4+
* Created by liwentian on 2017/9/6.
5+
*/
6+
7+
/**
8+
* https://leetcode.com/articles/android-unlock-patterns/
9+
*/
10+
public class AndroidUnlockPatterns {
11+
12+
int DFS(boolean vis[], int[][] skip, int cur, int remain) {
13+
if(remain < 0) return 0;
14+
if(remain == 0) return 1;
15+
vis[cur] = true;
16+
int rst = 0;
17+
for(int i = 1; i <= 9; ++i) {
18+
// If vis[i] is not visited and (two numbers are adjacent or skip number is already visited)
19+
if(!vis[i] && (skip[cur][i] == 0 || (vis[skip[cur][i]]))) {
20+
rst += DFS(vis, skip, i, remain - 1);
21+
}
22+
}
23+
vis[cur] = false;
24+
return rst;
25+
}
26+
27+
public int numberOfPatterns(int m, int n) {
28+
// Skip array represents number to skip between two pairs
29+
int skip[][] = new int[10][10];
30+
skip[1][3] = skip[3][1] = 2;
31+
skip[1][7] = skip[7][1] = 4;
32+
skip[3][9] = skip[9][3] = 6;
33+
skip[7][9] = skip[9][7] = 8;
34+
skip[1][9] = skip[9][1] = skip[2][8] = skip[8][2] = skip[3][7] = skip[7][3] = skip[4][6] = skip[6][4] = 5;
35+
boolean vis[] = new boolean[10];
36+
int rst = 0;
37+
// DFS search each length from m to n
38+
for(int i = m; i <= n; ++i) {
39+
rst += DFS(vis, skip, 1, i - 1) * 4; // 1, 3, 7, 9 are symmetric
40+
rst += DFS(vis, skip, 2, i - 1) * 4; // 2, 4, 6, 8 are symmetric
41+
rst += DFS(vis, skip, 5, i - 1); // 5
42+
}
43+
return rst;
44+
}
45+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.inuker.solution;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
/**
7+
* Created by liwentian on 2017/9/6.
8+
*/
9+
10+
public class FlipGame {
11+
12+
public List<String> generatePossibleNextMoves(String s) {
13+
List<String> result = new LinkedList<>();
14+
for (int i = 0; i < s.length(); ) {
15+
int index = s.indexOf("++", i);
16+
if (index >= i) {
17+
result.add(s.substring(0, index) + "--" + s.substring(index + 2));
18+
i = index + 1;
19+
} else {
20+
break;
21+
}
22+
}
23+
return result;
24+
}
25+
}

0 commit comments

Comments
 (0)