-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1205 lines (802 loc) · 74 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="zh-Hans">
<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.4.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":"tate-zwt.github.io","root":"/","scheme":"Mist","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 name="description" content="「 少点虚的,多点实践 」">
<meta property="og:type" content="website">
<meta property="og:title" content="Tate Blog">
<meta property="og:url" content="https://tate-zwt.github.io/index.html">
<meta property="og:site_name" content="Tate Blog">
<meta property="og:description" content="「 少点虚的,多点实践 」">
<meta property="og:locale">
<meta property="article:author" content="爱吃番茄炒蛋">
<meta name="twitter:card" content="summary">
<link rel="canonical" href="https://tate-zwt.github.io/">
<script id="page-configurations">
// https://hexo.io/docs/variables.html
CONFIG.page = {
sidebar: "",
isHome : true,
isPost : false,
lang : 'zh-Hans'
};
</script>
<title>Tate Blog</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>
</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">Tate Blog</h1>
<span class="logo-line-after"><i></i></span>
</a>
<p class="site-subtitle" itemprop="description">iOS Developer</p>
</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-tags">
<a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories</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="zh-Hans">
<link itemprop="mainEntityOfPage" href="https://tate-zwt.github.io/2022/10/01/Flutter%E7%8A%B6%E6%80%81%E7%AE%A1%E7%90%86Riverpod%E3%80%81Provider%E3%80%81GetX/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="爱吃番茄炒蛋">
<meta itemprop="description" content="「 少点虚的,多点实践 」">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Tate Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2022/10/01/Flutter%E7%8A%B6%E6%80%81%E7%AE%A1%E7%90%86Riverpod%E3%80%81Provider%E3%80%81GetX/" class="post-title-link" itemprop="url">Flutter状态管理Riverpod、Provider、GetX</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: 2022-10-01 20:53:22" itemprop="dateCreated datePublished" datetime="2022-10-01T20:53:22+08:00">2022-10-01</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">Edited on</span>
<time title="Modified: 2022-10-06 23:53:22" itemprop="dateModified" datetime="2022-10-06T23:53:22+08:00">2022-10-06</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Flutter/" itemprop="url" rel="index"><span itemprop="name">Flutter</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>刚接触Flutter开发时,不管是为了性能还是项目代码简洁等原因,总归还是会选择一个合适的状态管理工具。但是看着茫茫多的选择还是感觉无从下手,当然我也一样。下方记录一下各种状态管理(Riverpod、Provider、Getx)的使用,以便后续遇到其他项目使用不同状态管理时能快速上手。也希望为看到这篇文章的同学提供一些参考。</p>
<h2 id="Riverpod"><a href="#Riverpod" class="headerlink" title="Riverpod"></a>Riverpod</h2><p>使用的前提需要注意,需要使用ProviderScope包裹父级节点。</p>
<!--noindex-->
<div class="post-button">
<a class="btn" href="/2022/10/01/Flutter%E7%8A%B6%E6%80%81%E7%AE%A1%E7%90%86Riverpod%E3%80%81Provider%E3%80%81GetX/#more" rel="contents">
Read more »
</a>
</div>
<!--/noindex-->
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-Hans">
<link itemprop="mainEntityOfPage" href="https://tate-zwt.github.io/2022/01/07/Swift%20Leetcode%20%E4%BA%8C%E5%8F%89%E6%A0%91%20%E7%AE%97%E6%B3%95%E9%A2%98%E8%A7%A3/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="爱吃番茄炒蛋">
<meta itemprop="description" content="「 少点虚的,多点实践 」">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Tate Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2022/01/07/Swift%20Leetcode%20%E4%BA%8C%E5%8F%89%E6%A0%91%20%E7%AE%97%E6%B3%95%E9%A2%98%E8%A7%A3/" class="post-title-link" itemprop="url">Swift Leetcode 二叉树 算法题解</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: 2022-01-07 14:12:28" itemprop="dateCreated datePublished" datetime="2022-01-07T14:12:28+08:00">2022-01-07</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/" itemprop="url" rel="index"><span itemprop="name">数据结构与算法</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="1-二叉树的前序遍历"><a href="#1-二叉树的前序遍历" class="headerlink" title="1.二叉树的前序遍历"></a>1.<a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/binary-tree-preorder-traversal/">二叉树的前序遍历</a></h3><p>给你二叉树的根节点 <code>root</code> ,返回它节点值的 <strong>前序</strong> 遍历。</p>
<p><strong>示例 1:</strong></p>
<p><img src="https://cdn.jsdelivr.net/gh/Tate-zwt/ImageHost/blog/202201071456889.jpg" alt="img"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入:root = [1,null,2,3]</span><br><span class="line">输出:[1,2,3]</span><br></pre></td></tr></table></figure>
<p><strong>示例 2:</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入:root = []</span><br><span class="line">输出:[]</span><br></pre></td></tr></table></figure>
<figure class="highlight swift"><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></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Definition for a binary tree node.</span></span><br><span class="line"><span class="comment"> * public class TreeNode {</span></span><br><span class="line"><span class="comment"> * public var val: Int</span></span><br><span class="line"><span class="comment"> * public var left: TreeNode?</span></span><br><span class="line"><span class="comment"> * public var right: TreeNode?</span></span><br><span class="line"><span class="comment"> * public init() { self.val = 0; self.left = nil; self.right = nil; }</span></span><br><span class="line"><span class="comment"> * public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }</span></span><br><span class="line"><span class="comment"> * public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {</span></span><br><span class="line"><span class="comment"> * self.val = val</span></span><br><span class="line"><span class="comment"> * self.left = left</span></span><br><span class="line"><span class="comment"> * self.right = right</span></span><br><span class="line"><span class="comment"> * }</span></span><br><span class="line"><span class="comment"> * }</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><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">func</span> <span class="title">preorderTraversal</span>(<span class="keyword">_</span> <span class="params">root</span>: <span class="type">TreeNode</span>?)</span> -> [<span class="type">Int</span>] {</span><br><span class="line"> <span class="keyword">var</span> result <span class="operator">=</span> [<span class="type">Int</span>]()</span><br><span class="line"> <span class="keyword">guard</span> <span class="keyword">let</span> root <span class="operator">=</span> root <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> result</span><br><span class="line"> }</span><br><span class="line"> result.append(root.val)</span><br><span class="line"> result <span class="operator">+=</span> preorderTraversal(root.left)</span><br><span class="line"> result <span class="operator">+=</span> preorderTraversal(root.right)</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>
<p>解题思路:</p>
<ol>
<li>前序遍历按照根节点->左子树->右子树的顺序访问</li>
<li>访问根节点</li>
<li>采用中序递归遍历左子树</li>
<li>采用中序递归遍历右子树</li>
</ol>
<!--noindex-->
<div class="post-button">
<a class="btn" href="/2022/01/07/Swift%20Leetcode%20%E4%BA%8C%E5%8F%89%E6%A0%91%20%E7%AE%97%E6%B3%95%E9%A2%98%E8%A7%A3/#more" rel="contents">
Read more »
</a>
</div>
<!--/noindex-->
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-Hans">
<link itemprop="mainEntityOfPage" href="https://tate-zwt.github.io/2022/01/06/Swift%20Leetcode%20%E5%AD%97%E7%AC%A6%E4%B8%B2%20%E7%AE%97%E6%B3%95%E9%A2%98%E8%A7%A3/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="爱吃番茄炒蛋">
<meta itemprop="description" content="「 少点虚的,多点实践 」">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Tate Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2022/01/06/Swift%20Leetcode%20%E5%AD%97%E7%AC%A6%E4%B8%B2%20%E7%AE%97%E6%B3%95%E9%A2%98%E8%A7%A3/" class="post-title-link" itemprop="url">Swift Leetcode 字符串 算法题解</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: 2022-01-06 20:21:18 / Modified: 17:21:18" itemprop="dateCreated datePublished" datetime="2022-01-06T20:21:18+08:00">2022-01-06</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/" itemprop="url" rel="index"><span itemprop="name">数据结构与算法</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="1-字符串轮转"><a href="#1-字符串轮转" class="headerlink" title="1.字符串轮转"></a>1.<a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/string-rotation-lcci/">字符串轮转</a></h3><p>字符串轮转。给定两个字符串s1和s2,请编写代码检查s2是否为s1旋转而成(比如,waterbottle是erbottlewat旋转后的字符串)。</p>
<p>示例1:</p>
<p> 输入:s1 = “waterbottle”, s2 = “erbottlewat”<br> 输出:True<br>示例2:</p>
<p> 输入:s1 = “aa”, s2 = “aba”<br> 输出:False<br>提示:</p>
<p>字符串长度在[0, 100000]范围内。<br>说明:</p>
<p>你能只调用一次检查子串的方法吗?</p>
<figure class="highlight swift"><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></pre></td><td class="code"><pre><span class="line"><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">func</span> <span class="title">isFlipedString</span>(<span class="keyword">_</span> <span class="params">s1</span>: <span class="type">String</span>, <span class="keyword">_</span> <span class="params">s2</span>: <span class="type">String</span>)</span> -> <span class="type">Bool</span> {</span><br><span class="line"> <span class="keyword">if</span> s1 <span class="operator">==</span> <span class="string">""</span> <span class="operator">&&</span> s2 <span class="operator">==</span> <span class="string">""</span> { <span class="keyword">return</span> <span class="literal">true</span> } <span class="comment">//处理空字符串</span></span><br><span class="line"> <span class="keyword">guard</span> s1.count <span class="operator">==</span> s2.count <span class="keyword">else</span> {<span class="keyword">return</span> <span class="literal">false</span>}</span><br><span class="line"> <span class="keyword">return</span> (s1 <span class="operator">+</span> s1).contains(s2)</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>解题思路:</p>
<ol>
<li>利用s1+s1拼接起来,这样子就包括所有的旋转字符串了</li>
<li>只要新的字符串包含s2直接返回true</li>
</ol>
<!--noindex-->
<div class="post-button">
<a class="btn" href="/2022/01/06/Swift%20Leetcode%20%E5%AD%97%E7%AC%A6%E4%B8%B2%20%E7%AE%97%E6%B3%95%E9%A2%98%E8%A7%A3/#more" rel="contents">
Read more »
</a>
</div>
<!--/noindex-->
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-Hans">
<link itemprop="mainEntityOfPage" href="https://tate-zwt.github.io/2021/12/30/Swift%20Leetcode%20%E6%A0%88%E3%80%81%E9%98%9F%E5%88%97%20%E7%AE%97%E6%B3%95%E9%A2%98%E8%A7%A3/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="爱吃番茄炒蛋">
<meta itemprop="description" content="「 少点虚的,多点实践 」">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Tate Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/12/30/Swift%20Leetcode%20%E6%A0%88%E3%80%81%E9%98%9F%E5%88%97%20%E7%AE%97%E6%B3%95%E9%A2%98%E8%A7%A3/" class="post-title-link" itemprop="url">Swift Leetcode 栈、队列 算法题解</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: 2021-12-30 20:36:31" itemprop="dateCreated datePublished" datetime="2021-12-30T20:36:31+08:00">2021-12-30</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">Edited on</span>
<time title="Modified: 2021-01-06 23:22:18" itemprop="dateModified" datetime="2021-01-06T23:22:18+08:00">2021-01-06</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/" itemprop="url" rel="index"><span itemprop="name">数据结构与算法</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="1-最小栈"><a href="#1-最小栈" class="headerlink" title="1.最小栈"></a>1.<a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/min-stack/">最小栈</a></h3><p>设计一个支持 <code>push</code> ,<code>pop</code> ,<code>top</code> 操作,并能在常数时间内检索到最小元素的栈。</p>
<p>push(x) —— 将元素 x 推入栈中。<br>pop() —— 删除栈顶的元素。<br>top() —— 获取栈顶元素。<br>getMin() —— 检索栈中的最小元素。</p>
<p>示例:</p>
<figure class="highlight plaintext"><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></pre></td><td class="code"><pre><span class="line">输入:</span><br><span class="line">["MinStack","push","push","push","getMin","pop","top","getMin"]</span><br><span class="line">[[],[-2],[0],[-3],[],[],[],[]]</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">输出:</span><br><span class="line">[null,null,null,null,-3,null,0,-2]</span><br><span class="line"></span><br><span class="line">解释:</span><br><span class="line">MinStack minStack = new MinStack();</span><br><span class="line">minStack.push(-2);</span><br><span class="line">minStack.push(0);</span><br><span class="line">minStack.push(-3);</span><br><span class="line">minStack.getMin(); --> 返回 -3.</span><br><span class="line">minStack.pop();</span><br><span class="line">minStack.top(); --> 返回 0.</span><br><span class="line">minStack.getMin(); --> 返回 -2.</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>提示:</p>
<p><code>pop、top 和 getMin 操作总是在 非空栈 上调用。</code></p>
<figure class="highlight swift"><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></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MinStack</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">var</span> head:<span class="type">ListNode</span>?</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">init</span>()</span> {</span><br><span class="line"> head <span class="operator">=</span> <span class="type">ListNode</span>(min:<span class="type">Int</span>.max)</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">func</span> <span class="title">push</span>(<span class="keyword">_</span> <span class="params">val</span>: <span class="type">Int</span>)</span> {</span><br><span class="line"> <span class="comment">//每次push都记录最小值</span></span><br><span class="line"> head <span class="operator">=</span> <span class="type">ListNode</span>(val: val, min: <span class="built_in">min</span>(val, head<span class="operator">!</span>.min), next: head)</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">func</span> <span class="title">pop</span>()</span> {</span><br><span class="line"> head <span class="operator">=</span> head<span class="operator">?</span>.next</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">func</span> <span class="title">top</span>()</span> -> <span class="type">Int</span> {</span><br><span class="line"> <span class="keyword">return</span> head<span class="operator">?</span>.val <span class="operator">??</span> <span class="number">0</span></span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">func</span> <span class="title">getMin</span>()</span> -> <span class="type">Int</span> {</span><br><span class="line"> <span class="keyword">return</span> head<span class="operator">?</span>.min <span class="operator">??</span> <span class="number">0</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="class"><span class="keyword">class</span> <span class="title">ListNode</span> </span>{</span><br><span class="line"> <span class="keyword">let</span> val: <span class="type">Int</span></span><br><span class="line"> <span class="keyword">let</span> min:<span class="type">Int</span></span><br><span class="line"> <span class="keyword">let</span> next: <span class="type">ListNode</span>?</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">init</span>(<span class="params">val</span>: <span class="type">Int</span> <span class="operator">=</span> <span class="number">0</span>,<span class="params">min</span>:<span class="type">Int</span> <span class="operator">=</span> <span class="number">0</span>,<span class="params">next</span>:<span class="type">ListNode</span>? <span class="operator">=</span> <span class="literal">nil</span>)</span> {</span><br><span class="line"> <span class="keyword">self</span>.val <span class="operator">=</span> val</span><br><span class="line"> <span class="keyword">self</span>.min <span class="operator">=</span> min</span><br><span class="line"> <span class="keyword">self</span>.next <span class="operator">=</span> 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><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Your MinStack object will be instantiated and called as such:</span></span><br><span class="line"><span class="comment"> * let obj = MinStack()</span></span><br><span class="line"><span class="comment"> * obj.push(val)</span></span><br><span class="line"><span class="comment"> * obj.pop()</span></span><br><span class="line"><span class="comment"> * let ret_3: Int = obj.top()</span></span><br><span class="line"><span class="comment"> * let ret_4: Int = obj.getMin()</span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>
<p>解题思路:</p>
<p>题目要求我们从栈元素中找到最小值,按照正常想,我们都会遍历一遍栈的元素,但是这样时间复杂度就是O(n),题目要求是O(1),一般遇到这种问题,就需要空间换时间了。<br>使用链表,在每个节点保存当前的最小值,每次push都记录当前元素对应的最小值,每个节点都一一对应一个最小值,这样子getMin时间复杂度就是O(1),同道理,这里用两个栈也可以做到一一对应也能解决。</p>
<!--noindex-->
<div class="post-button">
<a class="btn" href="/2021/12/30/Swift%20Leetcode%20%E6%A0%88%E3%80%81%E9%98%9F%E5%88%97%20%E7%AE%97%E6%B3%95%E9%A2%98%E8%A7%A3/#more" rel="contents">
Read more »
</a>
</div>
<!--/noindex-->
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-Hans">
<link itemprop="mainEntityOfPage" href="https://tate-zwt.github.io/2021/12/27/Swift%20Leetcode%20%E9%93%BE%E8%A1%A8%20%E7%AE%97%E6%B3%95%E9%A2%98%E8%A7%A3/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="爱吃番茄炒蛋">
<meta itemprop="description" content="「 少点虚的,多点实践 」">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Tate Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/12/27/Swift%20Leetcode%20%E9%93%BE%E8%A1%A8%20%E7%AE%97%E6%B3%95%E9%A2%98%E8%A7%A3/" class="post-title-link" itemprop="url">Swift Leetcode 链表 算法题解</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: 2021-12-27 00:00:00" itemprop="dateCreated datePublished" datetime="2021-12-27T00:00:00+08:00">2021-12-27</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">Edited on</span>
<time title="Modified: 2021-12-30 00:00:00" itemprop="dateModified" datetime="2021-12-30T00:00:00+08:00">2021-12-30</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/" itemprop="url" rel="index"><span itemprop="name">数据结构与算法</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="1-移除链表元素"><a href="#1-移除链表元素" class="headerlink" title="1.移除链表元素"></a>1.<a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/remove-linked-list-elements/">移除链表元素</a></h3><p>给你一个链表的头节点 <code>head</code> 和一个整数 <code>val</code> ,请你删除链表中所有满足 <code>Node.val == val</code> 的节点,并返回 <strong>新的头节点</strong> 。</p>
<p>**示例 **</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入:head = [1,2,6,3,4,5,6], val = 6</span><br><span class="line">输出:[1,2,3,4,5]</span><br></pre></td></tr></table></figure>
<figure class="highlight swift"><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></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">ListNode</span> </span>{</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">var</span> val: <span class="type">Int</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">var</span> next: <span class="type">ListNode</span>?</span><br><span class="line"> <span class="keyword">public</span> <span class="function"><span class="keyword">init</span>()</span> { <span class="keyword">self</span>.val <span class="operator">=</span> <span class="number">0</span>; <span class="keyword">self</span>.next <span class="operator">=</span> <span class="literal">nil</span>; }</span><br><span class="line"> <span class="keyword">public</span> <span class="function"><span class="keyword">init</span>(<span class="keyword">_</span> <span class="params">val</span>: <span class="type">Int</span>)</span> { <span class="keyword">self</span>.val <span class="operator">=</span> val; <span class="keyword">self</span>.next <span class="operator">=</span> <span class="literal">nil</span>; }</span><br><span class="line"> <span class="keyword">public</span> <span class="function"><span class="keyword">init</span>(<span class="keyword">_</span> <span class="params">val</span>: <span class="type">Int</span>, <span class="keyword">_</span> <span class="params">next</span>: <span class="type">ListNode</span>?)</span> { <span class="keyword">self</span>.val <span class="operator">=</span> val; <span class="keyword">self</span>.next <span class="operator">=</span> next; }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">removeElements</span>(<span class="keyword">_</span> <span class="params">head</span>: <span class="type">ListNode</span>?, <span class="keyword">_</span> <span class="params">val</span>: <span class="type">Int</span>)</span> -> <span class="type">ListNode</span>? {</span><br><span class="line"> <span class="keyword">guard</span> head <span class="operator">!=</span> <span class="literal">nil</span> <span class="keyword">else</span> { <span class="keyword">return</span> head }</span><br><span class="line"> <span class="comment">//虚拟头结点 也是新链表的头结点</span></span><br><span class="line"> <span class="keyword">let</span> dummyHead <span class="operator">=</span> <span class="type">ListNode</span>() </span><br><span class="line"> <span class="comment">//新链表尾结点</span></span><br><span class="line"> <span class="keyword">var</span> newTail <span class="operator">=</span> dummyHead</span><br><span class="line"> </span><br><span class="line"> <span class="comment">//用来遍历的链表头,swift传参都是不可变的</span></span><br><span class="line"> <span class="keyword">var</span> curHead <span class="operator">=</span> head</span><br><span class="line"></span><br><span class="line"> <span class="comment">//遍历链表</span></span><br><span class="line"> <span class="keyword">while</span> curHead <span class="operator">!=</span> <span class="literal">nil</span> {</span><br><span class="line"> <span class="keyword">if</span> curHead<span class="operator">?</span>.val <span class="operator">!=</span> val {</span><br><span class="line"> newTail.next <span class="operator">=</span> curHead</span><br><span class="line"> newTail <span class="operator">=</span> curHead<span class="operator">!</span></span><br><span class="line"> }</span><br><span class="line"> curHead <span class="operator">=</span> curHead<span class="operator">?</span>.next</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//尾部置空</span></span><br><span class="line"> newTail.next <span class="operator">=</span> <span class="literal">nil</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> dummyHead.next</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>解题思路:<br>这里用一个虚拟头结点来作为新链表的头结点会让代码更加的简洁,遍历传进来的链表,判断当前的链表是否是需要删除的如果不是需要删除的,让新链表尾结点下一个指向它,也就是尾部结点也是当前结点,最后把尾部结点置空,返回新节点头的下一个结点。</p>
<!--noindex-->
<div class="post-button">
<a class="btn" href="/2021/12/27/Swift%20Leetcode%20%E9%93%BE%E8%A1%A8%20%E7%AE%97%E6%B3%95%E9%A2%98%E8%A7%A3/#more" rel="contents">
Read more »
</a>
</div>
<!--/noindex-->
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-Hans">
<link itemprop="mainEntityOfPage" href="https://tate-zwt.github.io/2021/12/24/Swift%20Leetcode%20%E6%95%B0%E7%BB%84%E6%8E%92%E5%BA%8F%20%E7%AE%97%E6%B3%95%E9%A2%98%E8%A7%A3/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="爱吃番茄炒蛋">
<meta itemprop="description" content="「 少点虚的,多点实践 」">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Tate Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/12/24/Swift%20Leetcode%20%E6%95%B0%E7%BB%84%E6%8E%92%E5%BA%8F%20%E7%AE%97%E6%B3%95%E9%A2%98%E8%A7%A3/" class="post-title-link" itemprop="url">Swift Leetcode 数组排序 算法题解</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: 2021-12-24 00:00:00" itemprop="dateCreated datePublished" datetime="2021-12-24T00:00:00+08:00">2021-12-24</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">Edited on</span>
<time title="Modified: 2021-12-26 00:00:00" itemprop="dateModified" datetime="2021-12-26T00:00:00+08:00">2021-12-26</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/" itemprop="url" rel="index"><span itemprop="name">数据结构与算法</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="1-合并两个有序数组"><a href="#1-合并两个有序数组" class="headerlink" title="1. 合并两个有序数组"></a>1. <a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/merge-sorted-array/">合并两个有序数组</a></h3><p>给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目。</p>
<p>请你 合并 nums2 到 nums1 中,使合并后的数组同样按 非递减顺序 排列。</p>
<p>注意:最终,合并后数组不应由函数返回,而是存储在数组 nums1 中。为了应对这种情况,nums1 的初始长度为 m + n,其中前 m 个元素表示应合并的元素,后 n 个元素为 0 ,应忽略。nums2 的长度为 n 。</p>
<p><strong>示例 :</strong></p>
<p>输入:nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3<br>输出:[1,2,2,3,5,6]<br>解释:需要合并 [1,2,3] 和 [2,5,6] 。<br>合并结果是 [1,2,2,3,5,6] ,其中斜体加粗标注的为 nums1 中的元素。</p>
<figure class="highlight swift"><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></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">merge</span>(<span class="keyword">_</span> <span class="params">nums1</span>: <span class="keyword">inout</span> [<span class="type">Int</span>], <span class="keyword">_</span> <span class="params">m</span>: <span class="type">Int</span>, <span class="keyword">_</span> <span class="params">nums2</span>: [<span class="type">Int</span>], <span class="keyword">_</span> <span class="params">n</span>: <span class="type">Int</span>)</span> {</span><br><span class="line"> <span class="comment">//思路是使用双指针的思路,这里用到了归并排序的思想</span></span><br><span class="line"> <span class="keyword">var</span> i1 <span class="operator">=</span> m <span class="operator">-</span> <span class="number">1</span></span><br><span class="line"> <span class="keyword">var</span> i2 <span class="operator">=</span> n <span class="operator">-</span> <span class="number">1</span></span><br><span class="line"> <span class="keyword">var</span> cur <span class="operator">=</span> nums1.count <span class="operator">-</span> <span class="number">1</span></span><br><span class="line"> <span class="keyword">while</span> i2 <span class="operator">>=</span> <span class="number">0</span> { <span class="comment">//这里必须要大于等于0 代表还没有合并结束</span></span><br><span class="line"> <span class="keyword">if</span> i1 <span class="operator">>=</span> <span class="number">0</span> <span class="operator">&&</span> nums2[i2] <span class="operator"><</span> nums1[i1] {</span><br><span class="line"> nums1[cur] <span class="operator">=</span> nums1[i1]</span><br><span class="line"> i1 <span class="operator">-=</span> <span class="number">1</span></span><br><span class="line"> }<span class="keyword">else</span>{ <span class="comment">// i1 < 0 || nums2[i2] >= nums1[i1] 这里i1已经为0了</span></span><br><span class="line"> nums1[cur] <span class="operator">=</span> nums2[i2]</span><br><span class="line"> i2 <span class="operator">-=</span> <span class="number">1</span></span><br><span class="line"> }</span><br><span class="line"> cur <span class="operator">-=</span> <span class="number">1</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>解题思路:</p>
<p>双指针法,从后面插入替换合并,把nums2数值添加到nums1,如果在前面插入会替换到后面的数字</p>
<p>只要nums2的指针索引>=0代表还没有合并结束,只要成功合并一次,两个指针索引都需要向后移动一位</p>
<p>这里还需要考虑到比如nums1的长度比nums2 数组长度短的情况,所以 else 里就是 // i1 < 0 || nums2[i2] >= nums1[i1] 这里i1已经为0了 这里直接把nums2的数字往nums1数组合并</p>
<p>这里解题思路用到了归并排序算法:</p>
<p><img src="https://cdn.jsdelivr.net/gh/Tate-zwt/ImageHost/blog/202112241037111.gif" alt="图片"></p>
<ol>
<li>申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列;</li>
<li>设定两个指针,最初位置分别为两个已经排序序列的起始位置;</li>
<li>比较两个指针所指向的元素,选择相对小的元素放入到合并空间,并移动指针到下一位置;</li>
<li>重复步骤 3 直到某一指针达到序列尾;</li>
<li>将另一序列剩下的所有元素直接复制到合并序列尾。</li>
</ol>
<!--noindex-->
<div class="post-button">
<a class="btn" href="/2021/12/24/Swift%20Leetcode%20%E6%95%B0%E7%BB%84%E6%8E%92%E5%BA%8F%20%E7%AE%97%E6%B3%95%E9%A2%98%E8%A7%A3/#more" rel="contents">
Read more »
</a>
</div>
<!--/noindex-->
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-Hans">
<link itemprop="mainEntityOfPage" href="https://tate-zwt.github.io/2021/12/17/%E5%B0%8F%E7%A8%8B%E5%BA%8F/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="爱吃番茄炒蛋">
<meta itemprop="description" content="「 少点虚的,多点实践 」">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Tate Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/12/17/%E5%B0%8F%E7%A8%8B%E5%BA%8F/" class="post-title-link" itemprop="url">小程序</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: 2021-12-17 17:31:00" itemprop="dateCreated datePublished" datetime="2021-12-17T17:31:00+08:00">2021-12-17</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/%E5%B0%8F%E7%A8%8B%E5%BA%8F/" itemprop="url" rel="index"><span itemprop="name">小程序</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="初识"><a href="#初识" class="headerlink" title="初识"></a>初识</h2><p>作为一个移动端iOS开发,小程序也是有必要学习的,学好小程序的基本流程,会发现跟iOS原生开发很多都相似的,比如每个界面都有它的生命周期,数据绑定,响应事件等等,而且微信团队把它封装得非常易用,只要看文档,补充下CSS的知识就可以上手开发。</p>
<h3 id="CSS布局知识"><a href="#CSS布局知识" class="headerlink" title="CSS布局知识"></a>CSS布局知识</h3><p>clssname .clssname</p>
<figure class="highlight css"><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></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.clssname</span> <span class="selector-class">.clssname</span>{</span><br><span class="line"> <span class="attribute">float</span><span class="selector-pseudo">:left</span>;</span><br><span class="line"> <span class="attribute">height</span>: <span class="number">100</span>rpx;</span><br><span class="line"> <span class="attribute">line-height</span>: <span class="number">100</span>rpx;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>代表 clssname 里的clssname样式更改</p>
<p>clssname view</p>
<figure class="highlight css"><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></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.clssname</span> view{</span><br><span class="line"> <span class="attribute">flex</span>:<span class="number">1</span>;</span><br><span class="line"> <span class="attribute">color</span>:white;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>代表 clssname 里所有的view样式更改</p>
<!--noindex-->
<div class="post-button">
<a class="btn" href="/2021/12/17/%E5%B0%8F%E7%A8%8B%E5%BA%8F/#more" rel="contents">
Read more »
</a>
</div>
<!--/noindex-->
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-Hans">
<link itemprop="mainEntityOfPage" href="https://tate-zwt.github.io/2021/10/17/%E5%8D%81%E5%A4%A7%E5%B8%B8%E8%A7%81%E7%AE%97%E6%B3%95%E6%A0%B8%E5%BF%83%E6%80%9D%E6%83%B3/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="爱吃番茄炒蛋">
<meta itemprop="description" content="「 少点虚的,多点实践 」">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Tate Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/10/17/%E5%8D%81%E5%A4%A7%E5%B8%B8%E8%A7%81%E7%AE%97%E6%B3%95%E6%A0%B8%E5%BF%83%E6%80%9D%E6%83%B3/" class="post-title-link" itemprop="url">十大常见算法核心思想</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: 2021-10-17 14:13:37 / Modified: 00:00:00" itemprop="dateCreated datePublished" datetime="2021-10-17T14:13:37+08:00">2021-10-17</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/" itemprop="url" rel="index"><span itemprop="name">数据结构与算法</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="二分查找算法-非递归-:"><a href="#二分查找算法-非递归-:" class="headerlink" title="二分查找算法(非递归):"></a>二分查找算法(非递归):</h3><ol>
<li>二分查找算法有递归和非递归方式,下面我们讲解二分查找算法的非递归方式 </li>
<li>二分查找法只适用于从有序的数列中进行查找(比如数字和字母等),将数列排序后再进行查找</li>
<li>二分查找法的运行时间为对数时间 O(㏒₂n) ,即查找到需要的目标位置最多只需要㏒₂n 步,假设从[0,99]的 队列(100 个数,即 n=100)中寻到目标数 30,则需要查找步数为㏒₂100 , 即最多需要查找 7 次( 2^6 < 100 < 2^7)</li>
</ol>
<h3 id="分治算法:"><a href="#分治算法:" class="headerlink" title="分治算法:"></a>分治算法:</h3><ol>
<li>分治法是一种很重要的算法。字面上的解释是“分而治之”,就是把一个复杂的问题分成两个或更多的相同或 相似的子问题,再把子问题分成更小的子问题……直到最后子问题可以简单的直接求解,原问题的解即子问题 的解的合并。这个技巧是很多高效算法的基础,如排序算法(快速排序,归并排序),傅立叶变换(快速傅立叶变 换)…… </li>
<li>分治算法可以求解的一些经典问题</li>
</ol>
<ul>
<li>二分搜索 </li>
<li>大整数乘法 </li>
<li>棋盘覆盖 </li>
<li>合并排序 </li>
<li>快速排序 </li>
<li>线性时间选择</li>
<li>最接近点对问题 </li>
<li>循环赛日程表 </li>
<li>汉诺塔</li>
</ul>
<!--noindex-->
<div class="post-button">
<a class="btn" href="/2021/10/17/%E5%8D%81%E5%A4%A7%E5%B8%B8%E8%A7%81%E7%AE%97%E6%B3%95%E6%A0%B8%E5%BF%83%E6%80%9D%E6%83%B3/#more" rel="contents">
Read more »
</a>
</div>
<!--/noindex-->
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-Hans">
<link itemprop="mainEntityOfPage" href="https://tate-zwt.github.io/2021/03/05/SwiftUI-Develop-Tips/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="爱吃番茄炒蛋">
<meta itemprop="description" content="「 少点虚的,多点实践 」">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Tate Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/03/05/SwiftUI-Develop-Tips/" class="post-title-link" itemprop="url">SwiftUI Develop Tips</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: 2021-03-05 20:42:52" itemprop="dateCreated datePublished" datetime="2021-03-05T20:42:52+08:00">2021-03-05</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/iOS/" itemprop="url" rel="index"><span itemprop="name">iOS</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="1、使用SwiftRealm会导致没法预览报Updating-took-more-than-5-seconds"><a href="#1、使用SwiftRealm会导致没法预览报Updating-took-more-than-5-seconds" class="headerlink" title="1、使用SwiftRealm会导致没法预览报Updating took more than 5 seconds"></a>1、使用SwiftRealm会导致没法预览报<code>Updating took more than 5 seconds</code></h2><p>花了点时间查找问题,最后发现是因为初始化数据库写在AppDelegate,写在SceneDelegate就没事了!</p>
<h2 id="2、SwiftUI数据流温故知新-属性包装"><a href="#2、SwiftUI数据流温故知新-属性包装" class="headerlink" title="2、SwiftUI数据流温故知新 - 属性包装"></a>2、SwiftUI数据流温故知新 - 属性包装</h2><h3 id="State"><a href="#State" class="headerlink" title="@State"></a>@State</h3><p>@State属性允许修改Struct的属性,这些属性在普通的Struct里面是不允许修改的。当把@State放置到属性前,该属性实际上会被放到Struct的外部存储起来,这意味着SwiftUI能够随时销毁和重建Struct而不会丢失属性的值。<br>@State包装的属性通常是设置成私有的,不让外部使用。如果想让外部使用,则应该使用@ObservedObject和@EnvironmentObject,他们能够使外部修改属性后,状态能够得到改变。<br>建议把@State包装的属性都设置成私有</p>
<h3 id="Published"><a href="#Published" class="headerlink" title="@Published"></a>@Published</h3><p>@Published是SwiftUI最有用的包装之一,允许我们创建出能够被自动观察的对象属性,SwiftUI会自动监视这个属性,一旦发生了改变,会自动修改与该属性绑定的界面。<br>1.首先需要遵循ObservableObject属性</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">class Bag: ObservableObject { var items = [String]() }</span><br></pre></td></tr></table></figure>
<p>2.包装属性</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">class Bag: ObservableObject { @Published var items = [String]() }</span><br></pre></td></tr></table></figure>
<p>这样就完成了。@Published包装会自动添加willSet方法监视属性的改变。</p>
<h3 id="ObservedObject"><a href="#ObservedObject" class="headerlink" title="@ObservedObject"></a>@ObservedObject</h3><p>@ObservedObject告诉SwiftUI,这个对象是可以被观察的,里面含有被@Published包装了的属性。<br>@ObservedObject包装的对象,必须遵循ObservableObject协议。也就是说必须是class对象,不能是struct。<br>@ObservedObject允许外部进行访问和修改。</p>
<h3 id="EnvironmentObject"><a href="#EnvironmentObject" class="headerlink" title="@EnvironmentObject"></a>@EnvironmentObject</h3><p>@EnvironmentObject包装的属性是全局的,整个app都可以访问<br>需要注意的是,不需要给定默认值,由于该属性是整个app都可以访问的,SwiftUI会自动的从环境中取出来。<br>当有这样一个场景,A->B->C->D->E->F,A界面的数据要传递给F界面,假如使用@ObservedObject包装,则需要一层一层传递,而使用@EnvironmentObject则不需要,直接在F界面,通过SwiftUI环境直接取出来就行。<br>需要注意的是,当界面显示时,就会去环境中取,但是,假如之前没有把属性放到环境中,则程序会崩溃。</p>
<h3 id="Environment"><a href="#Environment" class="headerlink" title="@Environment"></a>@Environment</h3><p>@Environment与@EnvironmentObject作用是不同的,@Environment是从环境中取出预定义的值,比如获得当前是暗黑模式还是正常模式,屏幕的大小等等。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">@Environment(\.horizontalSizeClass) var horizontalSizeClass</span><br><span class="line">@Environment(\.managedObjectContext) var managedObjectContext</span><br></pre></td></tr></table></figure>
<h3 id="Binding"><a href="#Binding" class="headerlink" title="@Binding"></a>@Binding</h3><p>@Binding也是非常重要的一个包装,声明一个属性是从外部获取的,并且与外部是共享的。相当于外部传过来的时候,不是传递的值。任何一方修改都会让SwiftUI的监视生效。</p>
<h3 id="GestureState"><a href="#GestureState" class="headerlink" title="@GestureState"></a>@GestureState</h3><p>@GestureState能够让我们传递手势的状态,虽然使用@State也能实现,但@GestureState能让手势结束后我们回到最初的状态。</p>
<p>设置属性,设置zero意味着手势结束后,会回到最初的值。</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-Hans">
<link itemprop="mainEntityOfPage" href="https://tate-zwt.github.io/2021/01/03/Python-Develop-Tips/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="爱吃番茄炒蛋">
<meta itemprop="description" content="「 少点虚的,多点实践 」">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Tate Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/01/03/Python-Develop-Tips/" class="post-title-link" itemprop="url">Python Develop Tips</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: 2021-01-03 22:41:44" itemprop="dateCreated datePublished" datetime="2021-01-03T22:41:44+08:00">2021-01-03</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">前端</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">