-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
884 lines (383 loc) · 28.5 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
<!DOCTYPE html>
<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">
<link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">
<meta name="keywords" content="Hexo, NexT" />
<meta property="og:type" content="website">
<meta property="og:title" content="wx2020's blog">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="wx2020's blog">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="wx2020's blog">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Mist',
version: '5.1.4',
sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
fancybox: true,
tabs: true,
motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
duoshuo: {
userId: '0',
author: '博主'
},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
hits: {"per_page":10},
labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
}
};
</script>
<link rel="canonical" href="http://yoursite.com/"/>
<title>wx2020's blog</title>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container sidebar-position-left
page-home">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-wrapper">
<div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">wx2020's blog</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle"></p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-fw fa-home"></i> <br />
首页
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section">
<i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
归档
</a>
</li>
</ul>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/05/06/初步搭建开源PT程序NexusPHP/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="wx2020">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="wx2020's blog">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2018/05/06/初步搭建开源PT程序NexusPHP/" itemprop="url">初步搭建开源PT程序NexusPHP</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2018-05-06T18:29:33+08:00">
2018-05-06
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>本学期开学,宿舍里最大的变化就是学校正式开通了LCU-Wifi宿舍无线,属于教育网双栈,30M/s ipv4,100M/s ipv6。<br>比较坑的是,我校ipv6使用也需要登陆,还好没有v4的流量限制。使用ipv6去TUNA镜像网站下载速度能跑满网速。前期听老师们说v6可以高速PT下载,自己就在网上找了找资料。</p>
<blockquote>
<p>PT(Private Tracker)下载其实也是Bt下载的一种,但有两个明显的改进:<br>一是私密的小范围下载,二是进行流量统计,根据上载量决定你的权限。 BT下载时,软件会分析.torrent种子文件得到Tracker地址,然后连接Tracker服务器,服务器返回其他下载者的IP,下载者再与这些IP联系进行下载,从而减轻了服务器的负担,BT下载的Tracker是公开的,而Private Tracker 下载(PT下载)的Tracker则是私有的,每个人的Tracker是不同的,即passkey不同,passkey对PT下载者很重要,所以不要轻易泄露出去。</p>
</blockquote>
<p>购买了校园网无线以后,为了更好的利用ipv6,我使用我校的.edu.cn邮箱,注册了一个西工大蒲公英pt,第一次体验下载了个视频,果然速度美滋滋。</p>
<p>目前在实验室我有一台自己使用的电脑,与宿舍的无线基本上共属于同一台局域网,并且可以指定教育网ip,v6的ip也是重启后才会更换,了解到浙大在2010年开源了一个PT程序————NexusPHP,我就开始在实验室的电脑上初步搭建。</p>
<p>以下记录了我这三天来的搭建实例,可复现。</p>
<p>开始之前先科普一下上述中的基本知识。</p>
<blockquote>
<p>Ubuntu:世界有名的Linux桌面版,采用的最新版16.04.4LTS。</p>
<p>Nginx: 采用Ubuntu apt 安装方式安装的1.13.3版本。</p>
<p>MySQL:也是用的Ubuntu apt安装方式,版本为5.7.21。</p>
<p>Php:安装方式同上,版本为5.6。<strong><em>注意,必须使用5.6版本,新版7.X与本项目不兼容。</em></strong></p>
<p>nexusPHP:这是NexusPHP由来自浙江大学的Nexus团队发起并开发完成。它的目标是代替TBSource项目,提供一个完整的、有序的、重视用户信誉和知识的资源分享社区的解决方案。 这是一个比较老的BT开源项目,2010年开源的。</p>
</blockquote>
<hr>
<p>介绍完毕,开始干活!</p>
<h2 id="安装MySQL"><a href="#安装MySQL" class="headerlink" title="安装MySQL"></a>安装MySQL</h2><p>1、启动终端,输入以下代码安装:</p>
<pre><code>sudo apt update
sudo apt-get install mysql-server
sudo apt install libmysqlclient-dev
</code></pre><ul>
<li>安装过程会提示设置mysql root用户的密码,设置完成后等待自动安装即可。默认安装完成就启动了mysql。</li>
<li>安装成功后可以通过下面的命令测试是否安装成功:<br><code>sudo netstat -tap | grep mysql</code></li>
</ul>
<p>2、更改mysql字符集为utf8:</p>
<pre><code>#分别用编辑器打开两个文件#
sudo gedit /etc/mysql/mysql.conf.d/mysqld.cnf
sudo gedit /etc/mysql/conf.d/mysql.cnf
</code></pre><ul>
<li>在[mysqld]下添加一行 <code>character_set_server=utf8</code></li>
<li>在[mysql]下添加一行 <code>default_character_set=utf8</code></li>
<li>使用 <code>service mysql restart</code>重启mysql服务。</li>
</ul>
<h2 id="安装Nginx"><a href="#安装Nginx" class="headerlink" title="安装Nginx"></a>安装Nginx</h2><p>启动终端,输入以下代码安装:</p>
<pre><code>sudo apt install nginx
</code></pre><ul>
<li>浏览器输入 <code>localhost</code>,出现 nginx 欢迎界面则表示成功</li>
</ul>
<h2 id="安装PHP5-6"><a href="#安装PHP5-6" class="headerlink" title="安装PHP5.6"></a>安装PHP5.6</h2><p>1、添加PPA。</p>
<pre><code>sudo apt install python-software-properties software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
</code></pre><p>2、安装PHP5.6以及扩展。</p>
<pre><code>sudo apt install php5.6-fpm php5.6-mysql php5.6-common php5.6-curl php5.6-cli php5.6-mcrypt php5.6-mbstring php5.6-xml
</code></pre><p>3、配置PHP5.6:</p>
<pre><code>sudo gedit /etc/php/5.6/fpm/php.ini
</code></pre><ul>
<li><p>找到cgi.fix_pathinfo选项,去掉注释;,然后将值设置为0:</p>
<p> <code>cgi.fix_pathinfo = 0</code></p>
</li>
</ul>
<p>4、再次配置Nginx:</p>
<pre><code>sudo gedit /etc/nginx/sites-available/default
</code></pre><p>去掉 <code>location ~ \.php</code> 整个闭包的注释,并将其修改为:</p>
<pre><code>location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php5.6-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
</code></pre><p>5、重启Nginx 和 PHP5.6并测试:</p>
<pre><code>sudo service php5.6-fpm restart
sudo service nginx restart
</code></pre><ul>
<li>使用 <code>echo "<?php phpinfo(); ?>" >> /var/www/html/phpinfo.php</code> 生成测试文件。</li>
<li>从浏览器打开 <code>localhost/phpinfo.php</code>,出现 php的一系列信息则表示配置成功。</li>
</ul>
<h2 id="开始NexusPHP安装实战"><a href="#开始NexusPHP安装实战" class="headerlink" title="开始NexusPHP安装实战"></a>开始NexusPHP安装实战</h2><h2 id="安装MemCache-MemCached"><a href="#安装MemCache-MemCached" class="headerlink" title="安装MemCache/MemCached"></a>安装MemCache/MemCached</h2><p>1、启动终端,输入以下代码安装:</p>
<pre><code>sudo apt install memcached
sudo apt install php-memcache
sudo apt install php-memcached
</code></pre><p>2、配置php.ini。</p>
<ul>
<li>寻找 扩展目录 :<br> <code>find /usr -name "memcache*</code></li>
</ul>
<p>示例输出:</p>
<pre><code>/usr/lib/php/20131226/memcache.so
/usr/lib/php/20131226/memcached.so
</code></pre><ul>
<li>根据以上输出,修改php.ini文件。<br>①把php.ini中的 <code>extension_dir = "./"</code> 修改为 <code>extension_dir = "/usr/lib/php/20131226/"</code>并去掉注释。②添加两行来载入memcache/memcached扩展:<code>extension=memcache.so</code> <code>extension=memcached.so</code> </li>
</ul>
<p>3、Memcache环境测试: </p>
<ul>
<li>在/var/www/html/下创建以下面为内容的memcached.php文件。创建完毕后使用浏览器访问 <code>localhost/memcache.php</code> ,如果有输出 <code>This is a test!</code> ,就表示环境搭建成功。</li>
</ul>
<h2 id="安装Pear和HTTP-Request2"><a href="#安装Pear和HTTP-Request2" class="headerlink" title="安装Pear和HTTP_Request2"></a>安装Pear和HTTP_Request2</h2><p>启动终端,输入以下代码安装:</p>
<pre><code>sudo apt install php-pear
sudo pear config-set preferred_state alpha
pear install HTTP_Request2
</code></pre><h2 id="安装-phpmyadmin-并配置"><a href="#安装-phpmyadmin-并配置" class="headerlink" title="安装 phpmyadmin 并配置"></a>安装 phpmyadmin 并配置</h2><p>启动终端,输入以下代码安装并配置:</p>
<pre><code>sudo apt install phpmyadmin
cd /var/www/html && ln -s /usr/share/phpmyadmin
sudo service nginx restart
</code></pre><p>然后打开浏览器访问 <code>localhost/phpmyadmin/index.php</code> 出现 <code>欢迎使用phpmyadmin</code> ye’mian则表示成功。</p>
<h2 id="开始正式安装nexusPHP"><a href="#开始正式安装nexusPHP" class="headerlink" title="开始正式安装nexusPHP"></a>开始正式安装nexusPHP</h2><p>1、首先从sourceforge官网下载NexusPHP源码镜像并解压至网站目录。</p>
<pre><code>wget https://nchc.dl.sourceforge.net/project/nexusphp/nexusphp.v1.5.beta5.20120707.zip
unzip -o nexusphp* -d /var/www/html/
</code></pre><p>2、恢复原网站msyqldump文件。</p>
<pre><code>mysql -u root -p password
# 输入密码 #
mysql > create database nexusphp;
mysql > use nexusphp;
mysql > source /var/www/html/_db/dbstructure.sql;
</code></pre><ul>
<li>打开 localhost/phpmyadmin/index.php 确认数据表是否添加成功。</li>
</ul>
<p>3、修改配置文件allconfig.php。</p>
<pre><code>gedit /var/www/html/config/allconfig.php
</code></pre><p>修改 <code>$BASIC=array</code> 闭包为以下内容:</p>
<pre><code>$BASIC=array(
'SITENAME' => 'NexusPHP',
'BASEURL' => 'localhost',
'announce_url' => 'localhost/announce.php',
'mysql_host' => 'localhost',
'mysql_user' => 'root',
'mysql_pass' => '数据库密码',
'mysql_db' => 'nexusphp',
</code></pre><p>);</p>
<ul>
<li>请用正确密码匹配以上信息。</li>
</ul>
<p>4、设置管理员。</p>
<ul>
<li>首先自己访问网站然后注册一个用户名,接着进入数据库管理(phpMyAdmin),在users表里面找到你注册的用户,编辑它的class属性为16,完成。</li>
</ul>
<p><strong>到这里PT基本上就架设完毕了,现在只需要利用管理员帐号对站点进行设置和调整就可以了。</strong></p>
<p>浏览器输入localhost/nexusphp/login.php 出现登陆页面则表示成功。</p>
<hr>
<p>部分信息来自:</p>
<ul>
<li><a href="http://blog.csdn.net/Mikeoperfect/article/details/78868185" target="_blank" rel="noopener">http://blog.csdn.net/Mikeoperfect/article/details/78868185</a></li>
<li><a href="https://www.zhihu.com/question/27344154/answer/105577094" target="_blank" rel="noopener">https://www.zhihu.com/question/27344154/answer/105577094</a></li>
</ul>
<p><strong>著作权归作者所有。</strong></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/05/06/斐波那契数列问题的非递归实现/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="wx2020">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="wx2020's blog">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2018/05/06/斐波那契数列问题的非递归实现/" itemprop="url">斐波那契数列问题的非递归实现</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2018-05-06T17:27:33+08:00">
2018-05-06
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="问题引入"><a href="#问题引入" class="headerlink" title="问题引入"></a>问题引入</h2><p>有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少? </p>
<h2 id="程序分析"><a href="#程序分析" class="headerlink" title="程序分析"></a>程序分析</h2><p>兔子的规律为数列1,1,2,3,5,8,13,21 … ,属于斐波那契数列问题。该数列有一个规律: F(0) = 1 , F(1) = 1, F(n) = F(n-1) + F(n-2)(n>=2) …</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><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Prog1</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="keyword">int</span> n = <span class="number">50</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) {</span><br><span class="line"> System.out.println(<span class="string">"After "</span> + i + <span class="string">" mouths, the number of rabbit is "</span> + fun2(i));</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">long</span> <span class="title">fun2</span><span class="params">(<span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span> (n == <span class="number">1</span> || n == <span class="number">2</span>)</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> fun(n-<span class="number">1</span>) + fun(n-<span class="number">2</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>这样的递归算法虽然只有简单的几行,但是效率却很低。为什么呢?我们可以分析其递归调用的时间复杂度: </p>
<p><strong>时间复杂度</strong> —– O(2^N) </p>
<p>由于使用递归时,其执行步骤是:要得到后一个数之前必须先计算出之前的两个数,即在每个递归调用时都会触发另外两个递归调用,例如:要得到F(10)之前得先得到F(9)、F(8),那么得到F(9)之前得先得到F(8)、F(7)……如此递归下去 。</p>
<p>这样的计算是以 2 的次方在增长的。</p>
<p>除此之外,我们也可以看到,F(8)和F(7)的值都被多次计算,如果递归的深度越深,那么F(8)和F(7)的值会被计算更多次,但是这样计算的结果都是一样的,除了其中之一外,其余的都是浪费,可想而知,这样的开销是非常恐怖的 。</p>
<p>所以,如果在时间复杂度和空间复杂度都有要求的话,我们可以用以下的非递归算法来实现。</p>
<h2 id="非递归算法"><a href="#非递归算法" class="headerlink" title="非递归算法"></a>非递归算法</h2><p><strong>时间复杂度为<em>O(N)</em>,空间复杂度为<em>O(1)</em> </strong></p>
<p><strong>思路</strong>:借助两个变量 first 和 second ,每次将 first 和 second 相加后赋给 third ,再将 second 赋给 first ,third 赋给 second,如此循环。 </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></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Prog1</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="keyword">int</span> n = <span class="number">50</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) {</span><br><span class="line"> System.out.println(<span class="string">"After "</span> + i + <span class="string">" mouths, the number of rabbit is "</span> + fun2(i));</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">long</span> <span class="title">fun</span><span class="params">(<span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">long</span> first = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">long</span> second = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">long</span> third = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">2</span>; i <= n; i++) {</span><br><span class="line"> third = first + second;</span><br><span class="line"> first = second;</span><br><span class="line"> second = third;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> third;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
</section>
</div>
</div>
<div class="sidebar-toggle">
<div class="sidebar-toggle-line-wrap">
<span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
<span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
<span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
</div>
</div>
<aside id="sidebar" class="sidebar">
<div class="sidebar-inner">
<section class="site-overview-wrap sidebar-panel sidebar-panel-active">
<div class="site-overview">
<div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
<p class="site-author-name" itemprop="name">wx2020</p>
<p class="site-description motion-element" itemprop="description"></p>
</div>
<nav class="site-state motion-element">
<div class="site-state-item site-state-posts">
<a href="/archives/">
<span class="site-state-item-count">2</span>
<span class="site-state-item-name">日志</span>
</a>
</div>
<div class="site-state-item site-state-tags">
<span class="site-state-item-count">2</span>
<span class="site-state-item-name">标签</span>
</div>
</nav>
</div>
</section>
</div>
</aside>
</div>
</main>
<footer id="footer" class="footer">
<div class="footer-inner">
<div class="copyright">© <span itemprop="copyrightYear">2018</span>
<span class="with-love">
<i class="fa fa-user"></i>
</span>
<span class="author" itemprop="copyrightHolder">wx2020</span>
</div>
<div class="powered-by">由 <a class="theme-link" target="_blank" href="https://console.upyun.com/register/?invite=r1HkUzOvM">又拍云</a> 强力驱动</div>
</div>
</footer>
<div class="back-to-top">
<i class="fa fa-arrow-up"></i>
</div>
</div>
<script type="text/javascript">
if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
window.Promise = null;
}
</script>
<script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
<script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
<script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>
</body>
</html>