-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
918 lines (658 loc) · 143 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hexo</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta property="og:type" content="website">
<meta property="og:title" content="Hexo">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
<link rel="icon" href="/favicon.png">
<link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="/css/style.css">
<meta name="generator" content="Hexo 4.2.0"></head>
<body>
<div id="container">
<div id="wrap">
<header id="header">
<div id="banner"></div>
<div id="header-outer" class="outer">
<div id="header-title" class="inner">
<h1 id="logo-wrap">
<a href="/" id="logo">Hexo</a>
</h1>
</div>
<div id="header-inner" class="inner">
<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>
<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>
<div id="search-form-wrap">
<form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit"></button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
</div>
</div>
</div>
</header>
<div class="outer">
<section id="main">
<article id="post-碎嘴算法-普通队列" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2020/03/10/%E7%A2%8E%E5%98%B4%E7%AE%97%E6%B3%95-%E6%99%AE%E9%80%9A%E9%98%9F%E5%88%97/" class="article-date">
<time datetime="2020-03-10T12:52:17.000Z" itemprop="datePublished">2020-03-10</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2020/03/10/%E7%A2%8E%E5%98%B4%E7%AE%97%E6%B3%95-%E6%99%AE%E9%80%9A%E9%98%9F%E5%88%97/">碎嘴算法--普通队列</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>首先明确一下队列的概念.</p>
<p>队列是一种有序列表,使用数组的结构来存储队列的数据.</p>
<p>队列是一种先进先出的算法.由前端加入,由后端输出.</p>
<p>如下图:</p>
<p><img src="https://pic.leetcode-cn.com/942a95e334b897f28de5ae2956fcc736929aadbafec3eb5c85885a6195f6489b-image.png" alt="image.png"></p>
<p> 第一个图 第二个图 第三个图</p>
<p>这就是队列的形状.</p>
<p>他的本体是一个空空的数组(Queue)加上两个指在同一个位置的指针(rear,front).</p>
<p>这个数组有长度的限制(因为是java算法,所以数组长度有限制,必须在声明时就写出他的大小.)</p>
<p>于是我们看到了上面的情况,如果要加入元素就移动后面的指针(rear),如果要获取(退出)元素就把前面的指针移动(front).</p>
<p>于是我们可以产生这样的一个思路.</p>
<ol>
<li>front永远指向队列中第一个值</li>
<li>rear永远指向队列中最后一个值</li>
<li>当rear==front的时候队列为空(理解不了就看一眼第一个图)</li>
<li>移动front是从队中删除元素</li>
<li>移动rear是从队中添加元素</li>
</ol>
<p>由此我们可以写出代码:</p>
<p> 第一部分是创建一个队列.(与稀疏数组一样,第一步都是创建这个数据结构)</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></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ArrayQueue</span></span>{</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> maxSize;<span class="comment">//表示数组的最大容量</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> front;<span class="comment">//队列头</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> rear;<span class="comment">//队列尾</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> arr[];<span class="comment">//该数据用于存放数据,模拟队列</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>如上几个就是构建队列所需要的资源</p>
<p>但是这个资源有一个值必须由外部来给定,那就是maxSize.</p>
<p>所以构造函数如下:</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></pre></td><td class="code"><pre><span class="line"><span class="comment">//创建队列的构造器</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">ArrayQueue</span><span class="params">(<span class="keyword">int</span> maxSize)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.maxSize = maxSize;</span><br><span class="line"> arr=<span class="keyword">new</span> <span class="keyword">int</span>[maxSize];</span><br><span class="line"> front=-<span class="number">1</span>;<span class="comment">//只想对队列头部,分析出front是指向队里的头的*前一个位置*</span></span><br><span class="line"> rear=-<span class="number">1</span>;<span class="comment">//指向队列尾的数据(即就是队列最后的一个数据)</span></span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>由此我们就可以构建出一个队列了</p>
<p>但是一个队列并没有什么用,我们还要操作这个家伙.</p>
<p>如果我们要构造<strong>加入队列</strong>的功能,思路一般是构造一个<strong>循环</strong>,而这个循环的停止条件则是应该是<strong>队列满</strong></p>
<p>就必须要有判断队列是否满的功能.</p>
<p>ISFULL</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></pre></td><td class="code"><pre><span class="line"> </span><br><span class="line"><span class="comment">//看图就知道,当rear指到最后一个的时候合格就无法继续添加了</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isFull</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> rear==maxSize-<span class="number">1</span>;</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>addQueue:加入队列功能.根据上面说过的内容,rear指针向后移动就是像队列中加入元素.</p>
<p>如果满了,就必须阻止这个加入步骤.</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></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addQueue</span><span class="params">(<span class="keyword">int</span> a)</span></span>{</span><br><span class="line"> <span class="comment">//判断队列不为空</span></span><br><span class="line"> <span class="keyword">if</span>(isFull()){</span><br><span class="line"> System.out.println(<span class="string">"队列满,不能加入数据"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//加入数据</span></span><br><span class="line"> rear++;<span class="comment">//让rear后移</span></span><br><span class="line"> arr[rear]=a;<span class="comment">//赋值</span></span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>构建完了增加模块,我们就要构造获取模块了.</p>
<p>和增加模块一样,获取模块也准备由两个函数构成.</p>
<p>我们也要担心一下这个是不是空的,要组织获取不存在得数值.</p>
<p>我们知道,当rear==front的时候,这个队列就回到了最初的状态,里面为空.</p>
<p>ISEMPTY</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></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">isEmpty</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">return</span> rear==front;</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>由此我们就只要构建一个获取模块函数可以了,用ISEMPTY来阻止取出不存在的值</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></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getQueue</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">if</span> (isEmpty()){</span><br><span class="line"> System.out.println(<span class="string">"栈空了!"</span>);</span><br><span class="line"><span class="comment">// return -1;这个不行,因为这样的话,只能返回-1</span></span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"队列空。"</span>);</span><br><span class="line"> }</span><br><span class="line"> front++;</span><br><span class="line"> <span class="keyword">return</span> arr[front];</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>这就是队列的主要功能了.</p>
<p>但是要想观察整个队列中有的元素,总不能挨个退出来之后再挨个加上去吧!<br>那就必须重写一个函数了.</p>
<p>正好,因为队列是数组,所以我们只要遍历数组就能得到其中全部的数据,当然这个只是为了让你搞清楚这个队列的结构以及其中的内容,看一下即可.因为它会将队列用过的所有结点都打印出来.</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></pre></td><td class="code"><pre><span class="line"><span class="comment">//显示队列的所有数据</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">showQueue</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">if</span> (isEmpty()){</span><br><span class="line"> System.out.println(<span class="string">"队列是空的。"</span>);</span><br><span class="line"> }</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"> System.out.printf(<span class="string">"arr[%d]=%d\n"</span>,i,arr[i]); <span class="comment">//这个要学习一下。</span></span><br><span class="line"> <span class="comment">//标准化输出解决输出的值是空值的问题。</span></span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>还有一个办法,就是发现头节点.</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></pre></td><td class="code"><pre><span class="line"><span class="comment">//显示队列头数据,注意不是取出数据</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">headQueue</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">if</span> (isEmpty()){</span><br><span class="line"> System.out.println(<span class="string">"队列空的,没有数据"</span>);</span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"队列是空的"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> arr[front+<span class="number">1</span>];<span class="comment">//因为队列头front永远指的是队列中的第一个数的前面一个</span></span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>下面是全部得代码</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><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> queue;</span><br><span class="line"></span><br><span class="line"><span class="comment">//import jdk.nashorn.internal.parser.Scanner;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ArrayQueueDemo</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"><span class="comment">//测试一下</span></span><br><span class="line"> <span class="comment">//创建一个队列</span></span><br><span class="line"> ArrayQueue arrayQueue = <span class="keyword">new</span> ArrayQueue(<span class="number">3</span>);</span><br><span class="line"> <span class="keyword">char</span> key=<span class="string">' '</span>;<span class="comment">//接受用户的输入</span></span><br><span class="line"> Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line"> <span class="keyword">boolean</span> loop=<span class="keyword">true</span>;</span><br><span class="line"> <span class="comment">//输出一个菜单</span></span><br><span class="line"> <span class="keyword">while</span>(loop){</span><br><span class="line"> System.out.println(<span class="string">"s:显示队列"</span>);</span><br><span class="line"> System.out.println(<span class="string">"e:退出程序"</span>);</span><br><span class="line"> System.out.println(<span class="string">"a:添加数据到队列"</span>);</span><br><span class="line"> System.out.println(<span class="string">"g:从队列取出数据"</span>);</span><br><span class="line"> System.out.println(<span class="string">"h:查看队列头的数据"</span>);</span><br><span class="line"><span class="comment">// System.out.println("s:显示队列");</span></span><br><span class="line"> key=scanner.next().charAt(<span class="number">0</span>);</span><br><span class="line"> <span class="keyword">switch</span> (key){</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'s'</span>:</span><br><span class="line"> arrayQueue.showQueue();</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'a'</span>:</span><br><span class="line"> System.out.println(<span class="string">"输出一个数"</span>);</span><br><span class="line"> <span class="keyword">int</span> value=scanner.nextInt();</span><br><span class="line"> arrayQueue.addQueue(value);</span><br><span class="line"> arrayQueue.showQueue();</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'g'</span>:<span class="comment">//取出数据</span></span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="keyword">int</span> res=arrayQueue.getQueue();</span><br><span class="line"> System.out.printf(<span class="string">"取出的数据是%d\n,"</span>,res);</span><br><span class="line"> }<span class="keyword">catch</span> (Exception e){</span><br><span class="line"> System.out.println(e.getMessage());</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'h'</span>:<span class="comment">//查看队列头的数据</span></span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> System.out.println( arrayQueue.headQueue());</span><br><span class="line"> }<span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> System.out.println(e.getMessage());</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'e'</span>:<span class="comment">//退出</span></span><br><span class="line"> scanner.close();</span><br><span class="line"> loop=<span class="keyword">false</span>;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">default</span>:</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"程序退出~~~时"</span>);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="comment">//编写一个叫做ArrayQueue的类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ArrayQueue</span></span>{</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> maxSize;<span class="comment">//表示数组的最大容量</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> front;<span class="comment">//队列头</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> rear;<span class="comment">//队列尾</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> arr[];<span class="comment">//该数据用于存放数据,模拟队列</span></span><br><span class="line"> <span class="comment">//创建队列的构造器</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">ArrayQueue</span><span class="params">(<span class="keyword">int</span> maxSize)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.maxSize = maxSize;</span><br><span class="line"> arr=<span class="keyword">new</span> <span class="keyword">int</span>[maxSize];</span><br><span class="line"> front=-<span class="number">1</span>;<span class="comment">//只想对队列头部,分析出front是指向队里的头的*前一个位置*</span></span><br><span class="line"> rear=-<span class="number">1</span>;<span class="comment">//指向队列尾的数据(即就是队列最后的一个数据)</span></span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//判断队列是否满</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isFull</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> rear==maxSize-<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//判断队列是否为空</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">return</span> rear==front;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//添加数据到队列</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addQueue</span><span class="params">(<span class="keyword">int</span> a)</span></span>{</span><br><span class="line"> <span class="comment">//判断队列不为空</span></span><br><span class="line"> <span class="keyword">if</span>(isFull()){</span><br><span class="line"> System.out.println(<span class="string">"队列满,不能加入数据"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//加入数据</span></span><br><span class="line"> rear++;<span class="comment">//让rear后移</span></span><br><span class="line"> arr[rear]=a;<span class="comment">//赋值</span></span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//获取队列的值</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getQueue</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">if</span> (isEmpty()){</span><br><span class="line"> System.out.println(<span class="string">"栈空了!"</span>);</span><br><span class="line"><span class="comment">// return -1;这个不行,因为这样的话,只能返回-1</span></span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"队列空。"</span>);</span><br><span class="line"> }</span><br><span class="line"> front++;</span><br><span class="line"> <span class="keyword">return</span> arr[front];</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//显示队列的所有数据</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">showQueue</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">if</span> (isEmpty()){</span><br><span class="line"> System.out.println(<span class="string">"队列是空的。"</span>);</span><br><span class="line"> }</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"> System.out.printf(<span class="string">"arr[%d]=%d\n"</span>,i,arr[i]); <span class="comment">//这个要学习一下。</span></span><br><span class="line"> <span class="comment">//标准化输出解决输出的值是空值的问题。</span></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="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">headQueue</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">if</span> (isEmpty()){</span><br><span class="line"> System.out.println(<span class="string">"队列空的,没有数据"</span>);</span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"队列是空的"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> arr[front+<span class="number">1</span>];<span class="comment">//因为队列头front永远指的是队列中的第一个数</span></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://yoursite.com/2020/03/10/%E7%A2%8E%E5%98%B4%E7%AE%97%E6%B3%95-%E6%99%AE%E9%80%9A%E9%98%9F%E5%88%97/" data-id="ck7lwa9i5000128v55nu6dfmn" class="article-share-link">Share</a>
</footer>
</div>
</article>
<article id="post-碎嘴算法-循环队列" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2020/03/10/%E7%A2%8E%E5%98%B4%E7%AE%97%E6%B3%95-%E5%BE%AA%E7%8E%AF%E9%98%9F%E5%88%97/" class="article-date">
<time datetime="2020-03-10T12:49:57.000Z" itemprop="datePublished">2020-03-10</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2020/03/10/%E7%A2%8E%E5%98%B4%E7%AE%97%E6%B3%95-%E5%BE%AA%E7%8E%AF%E9%98%9F%E5%88%97/">碎嘴算法--循环队列</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>我们再用队列得时候不知道发没发现这样一个问题.</p>
<p><img src="C:%5CUsers%5C22643%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20200310145207038.png" alt="image-20200310145207038"></p>
<p>这是一个只有三个位置得队列,在进行三次加入(addqueue)操作和三次取出(get)操作之后再进行加入操作时候的样子.明显可以看到,队列已经无法使用了.队列是靠rear和front指针来进行操作的.这两个指针只能向后运动,并不能向前移动,所以他们走过的数组元素就再也不能利用了.</p>
<p>也就是说队列只能用一次.加入,退出,然后就结束了.</p>
<p>当然了,着对于很多问题是不被原谅的.</p>
<p>所以这就要引用一个全新的概念,环形队列.</p>
<p><img src="C:%5CUsers%5C22643%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20200310150323508.png" alt="image-20200310150323508"></p>
<p>看一下这个吧,一看就知道这个东西可以循环利用.</p>
<p>比较一下:</p>
<p><img src="C:%5CUsers%5C22643%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20200310150540256.png" alt="image-20200310150540256"></p>
<p>来自百度百科</p>
<p>看见了吧,一个是长方形的,一个是圆形的.</p>
<p>但是我们知道计算机中并不存在圆形数组,这种环形队列是构建再逻辑上的.用数学来实现,其本质就是可以让rear走过队尾的时候能回到数组的前方,继续利用被front走过得数组元素.</p>
<p>我们主要看一眼她和普通队列的变化.</p>
<p>首先是构造</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></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CircleArray</span></span>{</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> maxSize;<span class="comment">//表示数组的最大容量</span></span><br><span class="line"> <span class="comment">//front变量的含义做一个调整,front指向队列的第一个元素。也就是说arr【front】就是队列第一个元素,</span></span><br><span class="line"> <span class="comment">//初始值为0</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> front;<span class="comment">//队列头</span></span><br><span class="line"> <span class="comment">//rear变量的含义做一个调整,rear指向队列的最后一个元素的后一个位置。因为我希望空出一个空间作为约定。</span></span><br><span class="line"> <span class="comment">//一位有了这个空格就可以比较轻松地知道,空格前面的是rear后面的是front</span></span><br><span class="line"> <span class="comment">//初始值为0</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> rear;<span class="comment">//队列尾</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> arr[];<span class="comment">//该数据用于存放数据,模拟队列</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>并没有什么变化,就像我刚才说到的,环形队列是用逻辑和数学实现的,而不是从物理地址(也就是改变队列的载体–数组来实现的.)</p>
<p>在这里有了一部分变化()</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></pre></td><td class="code"><pre><span class="line">rear由指向队尾最后一个元素变为指向队尾前一个元素.</span><br><span class="line">front由指向队列头部前一个元素,改为指向队列头部.</span><br><span class="line"> 这实际上是为了方便区分一个队列得首和尾,并不一定要遵守.</span><br></pre></td></tr></table></figure>
<p>变化发生在构造函数中,要知道实际上你所能提供的数据只有一个队列大小,rear,front这些东西</p>
<p>全是通过构造函数在构造之初就构建得,并不能赋值.</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></pre></td><td class="code"><pre><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">CircleArray</span><span class="params">(<span class="keyword">int</span> maxSize)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.maxSize = maxSize;</span><br><span class="line"><span class="comment">// this.maxSize = maxSize;</span></span><br><span class="line"> arr=<span class="keyword">new</span> <span class="keyword">int</span>[maxSize];</span><br><span class="line"> front=<span class="number">0</span>;</span><br><span class="line"> rear=<span class="number">0</span>;</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>然后还是增加模块.首先判定队列是否为满</p>
<p>在这里判定队列为满得情况发生了变化,因为现在是环形列表,所以rear指针是不可以再用maxsize进行操控了.(因为如果再用maxsize唯一的结果就是在rear指针指到maxsize之后就不再移动了,那环形列表就不存在了)</p>
<p>所以判断得标准将会转换,什么时候为满呢.</p>
<figure class="highlight plain"><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">rear+1-front=maxsize</span><br><span class="line">但是并不完善,因为就像在之前说过的一样,在逻辑上是环形,在现实中却还是一个普通的数组.</span><br><span class="line">结果java中又恰恰不存在指针这个东西,所以rear和front虽然被称为指针,但是实际上确实两个int类型得数据,用作数组的下标.</span><br><span class="line">所以会出现这种情况就是front的标号大于rear的标号,那么如果用</span><br><span class="line">rear+1-front=maxsize的唯一结果恐怕就不再准确了.</span><br><span class="line">所以我们必须换一种安全的办法</span><br><span class="line">(rear+1)%maxsize=front</span><br></pre></td></tr></table></figure>
<p><img src="C:%5CUsers%5C22643%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20200310201543943.png" alt="image-20200310201543943"></p>
<p>emmm字有点丑,将就着看.</p>
<p>在这个图中模拟了其中一种情况,我们发现,rear在front之下会出现问题.</p>
<p>所以代码应该这么写</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></pre></td><td class="code"><pre><span class="line"><span class="comment">//因为队满会让整个队列都站上。所以理论上rear+1的得数应该是0也就是front(注意这里的rear是数组的下标,不是地址。所以说rear加上必须存在的1就得到了整个的数量。)</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isFull</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> (rear+<span class="number">1</span>)%maxSize==front;</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>那么回到增加模块的说法上来.</p>
<p>增加模块也产生了变化,</p>
<p>这里又要老生常谈一下,环形队列并不是指队列的主体数组被首位相接,从物理上连接成是环形,这是由数学完成的.</p>
<p>如何让rear越过maxsize大小之后重新出现在数组开头.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rear=(rear+1)%maxSize;</span><br></pre></td></tr></table></figure>
<p>![fullsizerender(7)](C:\Users\22643\Documents\Tencent Files\2264381489\FileRecv\MobileFile\fullsizerender(7).jpg)</p>
<p>如图,就是这样办到的.</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></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addQueue</span><span class="params">(<span class="keyword">int</span> a)</span></span>{</span><br><span class="line"> <span class="comment">//判断队列不为空</span></span><br><span class="line"> <span class="keyword">if</span>(isFull()){</span><br><span class="line"> System.out.println(<span class="string">"队列满,不能加入数据"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//直接将数据加入</span></span><br><span class="line"> arr[rear]=a;<span class="comment">//赋值</span></span><br><span class="line"> <span class="comment">//将rear后移,这里必须考虑取模</span></span><br><span class="line"> rear=(rear+<span class="number">1</span>)%maxSize;<span class="comment">//加入rear到了这个数组的最后一位,此时rear+1在对他跟maxsize取余就直接让rear等于数组开头的值了。</span></span><br><span class="line"> <span class="comment">//加入说arr数组长5,front在3号位置(下标为2),rear的下标为4的时候,rear+1是5对他%maxsize,得出的数是0,这就用逻辑的思想来形成了一个圆环,</span></span><br><span class="line"> <span class="comment">// 而非在地址上进行什么操作。</span></span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>所以这就做到了.</p>
<p>然后是空值</p>
<p>空值的变化不大,还是那一出</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></pre></td><td class="code"><pre><span class="line"><span class="comment">//判断队列是否为空</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">return</span> rear==front;</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>取值有一点小小的变化因为涉及到front指针的移动,所以也要考虑越过maxsize的情况</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></pre></td><td class="code"><pre><span class="line"> <span class="comment">//获取队列的值</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getQueue</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">if</span> (isEmpty()){</span><br><span class="line"> System.out.println(<span class="string">"栈空了!"</span>);</span><br><span class="line"><span class="comment">// return -1;这个不行,因为这样的话,只能返回-1</span></span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"队列空。"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//这里需要分析出front是指向队列的第一个元素</span></span><br><span class="line"> <span class="comment">//1.先把front对应的值保留在一个临时变量</span></span><br><span class="line"> <span class="comment">//2.front后移,考虑取模</span></span><br><span class="line"> <span class="comment">//3.将临时保存的变量返回</span></span><br><span class="line"> <span class="keyword">int</span> value=arr[front];</span><br><span class="line"> front=(front+<span class="number">1</span>)%maxSize;<span class="comment">//对于front指针也是一样的,要是加的数量超过了maxSize就要靠取模来返回之前曾经走过的位置了</span></span><br><span class="line"> <span class="keyword">return</span> value;</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>这里要解释一下这个front+1.</p>
<p>这个部分的主要原因是因为之前的设置,我们设置了一个不装任何数据的数组元素.所以无论是front还是rear都要加上这个去再去取余.</p>
<p>(你可以理解为累积木,这个就是一个方块,必须加上)</p>
<p>以上是主要产生变化的地方.</p>
<p>下面就是几个必要的功能</p>
<p>显示所有数据的功能</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></pre></td><td class="code"><pre><span class="line"><span class="comment">//显示队列的所有数据</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">showQueue</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">if</span> (isEmpty()){</span><br><span class="line"> System.out.println(<span class="string">"队列是空的。"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//思路:从front开始遍历,遍历多少个元素。</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = front; i <front+size() ; i++) {</span><br><span class="line"> <span class="comment">//i是从front开始的,不断地加一有可能让他超过maxSize,所以取模就让她回道头部了。</span></span><br><span class="line"> System.out.printf(<span class="string">"arr[%d]=%d\n"</span>,i%maxSize,arr[i%maxSize]); <span class="comment">//这个要学习一下。</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>显示队列有效个数的功能</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></pre></td><td class="code"><pre><span class="line"><span class="comment">//当前队列有效数据的个数</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">return</span> (rear+maxSize-front)%maxSize;<span class="comment">//说白了就是rear-front的绝对值的一种表示形式。</span></span><br><span class="line"> <span class="comment">//rear=1</span></span><br><span class="line"> <span class="comment">//front=0</span></span><br><span class="line"> <span class="comment">//maxSize=3</span></span><br><span class="line"></span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>显示队列头</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></pre></td><td class="code"><pre><span class="line"><span class="comment">//显示队列头数据,注意不是取出数据</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">headQueue</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">if</span> (isEmpty()){</span><br><span class="line"> System.out.println(<span class="string">"队列空的,没有数据"</span>);</span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"队列是空的"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> arr[front];<span class="comment">//因为队列头front永远指的是队列中的第一个数</span></span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>最后把完整代码放一下.</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><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> queue;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CircleArrayQueueDemo</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"> System.out.println(<span class="string">"环形队列"</span>);</span><br><span class="line"> <span class="comment">//环形队列</span></span><br><span class="line"> CircleArray arrayQueue = <span class="keyword">new</span> CircleArray(<span class="number">4</span>);<span class="comment">//设置3,其队列的最大数字是3</span></span><br><span class="line"> <span class="keyword">char</span> key = <span class="string">' '</span>;<span class="comment">//接受用户的输入</span></span><br><span class="line"> Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line"> <span class="keyword">boolean</span> loop = <span class="keyword">true</span>;</span><br><span class="line"> <span class="comment">//输出一个菜单</span></span><br><span class="line"> <span class="keyword">while</span> (loop) {</span><br><span class="line"> System.out.println(<span class="string">"s:显示队列"</span>);</span><br><span class="line"> System.out.println(<span class="string">"e:退出程序"</span>);</span><br><span class="line"> System.out.println(<span class="string">"a:添加数据到队列"</span>);</span><br><span class="line"> System.out.println(<span class="string">"g:从队列取出数据"</span>);</span><br><span class="line"> System.out.println(<span class="string">"h:查看队列头的数据"</span>);</span><br><span class="line"><span class="comment">// System.out.println("s:显示队列");</span></span><br><span class="line"> key = scanner.next().charAt(<span class="number">0</span>);</span><br><span class="line"> <span class="keyword">switch</span> (key) {</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'s'</span>:</span><br><span class="line"> arrayQueue.showQueue();</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'a'</span>:</span><br><span class="line"> System.out.println(<span class="string">"输出一个数"</span>);</span><br><span class="line"> <span class="keyword">int</span> value = scanner.nextInt();</span><br><span class="line"> arrayQueue.addQueue(value);</span><br><span class="line"> arrayQueue.showQueue();</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'g'</span>:<span class="comment">//取出数据</span></span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="keyword">int</span> res = arrayQueue.getQueue();</span><br><span class="line"> System.out.printf(<span class="string">"取出的数据是%d\n,"</span>, res);</span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> System.out.println(e.getMessage());</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'h'</span>:<span class="comment">//查看队列头的数据</span></span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> System.out.println(arrayQueue.headQueue());</span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> System.out.println(e.getMessage());</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'e'</span>:<span class="comment">//退出</span></span><br><span class="line"> scanner.close();</span><br><span class="line"> loop = <span class="keyword">false</span>;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">default</span>:</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"程序退出~~~时"</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">CircleArray</span></span>{</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> maxSize;<span class="comment">//表示数组的最大容量</span></span><br><span class="line"> <span class="comment">//front变量的含义做一个调整,front指向队列的第一个元素。也就是说arr【front】就是队列第一个元素,</span></span><br><span class="line"> <span class="comment">//初始值为0</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> front;<span class="comment">//队列头</span></span><br><span class="line"> <span class="comment">//rear变量的含义做一个调整,rear指向队列的最后一个元素的后一个位置。因为我希望空出一个空间作为约定。</span></span><br><span class="line"> <span class="comment">//一位有了这个空格就可以比较轻松地知道,空格前面的是rear后面的是front</span></span><br><span class="line"> <span class="comment">//初始值为0</span></span><br><span class="line"> <span class="comment">//不留这个位置(rear不指向队列最后一个元素后一个位置的话)的唯一结果就是rear和front会撞在一起,撞在一起的两个指针会导致front原先有的被覆盖掉。</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> rear;<span class="comment">//队列尾</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> arr[];<span class="comment">//该数据用于存放数据,模拟队列</span></span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">CircleArray</span><span class="params">(<span class="keyword">int</span> maxSize)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.maxSize = maxSize;</span><br><span class="line"><span class="comment">// this.maxSize = maxSize;</span></span><br><span class="line"> arr=<span class="keyword">new</span> <span class="keyword">int</span>[maxSize];</span><br><span class="line"> front=<span class="number">0</span>;</span><br><span class="line"> rear=<span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//判断队列是否满</span></span><br><span class="line"> <span class="comment">//因为队满会让整个队列都站上。所以理论上rear+1的得数应该是0也就是front(注意这里的rear是数组的下标,不是地址。所以说rear加上必须存在的1就得到了整个的数量。)</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isFull</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> (rear+<span class="number">1</span>)%maxSize==front;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//判断队列是否为空</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">return</span> rear==front;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//添加数据到队列</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addQueue</span><span class="params">(<span class="keyword">int</span> a)</span></span>{</span><br><span class="line"> <span class="comment">//判断队列不为空</span></span><br><span class="line"> <span class="keyword">if</span>(isFull()){</span><br><span class="line"> System.out.println(<span class="string">"队列满,不能加入数据"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//直接将数据加入</span></span><br><span class="line"> arr[rear]=a;<span class="comment">//赋值</span></span><br><span class="line"> <span class="comment">//将rear后移,这里必须考虑取模</span></span><br><span class="line"> rear=(rear+<span class="number">1</span>)%maxSize;<span class="comment">//加入rear到了这个数组的最后一位,此时rear+1在对他跟maxsize取余就直接让rear等于数组开头的值了。</span></span><br><span class="line"> <span class="comment">//加入说arr数组长5,front在3号位置(下标为2),rear的下标为4的时候,rear+1是5对他%maxsize,得出的数是0,这就用逻辑的思想来形成了一个圆环,</span></span><br><span class="line"> <span class="comment">// 而非在地址上进行什么操作。</span></span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//获取队列的值</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getQueue</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">if</span> (isEmpty()){</span><br><span class="line"> System.out.println(<span class="string">"栈空了!"</span>);</span><br><span class="line"><span class="comment">// return -1;这个不行,因为这样的话,只能返回-1</span></span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"队列空。"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//这里需要分析出front是指向队列的第一个元素</span></span><br><span class="line"> <span class="comment">//1.先把front对应的值保留在一个临时变量</span></span><br><span class="line"> <span class="comment">//2.front后移,考虑取模</span></span><br><span class="line"> <span class="comment">//3.将临时保存的变量返回</span></span><br><span class="line"> <span class="keyword">int</span> value=arr[front];</span><br><span class="line"> front=(front+<span class="number">1</span>)%maxSize;<span class="comment">//对于front指针也是一样的,要是加的数量超过了maxSize就要靠取模来返回之前曾经走过的位置了</span></span><br><span class="line"> <span class="keyword">return</span> value;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//显示队列的所有数据</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">showQueue</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">if</span> (isEmpty()){</span><br><span class="line"> System.out.println(<span class="string">"队列是空的。"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//思路:从front开始遍历,遍历多少个元素。</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = front; i <front+size() ; i++) {</span><br><span class="line"> <span class="comment">//i是从front开始的,不断地加一有可能让他超过maxSize,所以取模就让她回道头部了。</span></span><br><span class="line"> System.out.printf(<span class="string">"arr[%d]=%d\n"</span>,i%maxSize,arr[i%maxSize]); <span class="comment">//这个要学习一下。</span></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="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">return</span> (rear+maxSize-front)%maxSize;<span class="comment">//说白了就是rear-front的绝对值的一种表示形式。</span></span><br><span class="line"> <span class="comment">//rear=1</span></span><br><span class="line"> <span class="comment">//front=0</span></span><br><span class="line"> <span class="comment">//maxSize=3</span></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="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">headQueue</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">if</span> (isEmpty()){</span><br><span class="line"> System.out.println(<span class="string">"队列空的,没有数据"</span>);</span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"队列是空的"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> arr[front];<span class="comment">//因为队列头front永远指的是队列中的第一个数</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://yoursite.com/2020/03/10/%E7%A2%8E%E5%98%B4%E7%AE%97%E6%B3%95-%E5%BE%AA%E7%8E%AF%E9%98%9F%E5%88%97/" data-id="ck7lwa9hp000028v5erakbr56" class="article-share-link">Share</a>
</footer>
</div>
</article>
<article id="post-leetcode必要技巧-动态规划-一" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2020/03/09/leetcode%E5%BF%85%E8%A6%81%E6%8A%80%E5%B7%A7-%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92-%E4%B8%80/" class="article-date">
<time datetime="2020-03-09T05:52:36.000Z" itemprop="datePublished">2020-03-09</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2020/03/09/leetcode%E5%BF%85%E8%A6%81%E6%8A%80%E5%B7%A7-%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92-%E4%B8%80/">leetcode必要技巧--动态规划(一)</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>首先我们要搞清楚什么是动态规划</p>
<blockquote>
<p> 动态规划是运筹学中用于求解决策过程中的最优化数学方法。当然,我们在这里关注的是作为一种算法设计技术,作为一种使用多阶段决策过程最优的通用方法。</p>
</blockquote>
<p>当然这个很难理解,但是按照本人的理解.<br>实际上就是一个种类的问题.<br>在这个问题中,我们只需要列出一个方程,就能用递归的方法解决大部分呢问题.</p>
<p>(突然看到一个不错的消息)</p>
<blockquote>
<p>动态规划问题的一般形式就是求最值。动态规划其实是运筹学的一种最优化方法,只不过在计算机问题上应用比较多,比如说让你求最长递增子序列呀,最小编辑距离呀等等。</p>
</blockquote>
<blockquote>
<p>既然是要求最值,核心问题是什么呢?求解动态规划的核心问题是穷举。因为要求最值,肯定要把所有可行的答案穷举出来,然后在其中找最值呗。</p>
</blockquote>
<p>作者:labuladong<br>链接:<a href="https://leetcode-cn.com/problems/coin-change/solution/dong-tai-gui-hua-tao-lu-xiang-jie-by-wei-lai-bu-ke/" target="_blank" rel="noopener">https://leetcode-cn.com/problems/coin-change/solution/dong-tai-gui-hua-tao-lu-xiang-jie-by-wei-lai-bu-ke/</a><br>来源:力扣(LeetCode)</p>
<p>总结一下,就是对整个内容进行穷举操作,恰好这种穷举操作有着大量的可重复性,可以利用递归来解决.</p>
<p>个人认为动态规划有俩个核心</p>
<ol>
<li>递归</li>
<li>冗余处理</li>
</ol>
<p>我们就用leetcode中的零钱兑换.</p>
<p>给定不同面额的硬币 coins 和一个总金额 amount。</p>
<p>编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。</p>
<p>示例 1:</p>
<p>输入: coins = [1, 2, 5], amount = 11</p>
<p>输出: 3</p>
<p>解释: 11 = 5 + 5 + 1</p>
<p>示例 2:</p>
<p>输入: coins = [2], amount = 3</p>
<p>输出: -1</p>
<p>说明:</p>
<p>你可以认为每种硬币的数量是无限的。</p>
<p>这里还要引申一下,一开始我想到的算法使用amount(总金额)%5(coins中最大的硬币)得出余数(在这里假设为remainder).再用remainder(上面得出的数)%2(coins中第二大的硬币面值)不断地循环下去.</p>
<p>据说这叫贪心算法.</p>
<p>这个无疑是错误的.比如[1,5,11] 凑15</p>
<p>那么按照这个贪心算法的唯一结果是 一个11和4个1</p>
<p>但是正确的答案是三个5</p>
<p>这就是这种算法的局限性</p>
<p>说回正题</p>
<p>要想解决问题,我们就必须分析这个问题.</p>
<p>我们就必须化出解决这个问题的穷举图像</p>
<p><img src="https://pic.leetcode-cn.com/5033d5448653c7a31393fbaa365b1f638933d5e467315e53641598e895a2de93-image.png" alt="image.png"></p>
<p>这里说一下,这个也是从leetcode上拿的.</p>
<p>我们看这个图像,最上面有一行小字,翻译如下:</p>
<p>emmm英语不好,挑重点说吧</p>
<p>[1,2,3]种面值的币子<br>凑6<br>我们会发现如果要遍历这个树,就会发现每一个节点执行的操作实际上都是大同小异的,而且面对的数据也都是大同小异的(都是那三个数,1,2,3都不会变化)</p>
<p>所以也正是因为如此.我们可以采用.递归的方式.</p>
<p>在这里就要说到处理这种方式的诀窍了.如果按照递归的思路一直深究下去,那么唯一的结果就是蒙圈.</p>
<p>毕竟太深入,太复杂了.</p>
<p>可是我们要意识到,其实这个算法再每一个节点上都是相同的,都可以用一样的算法.于是我们只要列出第一层和第二层之间的关系就可以用这个关系为突破口,想办法用递归实现这个问题.</p>
<blockquote>
<p>F(S)=F(S-C)+1;</p>
</blockquote>
<blockquote>
<p>F(S):所要求的目标->最少的硬币个数</p>
</blockquote>
<blockquote>
<p>S:总金额</p>
</blockquote>
<blockquote>
<p>c:(所用)最后一个硬币的面值</p>
</blockquote>
<pre><code>eg:
在最顶层 6节点开始有以下三种情况
F(6)=F(6-1)+1=F(5)+1
F(6)=F(6-2)+1=F(4)+1
F(6)=F(6-3)+1=F(3)+1
我们要知道其中F(5)因为也是一个函数所以,也会向下裂变化为
F(5)=F(5-1)+1
F(5)=F(5-2)+1
F(5)=F(5-3)+1
然后面还可以裂变,直到s-c=0或者s-c<0为止(这就是我们迭代的停止条件,s-c>0)
这就是用迭代形成了一个遍历的过程.相比while这种方式,方便了很多倍.[这个遍历的实质上得出的是每一条可能出现的路径所要消耗的硬币数量]
但是我们遍历的目的是为了找出一条最短的路径,使用最少的硬币数量,所以我们需要对其进行比较.
也就是在
--A代码块
F(6)=F(6-1)+1=F(5)+1
F(6)=F(6-2)+1=F(4)+1
F(6)=F(6-3)+1=F(3)+1
---
这三个可能中选择最小的一个.
F(S)是最少硬币的个数.
而如果要求出这三个我们就要知道F(5,4,3)[简写]中谁最小.于是这就要到了,我们要知道
F(5)=F(5-1)+1
F(5)=F(5-2)+1
F(5)=F(5-3)+1
种谁最小的问题.
完全是一摸一样的,这就是递归最好的温床.</code></pre><figure class="highlight javascript"><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">//coins 硬币面值(是个数组,在迭代过程中从不变化)</span></span><br><span class="line"> <span class="comment">//rem 总值就是S</span></span><br><span class="line"> <span class="comment">//count[] 记录的信息(这个是为了防止重复计算的问题)</span></span><br><span class="line"><span class="comment">//这个for就是找出A代码块中那个线路最小的步骤,存在min中</span></span><br><span class="line"> <span class="keyword">for</span> (int coin : coins) {</span><br><span class="line"> int res = coinChange(coins, rem - coin, count);</span><br><span class="line"> </span><br><span class="line"> <span class="comment">//F(S)=F(S-C)+1;</span></span><br><span class="line"> <span class="comment">//rem-coin对应的就是S-C</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (res >= <span class="number">0</span> && res < min)</span><br><span class="line"> <span class="comment">//min的结果实际上就是最小的res+1就是F(S).而res实际上是</span></span><br><span class="line"> min = <span class="number">1</span> + res;</span><br><span class="line"> <span class="comment">//F(S)=F(S-C)+1;</span></span><br><span class="line"> <span class="comment">//res=function(coins,S-C,count);</span></span><br><span class="line"> <span class="comment">//min=function(coins,S-C,count)+1;</span></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">作者:LeetCode-Solution</span><br><span class="line">链接:https:<span class="comment">//leetcode-cn.com/problems/coin-change/solution/322-ling-qian-dui-huan-by-leetcode-solution/</span></span><br><span class="line">来源:力扣(LeetCode)</span><br><span class="line">著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。</span><br></pre></td></tr></table></figure>
<p>这里贴一下完整的代码</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></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">public</span> <span class="keyword">int</span> <span class="title">coinChange</span><span class="params">(<span class="keyword">int</span>[] coins, <span class="keyword">int</span> amount)</span> </span>{</span><br><span class="line"> <span class="comment">// if(amount<1)</span></span><br><span class="line"> <span class="keyword">return</span> coinChange(coins,amount,<span class="keyword">new</span> <span class="keyword">int</span>[amount]); </span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> 在这里我们会发现我们的要想递归就只能拿这个函数制作递归,但是s-c就无法实现了,所以我们只能重载一个.让这个最终返回内容.</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"> <span class="comment">//coins 硬币面值</span></span><br><span class="line"> <span class="comment">//rem 总值</span></span><br><span class="line"> <span class="comment">//count[] 记录的信息</span></span><br><span class="line"> <span class="function"><span class="keyword">private</span> <span class="keyword">int</span> <span class="title">coinChange</span><span class="params">(<span class="keyword">int</span> [] coins,<span class="keyword">int</span> rem,<span class="keyword">int</span> [] count)</span></span>{</span><br><span class="line"> <span class="keyword">if</span>(rem==<span class="number">0</span>) <span class="keyword">return</span> <span class="number">0</span>;<span class="comment">//解决输入不合理</span></span><br><span class="line"> <span class="keyword">if</span>(rem<<span class="number">0</span>)<span class="keyword">return</span> -<span class="number">1</span>;<span class="comment">//解决输入合理</span></span><br><span class="line"> <span class="keyword">if</span>(count[rem-<span class="number">1</span>]!=<span class="number">0</span>){<span class="keyword">return</span> count[rem-<span class="number">1</span>];}<span class="comment">//如果以前已经有了记录,就返回被记录的数值</span></span><br><span class="line"> <span class="keyword">int</span> min=Integer.MAX_VALUE;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<coins.length;i++){</span><br><span class="line"> <span class="keyword">int</span> res=coinChange(coins,rem-coins[i],count);<span class="comment">//递归,如果以前有了相关的内容会直接反回</span></span><br><span class="line"> <span class="comment">//如果是最小的,那么就加1,作为最少币数</span></span><br><span class="line"> <span class="keyword">if</span>(res>=<span class="number">0</span>&&min>res){</span><br><span class="line"> min=res+<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//min有没有被改变,要是改变了就不会是原值,自然回min</span></span><br><span class="line"> count[rem - <span class="number">1</span>] = (min == Integer.MAX_VALUE) ? -<span class="number">1</span> : min;</span><br><span class="line"> <span class="keyword">return</span> count[rem - <span class="number">1</span>];</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://yoursite.com/2020/03/09/leetcode%E5%BF%85%E8%A6%81%E6%8A%80%E5%B7%A7-%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92-%E4%B8%80/" data-id="ck7k1v3gv0000h0v5cd0q1wnj" class="article-share-link">Share</a>
</footer>
</div>
</article>
<article id="post-第一篇博客" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2020/02/29/%E7%AC%AC%E4%B8%80%E7%AF%87%E5%8D%9A%E5%AE%A2/" class="article-date">
<time datetime="2020-02-29T08:51:16.585Z" itemprop="datePublished">2020-02-29</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2020/02/29/%E7%AC%AC%E4%B8%80%E7%AF%87%E5%8D%9A%E5%AE%A2/">第一篇博客</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>#js新手村出村之路–基础知识<br>在这里默认你已经粗粗的自学过了一遍js知识。(<em>也许是在昏昏欲睡的课堂上听了两分钟,也许是跟着b站上的视频打了一遍。who cares</em>)<br>在下面的内容中我将整理一些在平常的学习中经常容易忘记的部分。并且尽自己的可能,解释他们。</p>
<h2 id="基础知识"><a href="#基础知识" class="headerlink" title="基础知识"></a>基础知识</h2><h3 id="DOM是什么?"><a href="#DOM是什么?" class="headerlink" title="DOM是什么?"></a>DOM是什么?</h3><blockquote>
<p>文档对象模型(英语:Document Object Model,缩写DOM),是W3C组织推荐的处理可扩展置标>语言的标准编程接口。<br>(<em>简单一点呢</em>)<br>一种对文档内容进行抽象或者概念性的方法。</p>
</blockquote>
<p>(<em>糟糕透顶了对不对!这是在说什么</em>)<br>在这里可以做一个类比.<br>假如说我们看见一个树,我会说”看,这是大树!”每个周围听见我说的话的人都知道我说的是什么,知道我说的”大树”就是指我眼前的这个东西.<br>具体到编程中,就是我想加入一个图片,我必须用一个<code><images></code>标签来进行加入,这个就是抽象或概念性的方法.(浏览器看见<code><images></code>标签就知道这是图片)<br>下面具体的讲解一下DOM中的几个字母都是什么意思.<br><strong>文档:DOM中的D</strong><br>document文档<br>当创建了一个网页并把它加载到web浏览器中的时候,DOM就已经出现了,他把你的网页文档转换成一个文档对像.<br><strong>对象:DOM中的O</strong><br>object对象<br> 对象有三种类型<br> ? 用户定义对象—-就是用户们自己定义的<br> ? 内建对象—-JavaScript中自己带的<br> ? 宿主对象——浏览器中提供的。</p>
<h6 id="Window对象对应着浏览器本身,这个对象-的属性和方法被称之为BOM"><a href="#Window对象对应着浏览器本身,这个对象-的属性和方法被称之为BOM" class="headerlink" title="Window对象对应着浏览器本身,这个对象 的属性和方法被称之为BOM"></a>Window对象对应着浏览器本身,这个对象 的属性和方法被称之为BOM</h6><p><strong>模型:DOM中的M</strong><br>Model<br>DOM会把文档表示成一个树,并使用parent,child等来描述他们之间的关系。<br><strong>找一个DOM树的图像</strong><br><img src="https://i.loli.net/2020/02/28/AXuHbIs2CDxVtwh.png" alt="dom.png"><br>在html之后表示了文档的开始,因为这个文档中的所有元素都包含在这个html中。<br>1.HTML元素就是根。<br>2.接下来会看到有<code><head></code>and<code><body></code>两个分支。<br>3.他们相互不包含。……<br>4.这个被称为节点树。<br>你会看见这里面有以下几种节点<br>1.元素节点<br>2.文本节点<br>3.属性节点<br>例如<code><p title="a gentle。。。。">DON</p></code><br>title就是一个属性节点,<code><p></p></code>是一个元素节点.</p>
<h5 id="这里要引申一下"><a href="#这里要引申一下" class="headerlink" title="这里要引申一下."></a>这里要引申一下.</h5><p>在CSS中采用的也是相同的概念。<br>继承是css技术中的功能。类似于DOM,css也是把文档视为一棵树。<br>节点树上的各个元素将继承其父元素的样式属性。<br>假设<code><body></code>定义了一些颜色和字体,包含在其元素中的所有元素将自动获取那些样式。<br>这其实就是树的一种体现。<br>(<em>但是新手在初期能用到的也就是一下两种方法。</em>)</p>
<figure class="highlight javascript"><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"><span class="keyword">var</span> aLi=<span class="built_in">document</span>.getElementsByTagName(<span class="string">'li'</span>);<span class="comment">//可以捕获标签名</span></span><br><span class="line"><span class="keyword">var</span> ali1=<span class="built_in">document</span>.getElementById();<span class="comment">//捕获id属性</span></span><br></pre></td></tr></table></figure>
<p>(<em>好了这也就是我所知道得了,我会在将来总结更多的知识</em>)</p>
</div>
<footer class="article-footer">
<a data-url="http://yoursite.com/2020/02/29/%E7%AC%AC%E4%B8%80%E7%AF%87%E5%8D%9A%E5%AE%A2/" data-id="ck77d905a0000fgv52snecw2g" class="article-share-link">Share</a>
</footer>
</div>
</article>
<article id="post-第一篇文章-1" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2020/02/29/%E7%AC%AC%E4%B8%80%E7%AF%87%E6%96%87%E7%AB%A0-1/" class="article-date">
<time datetime="2020-02-29T08:33:09.000Z" itemprop="datePublished">2020-02-29</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2020/02/29/%E7%AC%AC%E4%B8%80%E7%AF%87%E6%96%87%E7%AB%A0-1/">JavaScript进阶入门(二)</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h1 id="JavaScript进阶入门(二)"><a href="#JavaScript进阶入门(二)" class="headerlink" title="JavaScript进阶入门(二)"></a>JavaScript进阶入门(二)</h1><h3 id="转换为数字"><a href="#转换为数字" class="headerlink" title="转换为数字"></a>转换为数字</h3><h4 id="使用parseInt()"><a href="#使用parseInt()" class="headerlink" title="使用parseInt()"></a>使用parseInt()</h4><p>parseInt函数会先查看位置0处的字符,如果该位置不是有效数字,则将返回NaN,如果0处的字符是数字,则将查看位置1处的字符,并进行同样的测试,以此类推在整个验证过程中,直到发现非数字字符为止,此时paresInt()函数将前面分析合法的数字字符转化为数值,并返回。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">alert(<span class="built_in">parseInt</span>(<span class="string">"123abc"</span>));<span class="comment">//返回数字123</span></span><br><span class="line">alert(<span class="built_in">parseInt</span>(<span class="string">"1.73"</span>));<span class="comment">//返回数字1</span></span><br><span class="line">alert(<span class="built_in">parseInt</span>(<span class="string">".123"</span>));<span class="comment">//返回值NaN</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><em>浮点数中的点号对于parseInt()函数来说属于非法字符,因此不会装换它,并返回。</em></p>
</blockquote>
<h4 id="使用parseFloat函数"><a href="#使用parseFloat函数" class="headerlink" title="使用parseFloat函数"></a>使用parseFloat函数</h4><p>parseFloat函数与parseInt函数用法基本相同,但是它能够识别第一个出现的小数点号,而第二个小数点号则被视为非法的。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">alert(<span class="built_in">parseFloat</span>(<span class="string">"1.234.11"</span>));<span class="comment">//返回数值1.234</span></span><br></pre></td></tr></table></figure>
<h4 id="使用乘号运算符"><a href="#使用乘号运算符" class="headerlink" title="使用乘号运算符"></a>使用乘号运算符</h4><p>加号运算符不仅能够执行数值求和运算,还可以吧字符串连接来。由于JavaScript处理字符串连接操作的优先级要高于数字求和运算。因此,当数字字符串与数值使用加号连接时,将优先执行连接操作,而不是求和运算。<br>由此可以看出,使用*号可以利用JavaScript的自动转换完成这个目标。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">var</span> b=<span class="string">"1"</span>;</span><br><span class="line">alert(a+(b*<span class="number">1</span>));</span><br></pre></td></tr></table></figure>
<h3 id="转换为布尔值"><a href="#转换为布尔值" class="headerlink" title="转换为布尔值"></a>转换为布尔值</h3><p>任何值如果在前面加上一个逻辑非运算符,JavaScript都会把这个表达式看作是逻辑运算,执行运算时,先把值转换为布尔值,然后再执行逻辑非运算。</p>
<figure class="highlight javascript"><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="keyword">var</span> a=!<span class="number">0</span>;<span class="comment">//true</span></span><br><span class="line"><span class="keyword">var</span> b=!<span class="number">1</span>;<span class="comment">//false</span></span><br><span class="line"><span class="keyword">var</span> c=!<span class="literal">NaN</span>;<span class="comment">//true</span></span><br><span class="line"><span class="keyword">var</span> d=!<span class="literal">null</span>;<span class="comment">//true</span></span><br><span class="line"><span class="keyword">var</span> e=!<span class="literal">undefined</span>;<span class="comment">//true</span></span><br><span class="line"><span class="keyword">var</span> f=![];<span class="comment">//false</span></span><br><span class="line"><span class="keyword">var</span> g=!{};<span class="comment">//false</span></span><br></pre></td></tr></table></figure>
<p>使用Boolean()构造函数转换</p>
<figure class="highlight javascript"><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="keyword">var</span> a=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">var</span> b=<span class="number">1</span>;</span><br><span class="line">a=<span class="keyword">new</span> <span class="built_in">Boolean</span>(a);<span class="comment">//false</span></span><br><span class="line">b=<span class="keyword">new</span> <span class="built_in">Boolean</span>(b);<span class="comment">//true</span></span><br></pre></td></tr></table></figure>
<h3 id="把对象转化为值"><a href="#把对象转化为值" class="headerlink" title="把对象转化为值"></a>把对象转化为值</h3><h4 id="对象在逻辑运算环境中的转换"><a href="#对象在逻辑运算环境中的转换" class="headerlink" title="对象在逻辑运算环境中的转换"></a>对象在逻辑运算环境中的转换</h4><p>对象在逻辑运算环境中,则对象被转换为true。这包括所有类型的对象,即使是值为false的包装对象也为true、</p>
<figure class="highlight javascript"><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></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a=<span class="keyword">new</span> <span class="built_in">Boolean</span>(<span class="literal">false</span>);</span><br><span class="line"><span class="keyword">var</span> b=<span class="keyword">new</span> <span class="built_in">Number</span>(<span class="number">0</span>);</span><br><span class="line"><span class="keyword">var</span> c=<span class="keyword">new</span> <span class="built_in">String</span>(<span class="string">""</span>);</span><br><span class="line">a && alert(a);<span class="comment">//a转换为布尔值true,但是她提示的字符串转换至为false</span></span><br><span class="line">b && alert(b);<span class="comment">//b转换为布尔值true,但是她提示的字符串转换至为"0"</span></span><br><span class="line">c && alert(c);<span class="comment">//c转换为布尔值true,但是她提示的字符串转换至为""</span></span><br></pre></td></tr></table></figure>
<h4 id="数组在数值运算环境中的转换"><a href="#数组在数值运算环境中的转换" class="headerlink" title="数组在数值运算环境中的转换"></a>数组在数值运算环境中的转换</h4><p>当数组被用在数值运算环境中是,数组将根据包含的元素来决定转换的值。<br>1)如果是空数组,则被转为数值0.当数组为空的时候,JavaScript将调用toString()方法把数组换位空字符串,然后经空字符串强制转换为0.<br>2)如果数组仅包含一个数字元素,则被转换为该数字的数值。</p>
<figure class="highlight javascript"><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="keyword">var</span> a=[<span class="number">5</span>];</span><br><span class="line">a=a*<span class="number">1</span>;<span class="comment">//投放到数值运算环境中</span></span><br><span class="line">alert(a);<span class="comment">//返回数值5</span></span><br><span class="line">alert(<span class="keyword">typeof</span> a);<span class="comment">//返回数值类型numbmer</span></span><br></pre></td></tr></table></figure>
<p>3)如果数组包含多个元素,或仅包含一个非数字元素,则返回NaN</p>
<h4 id="对象在模糊环境"><a href="#对象在模糊环境" class="headerlink" title="对象在模糊环境"></a>对象在模糊环境</h4><p>1)当对象与数值进行加号运算是,则会尝试把对象转化为数值,然后参与求和运算,如果不能够转化为有效数值,则执行字符串连接操作。</p>
<figure class="highlight javascript"><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></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a=<span class="keyword">new</span> <span class="built_in">String</span>(<span class="string">"a"</span>);</span><br><span class="line"><span class="keyword">var</span> b=<span class="keyword">new</span> <span class="built_in">Boolean</span>(<span class="literal">true</span>);</span><br><span class="line">a=a+<span class="number">0</span>;</span><br><span class="line">b=b+<span class="number">0</span>;</span><br><span class="line">alert(a);<span class="comment">//返回字符串"a0"</span></span><br><span class="line">alert(b);<span class="comment">//返回数值1</span></span><br></pre></td></tr></table></figure>
<p>2)当对象与字符串进行加号运算时,则直接转化为字符串,进行连接操作<br>3)当对象与数值进行比较运算时,则会尝试吧对象转换为数值。</p>
<h2 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h2><h3 id="优先级"><a href="#优先级" class="headerlink" title="优先级"></a>优先级</h3><p>优先级:表示运算符参与的先后顺序,顺序越大,运算优先级就越高。</p>
<h4 id="15"><a href="#15" class="headerlink" title="15"></a>15</h4><p>.(点号)<br>[]中括号<br>()小括号<br>new</p>
<h4 id="14"><a href="#14" class="headerlink" title="14"></a>14</h4><p>++(连加)<br>–(连减)<br>+(加号)从右到左<br>*(乘法运算)</p>
<hr>
<h3 id="NaN"><a href="#NaN" class="headerlink" title="NaN"></a>NaN</h3><p>NAN和其他的任何值相加都是NaN</p>
<p>减法运算中如果有一个运算数不是数字则返回NaN,如果数字为字符串,则会把它转换为数值之后在进行运算。</p>
<h3 id="逻辑运算符"><a href="#逻辑运算符" class="headerlink" title="逻辑运算符"></a>逻辑运算符</h3><p>逻辑与运算符&&的逻辑解析:</p>
<p>首先,计算第一个运算数,即左侧表达式,如果左侧的表达式的计算值可以被转换为false,那么就会结束计算,直接返回第一个运算数的值。<br>由于这种特性,在程序中也会利用它设计简易运算</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(n==<span class="number">1</span>) && alert(<span class="number">1</span>);</span><br></pre></td></tr></table></figure>
<p>逻辑或运算符||的逻辑解析:</p>
<p>计算第1个运算数。如果左侧的表达式的计算值可以被转换为true,那么就直接返回第一个运算数的值,忽略第2个运算数(既不执行)。</p>
<p><strong>&&的优先级高于||</strong></p>
<p>逻辑非运算符!是一元运算符,直接放在运算数之前,将对运算数执行布尔取反操作,并返回<strong>布尔值</strong></p>
<h1 id="等值检测"><a href="#等值检测" class="headerlink" title=" 等值检测"></a> <strong>等值检测</strong></h1><p>==(相等) 比较两个运算数的返回值,看是否相等</p>
<p>!=(不相等) 比较两个运算数的返回值,看是否不相等</p>
<p>===(全等) 比较两个运算数的返回值,看是否相等,同时检测他们的数据类型是否相等</p>
<p>!==(不全等) 比较两个运算数的返回值,看是否不相等,同时检测它们的数据类型是否不相等。</p>
<p><strong>相等运算的比较规则</strong></p>
<p>如果运算数是布尔值,在比较之前转化为数值。</p>
<p>如果一个运算数是字符串,另一个运算数是数字,在比较之前先尝试把字符串转化为数字。</p>
<p>如果一个运算数是数字,另一个运算数是对象,在比较前先将对象转化为数字。</p>
<p>如果一个运算数是字符串,另一个运算数的对象,在比较之前先尝试把对象转换为字符串。</p>
<p>如果两个运算数都是对象,那么比较他们的引用值。</p>
<p><strong>对象操作运算符</strong></p>
<p>new 运算符可以根据构造函数创建一个新的对象,并初始化这个对象</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> <span class="keyword">constructor</span>(arguments)</span><br></pre></td></tr></table></figure>
<p>constructor必须是一个构造函数表达式,其后面应该是利用小括号包含的参数列表,参数可有可无,参数之间通过逗号进行分割。</p>
<figure class="highlight javascript"><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"><span class="keyword">var</span> a=<span class="keyword">new</span> <span class="built_in">Array</span>();</span><br><span class="line"><span class="keyword">var</span> a=<span class="keyword">new</span> <span class="built_in">Array</span>(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>);</span><br></pre></td></tr></table></figure>
<p>new运算符被执行的时候,首先会创建一个新对象接着new运算符调用指定的构造函数,这里是Array数组的构造函数。</p>
<p>以下代码可以自定义类,并使用它创建新的对象</p>
<figure class="highlight javascript"><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></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="keyword">this</span>.x=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">this</span>.y=<span class="number">2</span>;</span><br><span class="line">};</span><br><span class="line"><span class="keyword">var</span> b=<span class="keyword">new</span> a; <span class="comment">//创建自定义类a的对象实例</span></span><br><span class="line">alert(b.x); <span class="comment">//返回1,调用对象的成员</span></span><br></pre></td></tr></table></figure>
<p>对于自定义类,只能通过new运算来进行实例化</p>
<p>(这个没必要太懂,就是知道JavaScript的类实际上是一个函数,调用这个函数,就自动造成了对象。之后会讲)</p>
<p><strong>delete运算符</strong></p>
<p>delete运算符能够删除指定对象的属性,数组元素或变量</p>
<figure class="highlight javascript"><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="keyword">var</span> a={</span><br><span class="line"> x:<span class="number">1</span>;</span><br><span class="line"> y:<span class="number">2</span>;</span><br><span class="line">};</span><br><span class="line">alert(a.x);<span class="comment">//返回1,调用对象成员</span></span><br><span class="line"><span class="keyword">delete</span> a.x;<span class="comment">//删除对象成员x</span></span><br><span class="line">alert(a.x);<span class="comment">//返回undefined,没有找到该对象成员</span></span><br></pre></td></tr></table></figure>
<p>不是所有的对象成员或变量都可以被删除,某些内置对象的预定义和客户端对象成员,以及使用var语句生命的变量都是不允许删除的。</p>
<p>如果删除不存在的对象成员,或者非对象成员,数组元素,变量时,他会返回true,所以使用delete运算时,应该注意这个问题,防止和成功删除发生混淆。</p>
<figure class="highlight javascript"><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></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a={</span><br><span class="line"> x:<span class="number">1</span>;</span><br><span class="line"> y:<span class="number">2</span>;</span><br><span class="line">};</span><br><span class="line">alert(<span class="keyword">delete</span> a); <span class="comment">//返回false,说明不允许删除</span></span><br><span class="line">alert(<span class="keyword">delete</span> a.z)<span class="comment">//返回true,说明不存在该属性</span></span><br><span class="line">alert(<span class="keyword">delete</span> <span class="built_in">Object</span>)<span class="comment">//返回true,说明删除的不是成员,元素或变量</span></span><br><span class="line">alert(<span class="keyword">delete</span> b)<span class="comment">//返回true,说明不存在该变量。</span></span><br></pre></td></tr></table></figure>
<p><strong>中括号和点括号运算符</strong></p>
<p>中括号和点号都属于存取运算符,用于访问对象后数组。使用中括号运算符([])可以存取数组元素值。</p>
<p>例子:</p>
<figure class="highlight javascript"><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="keyword">var</span> a={</span><br><span class="line"> x:<span class="number">1</span>;</span><br><span class="line"> y:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="number">2</span>;</span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line">alert(a[<span class="string">"x"</span>]);<span class="comment">//返回1,读取属性x的值。</span></span><br></pre></td></tr></table></figure>
<p>点号运算符(.)可以存取对象的属性值,它比中括号灵活方便,因为点号运算符右侧可以直接指定属性的标识符,而不是属性名称的字符串或变量。</p>
<p>对于中括号预算福可以通过变量或字符串表达式来传递特定值</p>
<figure class="highlight javascript"><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></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> b=<span class="string">"x"</span>;</span><br><span class="line"><span class="keyword">var</span> a={</span><br><span class="line"> x:<span class="number">1</span>;</span><br><span class="line">};</span><br><span class="line">alert(a[b]);</span><br><span class="line">alert(a.b);</span><br></pre></td></tr></table></figure>
<p>在中括号中的字符串形状的数字会被转化成数字来进行运算。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a=[<span class="string">"x"</span>,<span class="literal">true</span>,{}];</span><br><span class="line">alert(a[<span class="number">1</span>]);</span><br><span class="line">alert(a[<span class="string">"1"</span>]);</span><br></pre></td></tr></table></figure>
<p><strong>小括号运算符</strong></p>
<p>小括号是一个特殊的运算符,他没有固定数目的运算数。其中第一个运算数必须是一个函数名或者引用函数的表达式。</p>
<p>f(a,b,c);</p>
<figure class="highlight javascript"><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></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">a</span>(<span class="params"></span>)</span>{</span><br><span class="line"> alert(<span class="string">"Hello world"</span>);</span><br><span class="line">}</span><br><span class="line">a;</span><br><span class="line">a();</span><br><span class="line">a(<span class="number">1</span>,<span class="string">"string"</span>,{},<span class="literal">true</span>);</span><br></pre></td></tr></table></figure>
<p>小括号运算符在执行的时候是这样的,先对没个运算数进行计算,然后调用第一个运算数所指的函数,同时把余下的运算数传递给函数作为它的参数。</p>
<p><strong>其他运算符</strong></p>
<p>条件运算符</p>
<p>条件运算符是JavaScript唯一的三元运算符,语法形式如下:</p>
<p>a?x:y</p>
<p>a运算数必须是一个<strong>布尔值的表达式</strong>,即返回值必须是一个布尔值,一般使用比较表达式来表示。x和y是任意类型的值。如果运算数a返回值为<strong>true</strong>时,将<strong>执行x运算</strong>,并返回该表达式的值。如果运算数a返回值<strong>false</strong>时,将<strong>执行y运算数</strong>,并返回该表达式的值。</p>
<p>逗号运算符</p>
<p>逗号运算符是一个二元运算符,他能够<strong>执行运算符左侧的运算数</strong>,然后在<strong>执行右侧的运算数</strong>,最后仅把右侧运算数的值作为结果返回。</p>
<p>可以用来连续声明多个变量并赋值</p>
<figure class="highlight javascript"><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></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a=<span class="number">1</span>,b=<span class="number">2</span>,c=<span class="number">3</span>,d=<span class="number">4</span>;</span><br><span class="line"><span class="comment">//等于</span></span><br><span class="line"><span class="keyword">var</span> a=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">var</span> b=<span class="number">2</span>;</span><br><span class="line"><span class="keyword">var</span> c=<span class="number">3</span>;</span><br><span class="line"><span class="keyword">var</span> d=<span class="number">4</span>;</span><br></pre></td></tr></table></figure>
</div>
<footer class="article-footer">
<a data-url="http://yoursite.com/2020/02/29/%E7%AC%AC%E4%B8%80%E7%AF%87%E6%96%87%E7%AB%A0-1/" data-id="ck77cma0l0003s8v53qip1d1a" class="article-share-link">Share</a>
</footer>
</div>
</article>
<article id="post-入门进阶" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2020/01/14/%E5%85%A5%E9%97%A8%E8%BF%9B%E9%98%B6/" class="article-date">
<time datetime="2020-01-14T02:08:22.000Z" itemprop="datePublished">2020-01-14</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2020/01/14/%E5%85%A5%E9%97%A8%E8%BF%9B%E9%98%B6/">javascript入门进阶</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>#javascript 入门进阶</p>
<p>这里主要讲解一下在入门阶段很难注意的一些知识点,不一定有用。但是会了总比不会强。<br><strong>1.HTML为<code><script></code>标签准备的6个属性:</strong><br>-async:可选。表示应该立刻下载脚本,但不应妨碍页面中的其他操作。(其他的下载可以等待)<br>-charset:可选。表示通过src指定的代码的字符集。<br>-defer:可选。表示脚本可以延迟到文档被解析和显示之后在执行。该版本值对外部JavaScript有效果<br>-src:可选。执行代码的外部文件。<br>——type:可选。编写代码使用的脚本语言的内容类型。</p>
<hr>
<p><strong>几个小例子:</strong><br>defer的先后出现次序</p>
<figure class="highlight html"><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></pre></td><td class="code"><pre><span class="line"><span class="meta"><!doctype <span class="meta-keyword">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"><span class="tag"><<span class="name">script</span> <span class="attr">defer</span> <span class="attr">src</span>=<span class="string">"test.js"</span>></span><span class="tag"></<span class="name">script</span>></span></span><br><span class="line"><span class="tag"><<span class="name">script</span> <span class="attr">defer</span> <span class="attr">src</span>=<span class="string">"test2.js"</span>></span><span class="tag"></<span class="name">script</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">body</span>></span></span><br><span class="line"> </span><br><span class="line"> <span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></table></figure>
<p>HTML要求脚本按照他们出现的先后顺序执行,因此第一个延迟脚本会先于第二给延迟脚本执行。就是<em>test1.js</em>会先执行。因此最好只包含一个延迟脚本。</p>
<p>:fa-star:<em>defer属性只适用于外部脚本文件。</em></p>
<hr>
<p><strong>async的异步响应:</strong></p>
<figure class="highlight html"><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></pre></td><td class="code"><pre><span class="line"><span class="meta"><!doctype <span class="meta-keyword">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"><span class="tag"><<span class="name">script</span> <span class="attr">async</span> <span class="attr">src</span>=<span class="string">"test.js"</span>></span><span class="tag"></<span class="name">script</span>></span></span><br><span class="line"><span class="tag"><<span class="name">script</span> <span class="attr">async</span> <span class="attr">src</span>=<span class="string">"test2.js"</span>></span><span class="tag"></<span class="name">script</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">body</span>></span></span><br><span class="line"> </span><br><span class="line"> <span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></table></figure>
<p>第二个脚本文件可能会在第一个脚本文件之前执行。<br>:fa-star:<em>async属性只适用于外部脚本文件。</em></p>
<hr>
<h2 id="2-JavaScript执行"><a href="#2-JavaScript执行" class="headerlink" title="2.JavaScript执行"></a>2.JavaScript执行</h2><p>javascript解释过程包括两个阶段,<strong>预处理(预编译)</strong>和<strong>执行</strong>。在预编译期,JavaScript解释器将完成对JavaScript代码的预处理操作,把JavaScript代码转换为字节码;在执行期,JavaScript解释器的字节码生成二进制机械码,并且按照书序执行,完成程序设计的任务。</p>
<blockquote>
<p><em>预编译包括词法分析和语法分析。词法分析主要对JavaScript脚本进行逐一分析,检查脚本是否呼和JavaScript规范,是否存在语法错误</em></p>
</blockquote>
<p>HTML文档在浏览器中的解析过程是:按文档流从上到下逐步解析页面的结构和信息。javascript代码作为嵌入的脚本应该也算做HTML文档的组成部分。所以JavaScript代码在装载时的执行顺序也是根据<code><script></code>的标签出现的顺序来确定的。</p>
<hr>
<h5 id="预编译"><a href="#预编译" class="headerlink" title="预编译"></a>预编译</h5><p>当JavaScript引擎解析脚本时,他会在预编译期对所有的声明的变量和函数预先进行处理。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">alert(a);<span class="comment">//返回undefined</span></span><br><span class="line"><span class="keyword">var</span> a=<span class="number">1</span>;</span><br><span class="line">alert(a);<span class="comment">//返回1</span></span><br></pre></td></tr></table></figure>
<p>JavaScript解释器是按照代码先后顺序进行解析的,如果在前面代码行中没有为变量赋值,则JavaScript解释器会使用默认值undefined。 </p>
<figure class="highlight javascript"><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">f();<span class="comment">//调用函数,显示语法错误。</span></span><br><span class="line">avr f=<span class="function"><span class="keyword">function</span> <span class="title">f</span>(<span class="params"></span>)</span>{</span><br><span class="line"> alert(<span class="number">1</span>);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>在预编译器JavaScript解释器只能为了声明变量f进行处理,而对于变量f的值,只能等到执行期时按顺序进行赋值,自然就会出现语法错误。</p>
<hr>
<p>onload事件只有在文档加载完毕才会响应,因此为了运行安全,一般设计在页面初始化完毕之后才允许JavaScript代码执行,这样就可以避免因为代码加载延迟对JavaScript执行的影响。</p>
<h2 id="3-错误处理"><a href="#3-错误处理" class="headerlink" title="3.错误处理"></a>3.错误处理</h2><p>JavaScript是松散型的,不会验证函数的参数,因此错误只会在代码运行期间出现。</p>
<h6 id="错误类型"><a href="#错误类型" class="headerlink" title="错误类型"></a>错误类型</h6><p>-<strong>ERROR</strong>:普通异常,通常和throw和trycatch语句一同使用。利用属性name可以声明或了解异常的类型,利用message属性可以设置和读取异常的详细信息。</p>
<ul>
<li><strong>EvalError</strong>:在不正确使用eval()方法时抛出。</li>
<li><strong>SyntxError</strong>:抛出语法错误。</li>
<li><strong>RangerError</strong>:在数字超出合法范围时抛出。</li>
<li><strong>TypeError</strong>:当一个值得类型错误是抛出该异常。在变量中保存意外的类型,或者在访问不纯在的方法时,都会导致</li>
<li><strong>URIError</strong>:由URI的编码和编码方法抛出。</li>
</ul>
<hr>
<p>这些代码抛出了一个通用错误,其中有一跳自定义错误。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> e = <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">'Could not parse input'</span>); </span><br><span class="line"><span class="comment">// e.message is 'Could not parse input'</span></span><br><span class="line"><span class="keyword">throw</span> e;</span><br></pre></td></tr></table></figure>
<p>这个方式主要是应用在上千行代码的程序中,你可以抛出一个带有包含了函数的名称,以及为什么会发生错误的明确描述。如果一个复杂的WEB程序发生了这个错误,那么查找问题的根源也容易多了。</p>
<hr>
<p><strong>错误事件</strong><br>在任何web浏览器中,onerror事件处理程序都不会创建event对象,但是他可以接受三个参数;错误消息,,错误消息所在的URL和行号。<br>但是有些时候只有错误消息才会有用。剩下两个在有些情况下不能准确的完成一些事情。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.onerror=<span class="function"><span class="keyword">function</span>(<span class="params">message,url,line</span>)</span>{</span><br><span class="line"> alert(message);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>只要发生错误,无论是不是浏览器生成的都会触发error事件。并且执行这个处理程序。然后浏览器默认的机制发挥作用。<br>但是也可以通过以下方式,来组织浏览器报告错误的默认行为。</p>
<figure class="highlight javascript"><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="built_in">window</span>.onerror=<span class="function"><span class="keyword">function</span>(<span class="params">message,url,line</span>)</span>{</span><br><span class="line"> alert(message);</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>通过返回false,这个函数实际上是充当了<strong>整个文档</strong>中的trycatch语句,可以捕获所有无代码处理的运行时的错误,可以说是避免浏览器错误报告的最后一道防线。</p>
<hr>
<p>###3.标识符<br>标识符是表示名称的意思,有以下几条规则。</p>
<ul>
<li><p>第一个字符必须是字母,下划线(_)或美元$符号。</p>
</li>
<li><p>除了第一个字符之外,其他位置的自负可以使用字母,数字,下划线,美元符号。</p>
</li>
<li><p>标识符名称不能与JavaScript关键字或保留字同名。</p>
</li>
<li><p>可以在标识符中使用Unicode转义序列。</p>
<hr>
<p>允许用户不声明变量,而直接为变量赋值,因为JavaScript解释器能够自动隐式声明变量。但是隐式声明的变量总是作为全局变量而存在的。</p>
<figure class="highlight javascript"><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="function"><span class="keyword">function</span> <span class="title">f</span>(<span class="params"></span>)</span>{</span><br><span class="line"> a=<span class="number">1</span>;<span class="comment">//不声明变量</span></span><br><span class="line"> <span class="keyword">var</span> b=<span class="number">2</span>;</span><br><span class="line">}</span><br><span class="line">f();</span><br><span class="line">alert(a);<span class="comment">//返回1</span></span><br><span class="line">alert(b);<span class="comment">//提示语法错误,找不到该变量。</span></span><br></pre></td></tr></table></figure>
<hr>
<p>函数未调用之前,函数内部定义的全局变量是无效的,这是因为在JavaScript预编译器,仅对函数名,函数内各种表示符建立索引。<br>只有当JavaScript执行其时,才按照顺序为变量进行赋值,并初始化。而在执行期,如果函数未被调用,则函数内代码是不被解析的,所以才有了如下情况。</p>
<figure class="highlight javascript"><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></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">f</span>(<span class="params"></span>)</span>{</span><br><span class="line"> a=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">var</span> b=<span class="number">2</span>;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">try</span>{</span><br><span class="line"> alert(a);<span class="comment">//尝试读取变量。</span></span><br><span class="line">}<span class="keyword">catch</span>{</span><br><span class="line"> alert(e.message);<span class="comment">//显示错误信息,变量a未定义</span></span><br><span class="line">}</span><br><span class="line">f();<span class="comment">//调用函数</span></span><br><span class="line">alert(a);<span class="comment">//读取全局变量a,返回值为1</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<hr>
<p><strong>变量的作用域</strong></p>
<p>全局作用域是指变量在整个页面脚本中都是可见的,可以自由访问。<br>局部作用域是指变量仅能在生命的函数内部可见,函数外是不允许访问的。</p>
<p><strong>全局变量是所有全局变量的容器</strong>在web浏览器中,全局对象名为windows<br>上面的话是这个意思</p>
<figure class="highlight javascript"><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="keyword">var</span> f=<span class="number">123</span>;</span><br><span class="line"><span class="built_in">window</span>.f=<span class="number">123</span>;<span class="comment">//两者的意思是一样的。</span></span><br><span class="line"><span class="comment">//下面有一个小知识</span></span><br><span class="line">f=<span class="number">123</span><span class="comment">//隐式全局变量,刚才讲过。以这种方式定义的全局变量被称为隐式的全局变量</span></span><br></pre></td></tr></table></figure>
<h2 id="4-数据类型"><a href="#4-数据类型" class="headerlink" title="4.数据类型"></a>4.数据类型</h2><ul>
<li>number<ul>
<li>javascript数值类型不再细分整形,浮点型,所有数值都属于浮点型。</li>
<li>数值直接量可以细分为整形直接量和浮点型直接量。浮点数就是带有小数点的数值,而整数是不带小数点的数。<br><code>var int=1;//整形数值</code><br><code>var float =1.0//浮点型数值</code></li>
<li>toString()是一个非常实用的办法,它可以根据所传递的参数把数值转换为对应进制的数值字符串<figure class="highlight javascript"><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></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a=<span class="number">32</span>;</span><br><span class="line"><span class="built_in">document</span>.writeln(a.toSting(<span class="number">2</span>));<span class="comment">//返回二进制字符串</span></span><br><span class="line"><span class="built_in">document</span>.writeln(a.toSting(<span class="number">4</span>));<span class="comment">//返回字符串200</span></span><br><span class="line"><span class="built_in">document</span>.writeln(a.toSting(<span class="number">16</span>));<span class="comment">//返回字符串20</span></span><br><span class="line"><span class="built_in">document</span>.writeln(a.toSting(<span class="number">30</span>));</span><br><span class="line"><span class="built_in">document</span>.writeln(a.toSting(<span class="number">32</span>));</span><br><span class="line"><span class="comment">//对于直接量,不能直接调用toString();必须加一个()</span></span><br><span class="line"><span class="built_in">document</span>.writeln((<span class="number">32</span>).toSting(<span class="number">2</span>));</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li>字符串</li>
<li>布尔值</li>
<li>NULL<ul>
<li>Null类型只有一个值,即null,他表示空值。</li>
<li>typeof运算符检测null,返回Object,表名它属于对象类型。</li>
</ul>
</li>
<li>Undefined<ul>
<li>undefined是Undefined类型的唯一值,他表示未定义的值。当声明变量为赋值时,或者定义属性未设置值时,默认他们的值为undefined。</li>
</ul>
</li>
</ul>
<hr>
<p>####几个小例子<br>**使用constructor检测类型<br>对于对象,数组等复杂数据,可以使用Object对象的constructor属性进行检测。constructor表示构造器,概述性质引用的是构造当前对象的函数。</p>
<figure class="highlight javascript"><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="keyword">var</span> o={};</span><br><span class="line"><span class="keyword">var</span> a=[];</span><br><span class="line">alert(o.constrcutor==<span class="built_in">Object</span>);<span class="comment">//返回true</span></span><br><span class="line">alert(o.constrcutor==<span class="built_in">Array</span>);<span class="comment">//返回true</span></span><br></pre></td></tr></table></figure>
<p>**但是对于undefined和null特殊值就不能够使用constructor属性,要不然会抛出异常,你必须确定,其中是true</p>
</div>
<footer class="article-footer">
<a data-url="http://yoursite.com/2020/01/14/%E5%85%A5%E9%97%A8%E8%BF%9B%E9%98%B6/" data-id="ck7bowaxw00001cv52u08bafp" class="article-share-link">Share</a>
</footer>
</div>
</article>
<article id="post-hexo的部署" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2020/01/12/hexo%E7%9A%84%E9%83%A8%E7%BD%B2/" class="article-date">
<time datetime="2020-01-12T14:54:37.000Z" itemprop="datePublished">2020-01-12</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2020/01/12/hexo%E7%9A%84%E9%83%A8%E7%BD%B2/">helloWorld</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
</div>
<footer class="article-footer">
<a data-url="http://yoursite.com/2020/01/12/hexo%E7%9A%84%E9%83%A8%E7%BD%B2/" data-id="ck77d1udd00002ov56ut18i0d" class="article-share-link">Share</a>
</footer>
</div>
</article>
<article id="post-javascript" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2020/01/12/javascript/" class="article-date">
<time datetime="2020-01-12T14:54:37.000Z" itemprop="datePublished">2020-01-12</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2020/01/12/javascript/">helloWorld</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
</div>
<footer class="article-footer">
<a data-url="http://yoursite.com/2020/01/12/javascript/" data-id="ck77d1udi00012ov556tc7wwm" class="article-share-link">Share</a>
</footer>
</div>
</article>
<article id="post-java" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2020/01/12/java/" class="article-date">
<time datetime="2020-01-12T14:48:53.253Z" itemprop="datePublished">2020-01-12</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2020/01/12/java/">Hello World</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>Welcome to <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/" target="_blank" rel="noopener">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html" target="_blank" rel="noopener">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues" target="_blank" rel="noopener">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="https://hexo.io/docs/writing.html" target="_blank" rel="noopener">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="https://hexo.io/docs/server.html" target="_blank" rel="noopener">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="https://hexo.io/docs/generating.html" target="_blank" rel="noopener">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="https://hexo.io/docs/one-command-deployment.html" target="_blank" rel="noopener">Deployment</a></p>
</div>
<footer class="article-footer">
<a data-url="http://yoursite.com/2020/01/12/java/" data-id="ck77d1udp00022ov52glx8p93" class="article-share-link">Share</a>
</footer>
</div>
</article>
</section>
<aside id="sidebar">
<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/2020/03/">March 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/02/">February 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/01/">January 2020</a></li></ul>
</div>
</div>
<div class="widget-wrap">
<h3 class="widget-title">Recent Posts</h3>
<div class="widget">
<ul>
<li>
<a href="/2020/03/10/%E7%A2%8E%E5%98%B4%E7%AE%97%E6%B3%95-%E6%99%AE%E9%80%9A%E9%98%9F%E5%88%97/">碎嘴算法--普通队列</a>
</li>
<li>
<a href="/2020/03/10/%E7%A2%8E%E5%98%B4%E7%AE%97%E6%B3%95-%E5%BE%AA%E7%8E%AF%E9%98%9F%E5%88%97/">碎嘴算法--循环队列</a>
</li>
<li>
<a href="/2020/03/09/leetcode%E5%BF%85%E8%A6%81%E6%8A%80%E5%B7%A7-%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92-%E4%B8%80/">leetcode必要技巧--动态规划(一)</a>
</li>
<li>
<a href="/2020/02/29/%E7%AC%AC%E4%B8%80%E7%AF%87%E5%8D%9A%E5%AE%A2/">第一篇博客</a>
</li>
<li>
<a href="/2020/02/29/%E7%AC%AC%E4%B8%80%E7%AF%87%E6%96%87%E7%AB%A0-1/">JavaScript进阶入门(二)</a>
</li>
</ul>
</div>
</div>
</aside>
</div>
<footer id="footer">
<div class="outer">
<div id="footer-info" class="inner">
© 2020 John Doe<br>
Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
</div>
</div>
</footer>
</div>
<nav id="mobile-nav">
<a href="/" class="mobile-nav-link">Home</a>
<a href="/archives" class="mobile-nav-link">Archives</a>
</nav>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
<script src="/fancybox/jquery.fancybox.pack.js"></script>
<script src="/js/script.js"></script>
</div>
</body>
</html>