-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
476 lines (331 loc) · 18.4 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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
<link rel="mask-icon" href="/images/logo.svg" color="#222">
<link rel="stylesheet" href="/css/main.css">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
<script id="hexo-configurations">
var NexT = window.NexT || {};
var CONFIG = {"hostname":"example.com","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
</script>
<meta property="og:type" content="website">
<meta property="og:title" content="Joan Hou">
<meta property="og:url" content="http://example.com/index.html">
<meta property="og:site_name" content="Joan Hou">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="John Hou">
<meta name="twitter:card" content="summary">
<link rel="canonical" href="http://example.com/">
<script id="page-configurations">
// https://hexo.io/docs/variables.html
CONFIG.page = {
sidebar: "",
isHome : true,
isPost : false,
lang : 'en'
};
</script>
<title>Joan Hou</title>
<noscript>
<style>
.use-motion .brand,
.use-motion .menu-item,
.sidebar-inner,
.use-motion .post-block,
.use-motion .pagination,
.use-motion .comments,
.use-motion .post-header,
.use-motion .post-body,
.use-motion .collection-header { opacity: initial; }
.use-motion .site-title,
.use-motion .site-subtitle {
opacity: initial;
top: initial;
}
.use-motion .logo-line-before i { left: initial; }
.use-motion .logo-line-after i { right: initial; }
</style>
</noscript>
<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; } .github-emoji > span { position: relative; z-index: 10; } .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; } .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>
<body itemscope itemtype="http://schema.org/WebPage">
<div class="container use-motion">
<div class="headband"></div>
<header class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-container">
<div class="site-nav-toggle">
<div class="toggle" aria-label="Toggle navigation bar">
<span class="toggle-line toggle-line-first"></span>
<span class="toggle-line toggle-line-middle"></span>
<span class="toggle-line toggle-line-last"></span>
</div>
</div>
<div class="site-meta">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<h1 class="site-title">Joan Hou</h1>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<div class="site-nav-right">
<div class="toggle popup-trigger">
</div>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="main-menu menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a>
</li>
</ul>
</nav>
</div>
</header>
<div class="back-to-top">
<i class="fa fa-arrow-up"></i>
<span>0%</span>
</div>
<main class="main">
<div class="main-inner">
<div class="content-wrap">
<div class="content index posts-expand">
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
<link itemprop="mainEntityOfPage" href="http://example.com/2020/10/12/Leetcode-Topics-1/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="John Hou">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Joan Hou">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2020/10/12/Leetcode-Topics-1/" class="post-title-link" itemprop="url">Leetcode Topics</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2020-10-12 14:15:50 / Modified: 20:29:43" itemprop="dateCreated datePublished" datetime="2020-10-12T14:15:50+11:00">2020-10-12</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="复制复杂链表"><a href="#复制复杂链表" class="headerlink" title="复制复杂链表"></a>复制复杂链表</h1><blockquote>
<p>题目:请实现 copyRandomList 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null。<br>这是去往<a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof/">原题</a>的链接。</p>
</blockquote>
<h2 id="方法一:直接deepcopy"><a href="#方法一:直接deepcopy" class="headerlink" title="方法一:直接deepcopy"></a>方法一:直接deepcopy</h2><blockquote>
<p>复制分为浅拷贝和深拷贝。浅拷贝只复制指向某个对象的指针,而不复制对象本身,新旧对象还是共享同一块内存。但深拷贝会另外创造一个一模一样的对象,新对象跟原对象不共享内存,修改新对象不会改到原对象。</p>
</blockquote>
<p>明白了以上原理,对于 python 可直接调用相关函数:</p>
<pre><code>class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
return copy.deepcopy(head)</code></pre>
<h2 id="方法二:广度优先搜索-BFS-深度优先搜索(DFS)"><a href="#方法二:广度优先搜索-BFS-深度优先搜索(DFS)" class="headerlink" title="方法二:广度优先搜索 (BFS)/深度优先搜索(DFS)"></a>方法二:广度优先搜索 (BFS)/深度优先搜索(DFS)</h2><p>(先总结一下自己在做题过程中的困扰:初期学习算法,各种基础不够扎实,很多语句无法直接理解,我会尝试在colab中把大佬的代码跑一下,但是当涉及到调用class类中function时,经常会有问题,原因是题目会定义好一些内部class不可见,解决方法是直接在leetcode平常上跑代码即可。)</p>
<pre><code>"""
# Definition for a Node.
class Node:
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
self.val = int(x)
self.next = next
self.random = random
"""
#这表示这个class node在内部被定义了,我们可以在soluton中直接调用
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
if not head:
return None
dic = {} #set 作为hash表 用来存放node的value
cur = head
while cur:
dic[cur] = Node(cur.val)
cur = cur.next
for cur in dic:
if cur.next:
dic[cur].next = dic[cur.next]
if cur.random:
dic[cur].random = dic[cur.random]
return dic[head]</code></pre>
<blockquote>
<p>BFS 广度优先搜索,是从根结点开始,沿着树的宽度遍历树的节点,其应用主要是应用于走迷宫,从起点开始,找出到终点的最短路径。很多最短路径都基于此计算。这题是deep copy,我们利用hash表的特点,所谓哈希表(Hash table),是根据键(Key)而直接访问在内存存储位置的数据结构。</p>
</blockquote>
<p>这里的代码是搬运大佬的,并不是特别理解…<br>只好先来看一下方法三把…</p>
<h2 id="方法三:手写deepcopy"><a href="#方法三:手写deepcopy" class="headerlink" title="方法三:手写deepcopy"></a>方法三:手写deepcopy</h2><p>先来理解一下解题思路:</p>
<p>1.把链表每个节点复制一下(先不复制random),并紧跟被复制节点加入链表</p>
<p>2.给newNode的random赋值,如何赋值呢?可用node.next.random来表示newNode所要指向的random,用node.random.next来表示原来的node指向的random的复制品。令node.next.random = node.random.next</p>
<p>3.将newNode从链表中分离出来</p>
<p>python 3 代码:</p>
<pre><code>"""
# Definition for a Node.
class Node:
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
self.val = int(x)
self.next = next
self.random = random
"""
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
if not head: return
#1.把链表每个节点复制一下(先不复制random),并紧跟被节点加入链表
tmpt = head
while tmpt:
newnode = Node(tmpt.val,None,None)
#把原始node的值赋给newnode
newnode.next,tmpt.next = tmpt.next,newnode
#上面的语句可以用下面两个代替
#newnode.next = tmpt.next
#tmpt.next = newnode #把newnode放在临时变量tmpt.next中,把tmpt.next中的值赋给newnode.next
tmpt = tmpt.next.next
#将新的链表中新加入的节点的random赋值,可用node.next.random = node.random.next
tmpt = head
while tmpt:
if tmpt.random == None:
tmpt.next.random == None
else:
tmpt.next.random = tmpt.random.next
tmpt = tmpt.next.next
#将newNode从链表中分离出来
left = head
right = head.next
start = right
while left.next and right.next:
left.next = left.next.next
right.next = right.next.next
left = left.next
right = right.next
return start</code></pre>
<h1 id="最低票价问题"><a href="#最低票价问题" class="headerlink" title="最低票价问题"></a>最低票价问题</h1><blockquote>
<p>在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行。在接下来的一年里,你要旅行的日子将以一个名为 days 的数组给出。每一项是一个从 1 到 365 的整数。<br>火车票有三种不同的销售方式:<br>一张为期一天的通行证售价为 costs[0] 美元;<br>一张为期七天的通行证售价为 costs[1] 美元;<br>一张为期三十天的通行证售价为 costs[2] 美元。<br>通行证允许数天无限制的旅行。 例如,如果我们在第 2 天获得一张为期 7 天的通行证,那么我们可以连着旅行 7 天:第 2 天、第 3 天、第 4 天、第 5 天、第 6 天、第 7 天和第 8 天。<br>返回你想要完成在给定的列表 days 中列出的每一天的旅行所需要的最低消费。<br>这是去往<a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/minimum-cost-for-tickets/">原题</a>的链接。</p>
</blockquote>
<p>题目分析:本题考察的是一维dynamic programming,解题思路是新建dp array用来存放每天的票价,通过判断dp[i]中的index i是否与days array中的days[index]相对应,来建立两个array之间的联系。难点在于如果i!=days[index]的话,dp[i]=dp[i-1]。如果=的话,那么就需要从三种方案dp[max(0,i-1)]+costs[0],dp[max(0,i-7)]+costs[1],dp[max(0,i-30)]+costs[2]中选出最小的,从而作为iretation的内部,最后取dp[-1],则为最后花的最少的票价。</p>
<p>代码如下:</p>
<pre><code>class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
#对dp array进行初始化,dp array的长度应该是days最后一个元素➕1
dp=[0 for _ in range(days[-1]+1)]
#新增一个变量days_index,标记应该处理 days 数组中哪一个元素,并初始化为0
#这样可以避免多增加一个指针j,指向days[j],从而降低时间复杂度
days_index=0
for i in range(1,len(dp)):
if i!= days[days_index]:
#若当前天数不是待处理天数,则其花费费用和前一天相同
dp[i]=dp[i-1]
else:
#若 i 走到了待处理天数,则从三种方式中选一个最小的
dp[i]=min(dp[max(0,i-1)]+costs[0],
dp[max(0,i-7)]+costs[1],
dp[max(0,i-30)]+costs[2])
days_index += 1
return dp[-1] #返回最后一天对应的费用</code></pre>
<h1 id="棋盘问题(二维Dynamic-Programming)"><a href="#棋盘问题(二维Dynamic-Programming)" class="headerlink" title="棋盘问题(二维Dynamic Programming)"></a>棋盘问题(二维Dynamic Programming)</h1><blockquote>
<p>给你一个 m * n 的网格,其中每个单元格不是 0(空)就是 1(障碍物)。每一步,您都可以在空白单元格中上、下、左、右移动。<br>如果您 最多 可以消除 k 个障碍物,请找出从左上角 (0, 0) 到右下角 (m-1, n-1) 的最短路径,并返回通过该路径所需的步数。如果找不到这样的路径,则返回 -1。<br>这是去往<a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/">原题</a>的链接。</p>
</blockquote>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
</div>
<script>
window.addEventListener('tabs:register', () => {
let { activeClass } = CONFIG.comments;
if (CONFIG.comments.storage) {
activeClass = localStorage.getItem('comments_active') || activeClass;
}
if (activeClass) {
let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
if (activeTab) {
activeTab.click();
}
}
});
if (CONFIG.comments.storage) {
window.addEventListener('tabs:click', event => {
if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
let commentClass = event.target.classList[1];
localStorage.setItem('comments_active', commentClass);
});
}
</script>
</div>
<div class="toggle sidebar-toggle">
<span class="toggle-line toggle-line-first"></span>
<span class="toggle-line toggle-line-middle"></span>
<span class="toggle-line toggle-line-last"></span>
</div>
<aside class="sidebar">
<div class="sidebar-inner">
<ul class="sidebar-nav motion-element">
<li class="sidebar-nav-toc">
Table of Contents
</li>
<li class="sidebar-nav-overview">
Overview
</li>
</ul>
<!--noindex-->
<div class="post-toc-wrap sidebar-panel">
</div>
<!--/noindex-->
<div class="site-overview-wrap sidebar-panel">
<div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
<p class="site-author-name" itemprop="name">John Hou</p>
<div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
<nav class="site-state">
<div class="site-state-item site-state-posts">
<a href="/archives/">
<span class="site-state-item-count">1</span>
<span class="site-state-item-name">posts</span>
</a>
</div>
<div class="site-state-item site-state-tags">
<span class="site-state-item-count">1</span>
<span class="site-state-item-name">tags</span>
</div>
</nav>
</div>
</div>
</div>
</aside>
<div id="sidebar-dimmer"></div>
</div>
</main>
<footer class="footer">
<div class="footer-inner">
<div class="copyright">
©
<span itemprop="copyrightYear">2020</span>
<span class="with-love">
<i class="fa fa-heart"></i>
</span>
<span class="author" itemprop="copyrightHolder">John Hou</span>
</div>
<div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a>
</div>
</div>
</footer>
</div>
<script src="/lib/anime.min.js"></script>
<script src="/lib/velocity/velocity.min.js"></script>
<script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script>
<script src="/js/motion.js"></script>
<script src="/js/schemes/muse.js"></script>
<script src="/js/next-boot.js"></script>
</body>
</html>