-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
425 lines (265 loc) · 47 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>A Coder's Peaceful Life.</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="description">
<meta property="og:type" content="website">
<meta property="og:title" content="A Coder's Peaceful Life.">
<meta property="og:url" content="http://cindy52.github.io/index.html">
<meta property="og:site_name" content="A Coder's Peaceful Life.">
<meta property="og:description">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="A Coder's Peaceful Life.">
<meta name="twitter:description">
<link rel="alternative" href="/atom.xml" title="A Coder's Peaceful Life." type="application/atom+xml">
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
<link href="//fonts.googleapis.com/css?family=Inconsolata:400,700|Open+Sans:700,400" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="/css/style.css" type="text/css">
</head>
<body>
<div id="container">
<div id="wrap">
<div id="header">
<div id="header-outer" class="outer">
<div id="header-inner" class="inner">
<div id="header-title">
<h1 id="logo-wrap">
<a href="/" id="logo">A Coder's Peaceful Life.
<span id="subtitle">Cindy in US.</span>
</a>
</h1>
</div>
<nav id="sub-nav">
<a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
<a id="nav-search-btn" class="nav-icon" title="Search"></a>
</nav>
<nav id="main-nav">
<a id="main-nav-toggle" class="nav-icon"></a>
<a class="main-nav-link" href="/">Home</a>
<a class="main-nav-link" href="/archives">Archives</a>
</nav>
<div id="search-form-wrap">
<form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit"></button><input type="hidden" name="sitesearch" value="http://cindy52.github.io"></form>
</div>
</div>
</div>
</div>
<div class="outer">
<section id="main">
<article id="post-LeetCode-Word-Ladder" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2016/02/20/LeetCode-Word-Ladder/" class="article-date">
<time datetime="2016-02-21T04:16:28.000Z" itemprop="datePublished">2016-02-20</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2016/02/20/LeetCode-Word-Ladder/">LeetCode-Word-Ladder</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>A diagram problem. Imagine each word in the dic as a node in a diagram. Use BFS. Use a set to cache the word which is visited.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">ladderLength</span><span class="params">(String beginWord, String endWord, Set<String> wordList)</span> </span>{</span><br><span class="line"> wordList.add(endWord);</span><br><span class="line"> Queue<String> queue = <span class="keyword">new</span> LinkedList<>();</span><br><span class="line"> HashSet<String> set = <span class="keyword">new</span> HashSet<>();</span><br><span class="line"> queue.offer(beginWord);</span><br><span class="line"> set.add(beginWord);</span><br><span class="line"> <span class="keyword">int</span> res = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">int</span> cur = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">int</span> next = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span> (!queue.isEmpty()) {</span><br><span class="line"> String str = queue.poll();</span><br><span class="line"> cur--;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">for</span> (String word : findWord(str, wordList)) {</span><br><span class="line"> <span class="keyword">if</span> (word.equals(endWord)) {</span><br><span class="line"> <span class="keyword">return</span> res + <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (!set.contains(word)) {</span><br><span class="line"> set.add(word);</span><br><span class="line"> queue.offer(word);</span><br><span class="line"> next++;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (cur == <span class="number">0</span>) {</span><br><span class="line"> res++;</span><br><span class="line"> cur = next;</span><br><span class="line"> next = <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> </span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">private</span> List<String> <span class="title">findWord</span><span class="params">(String target, Set<String> wordList)</span> </span>{</span><br><span class="line"> List<String> result = <span class="keyword">new</span> ArrayList<>();</span><br><span class="line"> <span class="keyword">char</span>[] arr = target.toCharArray();</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < arr.length; i++) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">char</span> c = <span class="string">'a'</span>; c <= <span class="string">'z'</span>; c++) {</span><br><span class="line"> <span class="keyword">if</span> (arr[i] == c) {</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">char</span> temp = arr[i];</span><br><span class="line"> arr[i] = c;</span><br><span class="line"> String s = <span class="keyword">new</span> String(arr);</span><br><span class="line"> <span class="keyword">if</span> (wordList.contains(s)) {</span><br><span class="line"> result.add(s);</span><br><span class="line"> }</span><br><span class="line"> arr[i] = temp;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> result;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</div>
<footer class="article-footer">
<a data-url="http://cindy52.github.io/2016/02/20/LeetCode-Word-Ladder/" data-id="cil36h6jb0006lm94ezciicv1" class="article-share-link">Share</a>
<a href="http://cindy52.github.io/2016/02/20/LeetCode-Word-Ladder/#disqus_thread" class="article-comment-link">Comments</a>
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/LeetCode/">LeetCode</a></li></ul>
</footer>
</div>
</article>
<article id="post-LeetCode-LRU-Cache" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2016/02/20/LeetCode-LRU-Cache/" class="article-date">
<time datetime="2016-02-21T02:29:52.000Z" itemprop="datePublished">2016-02-20</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2016/02/20/LeetCode-LRU-Cache/">LeetCode-LRU-Cache</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>Use linked hashmap, the value of the hashmap is the linked list node has the key and value variable. Need to mention that, the value of the key in hashmap is not the value of corresponding key, however is the previous listnode.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LRUCache</span> </span>{</span><br><span class="line"> <span class="keyword">private</span> HashMap<Integer, ListNode> map;</span><br><span class="line"> ListNode dummy;</span><br><span class="line"> ListNode tail;</span><br><span class="line"> <span class="keyword">int</span> capacity;</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">LRUCache</span><span class="params">(<span class="keyword">int</span> capacity)</span> </span>{</span><br><span class="line"> map = <span class="keyword">new</span> HashMap<>();</span><br><span class="line"> <span class="keyword">this</span>.capacity = capacity;</span><br><span class="line"> dummy = <span class="keyword">new</span> ListNode(<span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line"> tail = dummy;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">get</span><span class="params">(<span class="keyword">int</span> key)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span> (!map.containsKey(key)) {</span><br><span class="line"> <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> moveToTail(key);</span><br><span class="line"> <span class="keyword">return</span> map.get(key).next.val;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">set</span><span class="params">(<span class="keyword">int</span> key, <span class="keyword">int</span> value)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span> (map.containsKey(key)) {</span><br><span class="line"> map.get(key).next.val = value;</span><br><span class="line"> moveToTail(key);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">if</span> (map.size() == capacity) {</span><br><span class="line"> ListNode node = dummy.next;</span><br><span class="line"> dummy.next = node.next;</span><br><span class="line"> map.remove(node.key);</span><br><span class="line"> <span class="keyword">if</span> (dummy.next != <span class="keyword">null</span>) {</span><br><span class="line"> map.put(dummy.next.key, dummy);</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> }</span><br><span class="line"> tail.next = <span class="keyword">new</span> ListNode(key, value);</span><br><span class="line"> map.put(key, tail);</span><br><span class="line"> tail = tail.next;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">moveToTail</span><span class="params">(<span class="keyword">int</span> key)</span> </span>{</span><br><span class="line"> ListNode node = map.get(key);</span><br><span class="line"> ListNode cur = node.next;</span><br><span class="line"> <span class="keyword">if</span> (cur == tail) {</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> map.put(cur.next.key, node);</span><br><span class="line"> node.next = node.next.next;</span><br><span class="line"> tail.next = cur;</span><br><span class="line"> cur.next = <span class="keyword">null</span>;</span><br><span class="line"> map.put(key, tail);</span><br><span class="line"> tail = cur;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">ListNode</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> key;</span><br><span class="line"> <span class="keyword">int</span> val;</span><br><span class="line"> ListNode next;</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">ListNode</span><span class="params">(<span class="keyword">int</span> key, <span class="keyword">int</span> val)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.key = key;</span><br><span class="line"> <span class="keyword">this</span>.val = val;</span><br><span class="line"> <span class="keyword">this</span>.next = next;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</div>
<footer class="article-footer">
<a data-url="http://cindy52.github.io/2016/02/20/LeetCode-LRU-Cache/" data-id="cil36h6jh000alm94c7ro59ni" class="article-share-link">Share</a>
<a href="http://cindy52.github.io/2016/02/20/LeetCode-LRU-Cache/#disqus_thread" class="article-comment-link">Comments</a>
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/LeetCode/">LeetCode</a></li></ul>
</footer>
</div>
</article>
<article id="post-LeetCode-Count-Univalue-Subtree" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2016/02/19/LeetCode-Count-Univalue-Subtree/" class="article-date">
<time datetime="2016-02-20T01:11:24.000Z" itemprop="datePublished">2016-02-19</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2016/02/19/LeetCode-Count-Univalue-Subtree/">LeetCode-Count-Univalue-Subtree</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>这题好像蛮简单的,没有subscribtion,不知道做的对不对。 Simple DFS.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">countUnivalSubtrees</span><span class="params">(TreeNode root)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span> (root.left == <span class="keyword">null</span> && root.right == <span class="keyword">null</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span> (root.left != <span class="keyword">null</span> && root.right == <span class="keyword">null</span>) {</span><br><span class="line"> <span class="keyword">if</span> (root.left.val == root.val) {</span><br><span class="line"> <span class="keyword">int</span> left = countUnivalSubtrees(root.left);</span><br><span class="line"> <span class="keyword">if</span> (left == -<span class="number">1</span>) {</span><br><span class="line"> <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> left + <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span> (root.left == <span class="keyword">null</span> && root.right != <span class="keyword">null</span>) {</span><br><span class="line"> <span class="keyword">if</span> (root.right.val == root.val) {</span><br><span class="line"> <span class="keyword">int</span> right = countUnivalSubtrees(root.right);</span><br><span class="line"> <span class="keyword">if</span> (right == -<span class="number">1</span>) {</span><br><span class="line"> <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> right + <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">if</span> (root.left == root.val && root.right == root.val) {</span><br><span class="line"> <span class="keyword">int</span> left = countUnivalSubtrees(root.left);</span><br><span class="line"> <span class="keyword">if</span> (left == -<span class="number">1</span>) {</span><br><span class="line"> <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> right = countUnivalSubtrees(root.right);</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span> (right == -<span class="number">1</span>) {</span><br><span class="line"> <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> left + right + <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</div>
<footer class="article-footer">
<a data-url="http://cindy52.github.io/2016/02/19/LeetCode-Count-Univalue-Subtree/" data-id="cil36h5m90000lm94ytdn0qf8" class="article-share-link">Share</a>
<a href="http://cindy52.github.io/2016/02/19/LeetCode-Count-Univalue-Subtree/#disqus_thread" class="article-comment-link">Comments</a>
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/LeetCode/">LeetCode</a></li></ul>
</footer>
</div>
</article>
<article id="post-LeetCode-Largest-BST-Subtree" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2016/02/18/LeetCode-Largest-BST-Subtree/" class="article-date">
<time datetime="2016-02-18T22:10:45.000Z" itemprop="datePublished">2016-02-18</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2016/02/18/LeetCode-Largest-BST-Subtree/">LeetCode-Largest-BST-Subtree</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>Largest BST subtree in a binary tree</p>
<p>Use a bottom up approach. First check if the subtree is BST, if it is DFS to go upper, else return false;<br>If it doesn’t need to be a subtree, use top-down approach. If it is BST, DFS go down, else copy the tree node, start a new tree and go down.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">solution</span> </span>{</span><br><span class="line"> <span class="keyword">private</span> TreeNode result = <span class="keyword">null</span>;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> maxNum = <span class="number">0</span>;</span><br><span class="line"> <span class="function"><span class="keyword">public</span> TreeNode <span class="title">findLargestBSTSubtree</span><span class="params">(TreeNode root)</span> </span>{</span><br><span class="line"> find(root);</span><br><span class="line"> <span class="keyword">return</span> result;</span><br><span class="line"> }</span><br><span class="line"> <span class="function"><span class="keyword">private</span> ResultType <span class="title">find</span><span class="params">(TreeNode root)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span> (root == <span class="keyword">null</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> ResultType(<span class="number">0</span>, Integer.MAX_VALUE, Integer.MIN_VALUE);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">boolean</span> isBST = <span class="keyword">true</span>;</span><br><span class="line"> ResultType left = find(root.left);</span><br><span class="line"> <span class="keyword">int</span> curMin = left.num == <span class="number">0</span> ? root.value : left.min;</span><br><span class="line"> <span class="keyword">if</span> (left.num == -<span class="number">1</span> || root.value <= left.max) {</span><br><span class="line"> isBST = <span class="keyword">false</span>;</span><br><span class="line"> }</span><br><span class="line"> ResultType right = find(root.right);</span><br><span class="line"> <span class="keyword">int</span> curMax = right.num == <span class="number">0</span> ? root.value : right.max;</span><br><span class="line"> <span class="keyword">if</span> (right.num == -<span class="number">1</span> || root.value >= right.min) {</span><br><span class="line"> isBST = <span class="keyword">false</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (isBST) {</span><br><span class="line"> <span class="keyword">int</span> totalNUm = left.num + right.num + <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span> (totalNUm > maxNum) {</span><br><span class="line"> maxNum = totalNUm;</span><br><span class="line"> result = root;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> ResultType(totalNUm, curMin, curMax);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> ResultType(-<span class="number">1</span>, <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">ResultType</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> num;</span><br><span class="line"> <span class="keyword">int</span> min;</span><br><span class="line"> <span class="keyword">int</span> max;</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">ResultType</span><span class="params">(<span class="keyword">int</span> num, <span class="keyword">int</span> min, <span class="keyword">int</span> max)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.num = num;</span><br><span class="line"> <span class="keyword">this</span>.min = min;</span><br><span class="line"> <span class="keyword">this</span>.max = max;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</div>
<footer class="article-footer">
<a data-url="http://cindy52.github.io/2016/02/18/LeetCode-Largest-BST-Subtree/" data-id="cil36h6je0008lm94zi70dc0u" class="article-share-link">Share</a>
<a href="http://cindy52.github.io/2016/02/18/LeetCode-Largest-BST-Subtree/#disqus_thread" class="article-comment-link">Comments</a>
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/LeetCode/">LeetCode</a></li></ul>
</footer>
</div>
</article>
<article id="post-LeetCode-WordPatternII" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2016/02/17/LeetCode-WordPatternII/" class="article-date">
<time datetime="2016-02-17T22:06:23.000Z" itemprop="datePublished">2016-02-17</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2016/02/17/LeetCode-WordPatternII/">LeetCode WordPatternII</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>Word Pattern</p>
<p>Because Word PatternI has space between words, we can use split and then use hash map to record and compare.<br>Word PatternII has no space, we don’t know how to separate a word. Use DFS to separate a word from the string. If the assumption of one pattern to one word can’t go through, then add one charactor to the word corresponding to the pattern. If all assumption related to this pattern can’t go through, then backtracing to change the rule of previous pattern.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">wordPatternMatch</span><span class="params">(String pattern, String str)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> dfs(pattern, str, <span class="number">0</span>, <span class="number">0</span>, <span class="keyword">new</span> HashMap<String, String>(), <span class="keyword">new</span> HashSet<String>());</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">private</span> <span class="keyword">boolean</span> <span class="title">dfs</span><span class="params">(String pattern, String str, <span class="keyword">int</span> patternStart, <span class="keyword">int</span> strStart, HashMap<String, String> map, HashSet<String> set)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span> (patternStart == pattern.length() && strStart == str.length()) {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (patternStart == pattern.length() || strStart == str.length()) {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"> }</span><br><span class="line"> String key = pattern.substring(patternStart, patternStart + <span class="number">1</span>);</span><br><span class="line"> <span class="keyword">if</span> (map.containsKey(key)) {</span><br><span class="line"> <span class="keyword">if</span> (str.startsWith(map.get(key))) {</span><br><span class="line"> <span class="keyword">return</span> dfs(pattern, str, patternStart + <span class="number">1</span>, strStart + map.get(key).length(), map, set);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = strStart; i < str.length(); i++) {</span><br><span class="line"> <span class="keyword">if</span> (set.contains(str.substring(strStart, i + <span class="number">1</span>))) {</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> map.put(key, str.substring(strStart, i + <span class="number">1</span>));</span><br><span class="line"> set.add(str.substring(strStart, i + <span class="number">1</span>));</span><br><span class="line"> <span class="keyword">if</span> (dfs(pattern, str, patternStart + <span class="number">1</span>, i + <span class="number">1</span>, map, set)) {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line"> }</span><br><span class="line"> map.remove(key);</span><br><span class="line"> ser.remove(str.substring(strStart, i + <span class="number">1</span>));</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</div>
<footer class="article-footer">
<a data-url="http://cindy52.github.io/2016/02/17/LeetCode-WordPatternII/" data-id="cil36h6j60004lm947lj585qh" class="article-share-link">Share</a>
<a href="http://cindy52.github.io/2016/02/17/LeetCode-WordPatternII/#disqus_thread" class="article-comment-link">Comments</a>
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/LeetCode/">LeetCode</a></li></ul>
</footer>
</div>
</article>
<article id="post-hello-world" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2016/02/17/hello-world/" class="article-date">
<time datetime="2016-02-17T22:04:18.000Z" itemprop="datePublished">2016-02-17</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2016/02/17/hello-world/">Hello World</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>Welcome to <a href="http://hexo.io/" target="_blank" rel="external">Hexo</a>! This is your very first post. Check <a href="http://hexo.io/docs/" target="_blank" rel="external">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="http://hexo.io/docs/troubleshooting.html" target="_blank" rel="external">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues" target="_blank" rel="external">GitHub</a>.</p>
<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">"My New Post"</span></span><br></pre></td></tr></table></figure>
<p>More info: <a href="http://hexo.io/docs/writing.html" target="_blank" rel="external">Writing</a></p>
<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure>
<p>More info: <a href="http://hexo.io/docs/server.html" target="_blank" rel="external">Server</a></p>
<h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure>
<p>More info: <a href="http://hexo.io/docs/generating.html" target="_blank" rel="external">Generating</a></p>
<h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure>
<p>More info: <a href="http://hexo.io/docs/deployment.html" target="_blank" rel="external">Deployment</a></p>
</div>
<footer class="article-footer">
<a data-url="http://cindy52.github.io/2016/02/17/hello-world/" data-id="cil36h6j00003lm94l7bgcs9o" class="article-share-link">Share</a>
<a href="http://cindy52.github.io/2016/02/17/hello-world/#disqus_thread" class="article-comment-link">Comments</a>
</footer>
</div>
</article>
</section>
<aside id="sidebar">
<div class="widget-wrap">
<h3 class="widget-title">Recents</h3>
<div class="widget recents">
<ul>
<li>
<a href="/2016/02/20/LeetCode-Word-Ladder/">LeetCode-Word-Ladder</a>
</li>
<li>
<a href="/2016/02/20/LeetCode-LRU-Cache/">LeetCode-LRU-Cache</a>
</li>
<li>
<a href="/2016/02/19/LeetCode-Count-Univalue-Subtree/">LeetCode-Count-Univalue-Subtree</a>
</li>
<li>
<a href="/2016/02/18/LeetCode-Largest-BST-Subtree/">LeetCode-Largest-BST-Subtree</a>
</li>
<li>
<a href="/2016/02/17/LeetCode-WordPatternII/">LeetCode WordPatternII</a>
</li>
</ul>
</div>
</div>
<div class="widget-wrap">
<h3 class="widget-title">Tag Cloud</h3>
<div class="widget tagcloud">
<a href="/tags/LeetCode/" style="font-size: 10px;">LeetCode</a>
</div>
</div>
<div class="widget-wrap">
<h3 class="widget-title">Archives</h3>
<div class="widget">
<ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/02/">February 2016</a><span class="archive-list-count">6</span></li></ul>
</div>
</div>
</aside>
</div>
<div id="footer">
<div class="outer">
<div id="footer-info" class="inner">
<a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc/4.0/88x31.png" /></a></br>
© 2016 Cindy<br>
Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
</div>
</div>
</div>
</div>
<nav id="mobile-nav">
<a href="/" class="mobile-nav-link">Home</a>
<a href="/archives" class="mobile-nav-link">Archives</a>
</nav>
<script>
var disqus_shortname = '9plpmHbnOH';
(function(){
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css" type="text/css">
<script src="/fancybox/jquery.fancybox.pack.js" type="text/javascript"></script>
<script src="/js/script.js" type="text/javascript"></script>
</div>
</body>
</html>