Skip to content

Commit 641c372

Browse files
author
liwentian
committed
fd
1 parent 2b87077 commit 641c372

File tree

11 files changed

+203
-17
lines changed

11 files changed

+203
-17
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
|145|[Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/BinaryTreePostorderTraversal.java)|80|
145145
|146|[LRU Cache](https://leetcode.com/problems/lru-cache/)|[Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/LRUCache.java)|95|好题目,多做几遍|
146146
|149|[Max Points on a Line](https://leetcode.com/problems/max-points-on-a-line/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/MaxPointsOnALine.java)|60|
147-
|151|Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/description/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/ReverseWordsInAString.java)|70|这题虽然不难,但是也错了几次|
147+
|151|[Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/description/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/ReverseWordsInAString.java)|70|这题虽然不难,但是也错了几次|
148148
|152|[Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/MaximumProductSubarray.java)||
149149
|153|[Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/FindMinimumInRotatedSortedArray.java)||
150150
|154|[Find Minimum in Rotated Sorted Array II](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/FindMinimumInRotatedSortedArrayII.java)||
@@ -275,6 +275,7 @@
275275
|339|[Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/NestedListWeightSum.java)||
276276
|340|[Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/LongestSubstringWithAtMostKDistinctCharacters.java)|80|#159类似|
277277
|341|[Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/NestedIterator.java)|75|这题有点意思,多做两遍|
278+
|344|[Reverse String](https://leetcode.com/problems/reverse-string/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/ReverseString.java)|100||
278279
|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||
279280
|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|
280281
|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|
@@ -324,5 +325,6 @@
324325
|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||
325326
|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||
326327
|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||
328+
|541|[Reverse String II](https://leetcode.com/problems/reverse-string-ii/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/ReverseStringII.java)|100||
327329
|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||
328330
|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/dp/images/.DS_Store

0 Bytes
Binary file not shown.

ebook/string/String.aux

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@
77
\@writefile{toc}{\contentsline {section}{\numberline {1.1}Reverse String}{1}{section.1.1}}
88
\@writefile{toc}{\contentsline {subsubsection}{Description}{1}{section*.2}}
99
\@writefile{toc}{\contentsline {subsubsection}{Solution}{1}{section*.3}}
10+
\@writefile{toc}{\contentsline {section}{\numberline {1.2}Reverse String II}{2}{section.1.2}}
11+
\@writefile{toc}{\contentsline {subsubsection}{Description}{2}{section*.4}}
12+
\@writefile{toc}{\contentsline {subsubsection}{Solution}{2}{section*.5}}
13+
\@writefile{toc}{\contentsline {section}{\numberline {1.3}Reverse Words in a String}{3}{section.1.3}}
14+
\@writefile{toc}{\contentsline {subsubsection}{Description}{3}{section*.6}}
15+
\@writefile{toc}{\contentsline {subsubsection}{Solution I}{3}{section*.7}}
16+
\@writefile{toc}{\contentsline {subsubsection}{Solution II}{3}{section*.8}}
1017
\@setckpt{String}{
11-
\setcounter{page}{2}
18+
\setcounter{page}{5}
1219
\setcounter{equation}{0}
1320
\setcounter{enumi}{0}
1421
\setcounter{enumii}{0}
@@ -18,20 +25,20 @@
1825
\setcounter{mpfootnote}{0}
1926
\setcounter{part}{0}
2027
\setcounter{chapter}{1}
21-
\setcounter{section}{1}
28+
\setcounter{section}{3}
2229
\setcounter{subsection}{0}
2330
\setcounter{subsubsection}{0}
2431
\setcounter{paragraph}{0}
2532
\setcounter{subparagraph}{0}
2633
\setcounter{figure}{0}
2734
\setcounter{table}{0}
28-
\setcounter{FancyVerbLine}{1}
35+
\setcounter{FancyVerbLine}{22}
2936
\setcounter{pp@next@reset}{1}
3037
\setcounter{@fnserial}{0}
3138
\setcounter{Item}{0}
3239
\setcounter{Hfootnote}{0}
3340
\setcounter{Hy@AnnotLevel}{0}
34-
\setcounter{bookmark@seq@number}{2}
41+
\setcounter{bookmark@seq@number}{4}
3542
\setcounter{parentequation}{0}
3643
\setcounter{section@level}{3}
3744
}

ebook/string/String.tex

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,122 @@ \subsubsection{Description}
1212
\subsubsection{Solution}
1313

1414
\begin{Code}
15+
public String reverseString(String s) {
16+
return new StringBuilder(s).reverse().toString();
17+
}
18+
\end{Code}
19+
20+
\newpage
21+
22+
\section{Reverse String II} %%%%%%%%%%%%%%%%%%%%%%


23+
24+
\subsubsection{Description}
25+
Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.
26+
27+
\textbf{Example:}
28+
29+
\textbf{Input:} s = \code{"abcdefg"}, k = 2
30+
31+
\textbf{Output:} \code{"bacdfeg"}
32+
33+
\textbf{Restrictions:}
34+
35+
The string consists of lower English letters only.
1536

37+
Length of the given string and k will in the range \code{[1, 10000]}
38+
39+
\subsubsection{Solution}
40+
41+
\begin{Code}
42+
public String reverseStr(String s, int k) {
43+
if (s.length() <= k) {
44+
return new StringBuilder(s).reverse().toString();
45+
}
46+
if (s.length() <= 2 * k) {
47+
return reverseStr(s.substring(0, k), k) + s.substring(k);
48+
}
49+
return reverseStr(s.substring(0, 2 * k), k) + reverseStr(s.substring(2 * k), k);
50+
}
1651
\end{Code}
1752

18-
\newpage
53+
\newpage
54+
55+
\section{Reverse Words in a String} %%%%%%%%%%%%%%%%%%%%%%


56+
57+
\subsubsection{Description}
58+
Given an input string, reverse the string word by word.
59+
60+
For example,
61+
Given s = "the sky is blue",
62+
return "blue is sky the".
63+
64+
Clarification:
65+
66+
1. What constitutes a word?
67+
68+
A sequence of non-space characters constitutes a word.
69+
70+
2. Could the input string contain leading or trailing spaces?
71+
72+
Yes. However, your reversed string should not contain leading or trailing spaces.
73+
74+
3. How about multiple spaces between two words?
75+
76+
Reduce them to a single space in the reversed string.
77+
\subsubsection{Solution I}
78+
79+
\begin{Code}
80+
// 耗时10ms
81+
public String reverseWords(String s) {
82+
StringBuilder sb = new StringBuilder();
83+
84+
boolean inWord = false;
85+
char[] cc = s.toCharArray();
86+
for (int i = cc.length - 1, idx = 0; i >= 0; i--) {
87+
if (cc[i] != ' ') {
88+
if (!inWord && sb.length() > 0) {
89+
sb.append(' ');
90+
}
91+
inWord = true;
92+
sb.insert(idx, cc[i]);
93+
} else if (inWord) {
94+
idx = sb.length() + 1;
95+
inWord = false;
96+
}
97+
}
98+
99+
return sb.toString();
100+
}
101+
\end{Code}
102+
103+
\newpage
104+
105+
\subsubsection{Solution II}
106+
107+
\begin{Code}
108+
// 耗时18ms
109+
public String reverseWords2(String s) {
110+
StringBuilder sb = new StringBuilder();
111+
112+
for (int i = 0, j = 0; i < s.length(); ) {
113+
if (s.charAt(i) == ' ') {
114+
i++;
115+
j = i;
116+
} else if (j >= s.length() || s.charAt(j) == ' ') {
117+
sb.insert(0, s.substring(i, j) + " ");
118+
i = j;
119+
} else {
120+
j++;
121+
}
122+
}
123+
124+
if (sb.length() > 0) {
125+
sb.setLength(sb.length() - 1);
126+
}
127+
128+
return sb.toString();
129+
}
130+
\end{Code}
131+
132+
\newpage
133+

ebook/string/leetcode-string.log

Lines changed: 34 additions & 11 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) 9 SEP 2017 12:14
1+
This is XeTeX, Version 3.14159265-2.6-0.99996 (TeX Live 2016) (preloaded format=xelatex 2017.9.4) 10 SEP 2017 09:00
22
entering extended mode
33
restricted \write18 enabled.
44
%&-line parsing enabled.
@@ -1853,7 +1853,28 @@ No file title.tex.
18531853

18541854
(./String.tex
18551855
第1章
1856-
\g__fontspec_family_AdobeFangsongStd-Regular_int=\count194
1856+
\g__fontspec_family_LatinModernMono_int=\count194
1857+
.................................................
1858+
. fontspec info: "no-font-shape"
1859+
.
1860+
. Could not resolve font Latin Modern Mono/B (it probably doesn't exist).
1861+
.................................................
1862+
.................................................
1863+
. fontspec info: "defining-font"
1864+
.
1865+
. Font family 'LatinModernMono(0)' created for font 'Latin Modern Mono' with
1866+
. options [].
1867+
.
1868+
. This font family consists of the following NFSS series/shapes:
1869+
. - 'normal' (m/n) with NFSS spec.: <->"Latin Modern
1870+
. Mono/OT:script=latn;language=DFLT;"
1871+
. - 'small caps' (m/sc) with NFSS spec.: - 'italic' (m/it) with NFSS spec.:
1872+
. <->"Latin Modern Mono/I/OT:script=latn;language=DFLT;"
1873+
. - 'italic small caps' (m/itsc) with NFSS spec.: - 'bold italic' (bx/it)
1874+
. with NFSS spec.: <->"Latin Modern Mono/BI/OT:script=latn;language=DFLT;"
1875+
. - 'bold italic small caps' (bx/itsc) with NFSS spec.:
1876+
.................................................
1877+
\g__fontspec_family_AdobeFangsongStd-Regular_int=\count195
18571878
.................................................
18581879
. fontspec info: "no-font-shape"
18591880
.
@@ -1872,7 +1893,8 @@ No file title.tex.
18721893
. Could not resolve font AdobeFangsongStd-Regular/I (it probably doesn't
18731894
. exist).
18741895
.................................................
1875-
LaTeX Font Info: Try loading font information for EU1+lmtt on input line 9.
1896+
LaTeX Font Info: Try loading font information for EU1+lmtt on input line 14.
1897+
18761898
(/usr/local/texlive/2016/texmf-dist/tex/latex/euenc/eu1lmtt.fd
18771899
File: eu1lmtt.fd 2009/10/30 v1.6 Font defs for Latin Modern
18781900
)
@@ -1892,7 +1914,8 @@ File: eu1lmtt.fd 2009/10/30 v1.6 Font defs for Latin Modern
18921914
[1
18931915

18941916

1895-
])
1917+
] [2]
1918+
[3] [4])
18961919
No file leetcode-string.ind.
18971920
Package atveryend Info: Empty hook `BeforeClearDocument' on input line 44.
18981921
Package atveryend Info: Empty hook `AfterLastShipout' on input line 44.
@@ -1902,12 +1925,12 @@ Package atveryend Info: Empty hook `AtEndAfterFileList' on input line 44.
19021925
Package atveryend Info: Empty hook `AtVeryVeryEnd' on input line 44.
19031926
)
19041927
Here is how much of TeX's memory you used:
1905-
29132 strings out of 493591
1906-
550932 string characters out of 6143547
1907-
562608 words of memory out of 5000000
1908-
32177 multiletter control sequences out of 15000+600000
1909-
5506 words of font info for 52 fonts, out of 8000000 for 9000
1928+
29164 strings out of 493591
1929+
551971 string characters out of 6143547
1930+
568609 words of memory out of 5000000
1931+
32195 multiletter control sequences out of 15000+600000
1932+
5554 words of font info for 58 fonts, out of 8000000 for 9000
19101933
1347 hyphenation exceptions out of 8191
1911-
65i,11n,77p,10421b,476s stack positions out of 5000i,500n,10000p,200000b,80000s
1934+
65i,11n,77p,10421b,588s stack positions out of 5000i,500n,10000p,200000b,80000s
19121935

1913-
Output written on leetcode-string.pdf (3 pages).
1936+
Output written on leetcode-string.pdf (6 pages).

ebook/string/leetcode-string.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
\BOOKMARK [0][-]{chapter.1}{\376\377\173\054\0001\172\340\000\040\000\040\000S\000t\000r\000i\000n\000g}{}% 1
22
\BOOKMARK [1][-]{section.1.1}{\376\377\0001\000.\0001\000\040\000R\000e\000v\000e\000r\000s\000e\000\040\000S\000t\000r\000i\000n\000g}{chapter.1}% 2
3+
\BOOKMARK [1][-]{section.1.2}{\376\377\0001\000.\0002\000\040\000R\000e\000v\000e\000r\000s\000e\000\040\000S\000t\000r\000i\000n\000g\000\040\000I\000I}{chapter.1}% 3
4+
\BOOKMARK [1][-]{section.1.3}{\376\377\0001\000.\0003\000\040\000R\000e\000v\000e\000r\000s\000e\000\040\000W\000o\000r\000d\000s\000\040\000i\000n\000\040\000a\000\040\000S\000t\000r\000i\000n\000g}{chapter.1}% 4

ebook/string/leetcode-string.pdf

33.2 KB
Binary file not shown.
7.43 KB
Binary file not shown.

ebook/string/leetcode-string.toc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,10 @@
22
\contentsline {section}{\numberline {1.1}Reverse String}{1}{section.1.1}
33
\contentsline {subsubsection}{Description}{1}{section*.2}
44
\contentsline {subsubsection}{Solution}{1}{section*.3}
5+
\contentsline {section}{\numberline {1.2}Reverse String II}{2}{section.1.2}
6+
\contentsline {subsubsection}{Description}{2}{section*.4}
7+
\contentsline {subsubsection}{Solution}{2}{section*.5}
8+
\contentsline {section}{\numberline {1.3}Reverse Words in a String}{3}{section.1.3}
9+
\contentsline {subsubsection}{Description}{3}{section*.6}
10+
\contentsline {subsubsection}{Solution I}{3}{section*.7}
11+
\contentsline {subsubsection}{Solution II}{3}{section*.8}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.inuker.solution;
2+
3+
/**
4+
* Created by liwentian on 2017/9/10.
5+
*/
6+
7+
public class ReverseString {
8+
9+
public String reverseString(String s) {
10+
return new StringBuilder(s).reverse().toString();
11+
}
12+
}

0 commit comments

Comments
 (0)