Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dde65b2

Browse files
committed
学习算法
1 parent 8c5e5f4 commit dde65b2

File tree

27 files changed

+873
-189
lines changed

27 files changed

+873
-189
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ node_modules
33
coverage
44
.vscode
55
.DS_Store
6+
7+
temp

README.zh-CN.md

Lines changed: 196 additions & 188 deletions
Large diffs are not rendered by default.

examples/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 算法练习
2+
3+
- 阶乘函数 [factorial]
4+
- 斐波那契数列 [fibonacci]
5+
- 全局唯一标识符
6+
- [uuid]
7+
- [guid]
8+
- Twitter的雪花算法 [snowflake]
9+
- LZ系列压缩算法
10+
- LZ系列压缩算法均为LZ77与LZ78的变种,在此基础上做了优化。
11+
- LZ77:LZSS、LZR、LZB、LZH;
12+
- LZ78:LZW、LZC、LZT、LZMW、LZJ、LZFG。
13+
- 其中,LZSS与LZW为这两大阵容里名气最响亮的算法。LZSS是由Storer与Szymanski [2]改进了LZ77:增加最小匹配长度的限制,当最长匹配的长度小于该限制时,则不压缩输出,但仍然滑动窗口右移一个字符。Google开源的Snappy压缩算法库大体遵循LZSS的编码方案,在其基础上做了一些工程上的优化。
14+
- LRU
15+
- 判断回文数
16+
- 最长回文子串 https://www.cnblogs.com/en-heng/p/3973679.html
17+
- 去掉重复值(数组)
18+
- 找出字符串中出现次数最多的字母
19+
- 求最大值、最小值
20+
- 验证是否为数组
21+
22+
Node.js大众点评爬虫 https://www.cnblogs.com/en-heng/p/5895207.html
23+
24+
- lz77 算法
25+
- https://www.jb51.net/article/23024.htm
26+
- https://www.jb51.net/article/120912.htm
27+
28+
- 随机算法 random
29+
- uuid
30+
- 随机洗牌 knuth-shuffle
31+
- http://caibaojian.com/js-random-array.html
32+
- https://www.h5jun.com/post/array-shuffle.html
33+
- http://caibaojian.com/get-random-element.html
34+
- 5分钟现场撸代码——谈总结会抽奖程序 https://www.h5jun.com/post/luckey-draw-in-5-minutes.html
35+
36+
- Twitter的雪花算法 snowflake
37+
38+
重学前端 https://www.w3cplus.com/relearn-the-front-end-techniques.html
39+
40+
排序算法
41+
42+
第三届360前端星计划在线作业题
43+
https://www.h5jun.com/post/75team-star-handlock.html
44+
45+
用65行代码实现JavaScript动画序列播放
46+
https://www.h5jun.com/post/sixty-lines-of-code-animation.html
47+
48+
为什么 [ ] 是 false 而 !![ ] 是 true
49+
https://www.h5jun.com/post/why-false-why-true.html
50+
51+
https://www.cnblogs.com/en-heng/category/582209.html
52+
【十大经典数据挖掘算法】SVM系列 https://www.cnblogs.com/en-heng/p/5965438.html
53+
54+
别人家的面试题:不可变数组快速范围求和 https://75team.com/post/range-sum-query-immutable.html
55+
别人家的面试题:自然数拆分求最大乘积 https://75team.com/post/integer-break.html
56+
57+
别人家的面试题:统计“1”的个数(续) https://75team.com/post/count_bits.html
58+
59+
https://www.h5jun.com/post/html5-element-flowchart.html
60+
HTML5 元素选择流程图
61+
62+
https://www.h5jun.com/post/multiply7.html 别人家的面试题:不用加减乘除,求整数的7倍
63+
64+
C4.5
65+
K-Means
66+
SVM
67+
Apriori
68+
EM
69+
PageRank
70+
AdaBoost
71+
kNN
72+
Naïve Bayes
73+
CART

examples/factorial/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 阶乘函数 Factorial
2+
3+
描述:一个正整数的阶乘是所有小于及等于该数的正整数的积,并且有`0`的阶乘为`1`。自然数`n`的阶乘写作`n!`
4+
5+
阶乘函数是[递归(Haskell)函数](https://zh.wikibooks.org/zh/Haskell/%E9%80%92%E5%BD%92)典型示例
6+
7+
参考资料:
8+
9+
- https://www.w3cplus.com/javascript/factorial-function-in-javascript.html
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Javascript Node CircleCI 2.0 configuration file
2+
#
3+
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
4+
#
5+
version: 2
6+
jobs:
7+
build:
8+
docker:
9+
# specify the version you desire here
10+
- image: circleci/node:8.11.1
11+
12+
# Specify service dependencies here if necessary
13+
# CircleCI maintains a library of pre-built images
14+
# documented at https://circleci.com/docs/2.0/circleci-images/
15+
# - image: circleci/mongo:3.4.4
16+
17+
working_directory: ~/repo
18+
19+
steps:
20+
- checkout
21+
22+
# Download and cache dependencies
23+
- restore_cache:
24+
keys:
25+
- v1-dependencies-{{ checksum "package.json" }}
26+
# fallback to using the latest cache if no exact match is found
27+
- v1-dependencies-
28+
29+
- run: yarn install
30+
31+
- save_cache:
32+
paths:
33+
- node_modules
34+
key: v1-dependencies-{{ checksum "package.json" }}
35+
36+
# run tests!
37+
- run: yarn test
38+
39+

examples/fibonacci/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 斐波那契数列 fibonacci
2+
3+
<p>
4+
<a href="https://circleci.com/gh/jskit/kit-fibonacci/tree/dev"><img src="https://img.shields.io/circleci/project/jskit/kit-fibonacci/dev.svg" alt="Build Status"></a>
5+
<a href="https://codecov.io/github/jskit/kit-fibonacci?branch=dev"><img src="https://img.shields.io/codecov/c/github/jskit/kit-fibonacci/dev.svg" alt="Coverage Status"></a>
6+
</p>
7+
8+
使用 JavaScript 尽可能快地来计算斐波那契数列。
9+
10+
from https://github.com/jskit/kit-fibonacci
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const assert = require('assert')
2+
const fastfib = require('../lib/index')
3+
4+
assert.equal(fastfib(0), 0)
5+
assert.equal(fastfib(1), 1)
6+
assert.equal(fastfib(2), 1)
7+
assert.equal(fastfib(3), 2)
8+
assert.equal(fastfib(4), 3)
9+
assert.equal(fastfib(5), 5)
10+
assert.equal(fastfib(6), 8)
11+
assert.equal(fastfib(7), 13)
12+
assert.equal(fastfib(8), 21)
13+
assert.equal(fastfib(9), 34)
14+
assert.equal(fastfib(10), 55)
15+
assert.equal(fastfib(11), 89)
16+
assert.equal(fastfib(12), 144)
17+
18+
console.log('Test passed')

examples/fibonacci/benchmark/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
// https://www.npmjs.com/package/benchmark
3+
4+
const assert = require('assert');
5+
const suite = new (require('benchmark')).Suite;
6+
const recurse = require('../lib/recurse');
7+
const tail = require('../lib/tail');
8+
const iter = require('../lib/iter');
9+
10+
/* eslint new-parens: 0 */
11+
// const suite = new Suite
12+
13+
/* eslint func-names: 0 */
14+
suite
15+
.add('recurse', () => { recurse(20); })
16+
.add('tail', () => { tail(20); })
17+
.add('iter', () => { iter(20); })
18+
.on('complete', function () {
19+
console.log('result: ');
20+
// console.log(this)
21+
this.forEach((result) => {
22+
console.log(result.name, result.count, result.times.elapsed);
23+
});
24+
assert.equal(
25+
this.filter('fastest').map('name'),
26+
'iter',
27+
'expect iter to be the fastest',
28+
);
29+
})
30+
.run();

examples/fibonacci/package.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "kit-fibonacci",
3+
"version": "1.0.2",
4+
"description": "fastfib 使用 JavaScript 尽可能快地来计算斐波那契数列。",
5+
"main": "lib/index.js",
6+
"scripts": {
7+
"prepublishOnly": "npm run lib",
8+
"lib": "babel src -d lib",
9+
"test": "node test/fastfib.spec.js && node benchmark"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/jskit/kit-fibonacci.git"
14+
},
15+
"keywords": [
16+
"fibonacci",
17+
"fastfib",
18+
"kitjs"
19+
],
20+
"author": "cloudyan <[email protected]>",
21+
"license": "MIT",
22+
"bugs": {
23+
"url": "https://github.com/jskit/kit-fibonacci/issues"
24+
},
25+
"homepage": "https://github.com/jskit/kit-fibonacci#readme",
26+
"dependencies": {},
27+
"devDependencies": {
28+
"babel-core": "^6.26.0",
29+
"babel-eslint": "^8.0.3",
30+
"babel-plugin-add-module-exports": "^0.2.1",
31+
"babel-plugin-transform-runtime": "^6.23.0",
32+
"babel-preset-env": "^1.6.0",
33+
"babel-preset-es2015": "^6.24.1",
34+
"babel-preset-stage-2": "^6.24.1",
35+
"benchmark": "^2.1.4"
36+
}
37+
}

examples/fibonacci/src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// import recurse from './recurse'
2+
// import tail from './tail'
3+
import iter from './iter';
4+
5+
export default iter;

examples/fibonacci/src/iter.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
/**
3+
* 迭代方式来实现斐波那契数列
4+
*
5+
* @param {number} n
6+
* @returns
7+
*/
8+
function iter(n) {
9+
let current = 0;
10+
let next = 1;
11+
/* eslint no-plusplus: 0 */
12+
for (let i = 0; i < n; i++) {
13+
const swap = current;
14+
current = next;
15+
next = swap + next;
16+
}
17+
return current;
18+
}
19+
20+
export default iter;

examples/fibonacci/src/recurse.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
3+
/**
4+
* 递归方法实现斐波那契数列
5+
*
6+
* @param {number} n
7+
* @returns
8+
*/
9+
function recurse(n) {
10+
if (n === 0) return 0;
11+
if (n === 1) return 1;
12+
return recurse(n - 1) + recurse(n - 2);
13+
}
14+
15+
export default recurse;

examples/fibonacci/src/tail.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
/**
3+
* 尾递归优化
4+
*
5+
* @param {number} n
6+
* @param {number} current
7+
* @param {number} next
8+
* @returns
9+
*/
10+
function fib(n, current, next) {
11+
if (n === 0) return current;
12+
return fib(n - 1, next, current + next);
13+
}
14+
15+
/**
16+
* 尾递归优化实现斐波那契数列
17+
*
18+
* @param {number} n
19+
* @returns
20+
*/
21+
function tail(n) {
22+
return fib(n, 0, 1);
23+
}
24+
25+
export default tail;

examples/fibonacci/ts/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// import recurse from './recurse'
2+
// import tail from './tail'
3+
import iter from './iter';
4+
5+
export default iter;

examples/fibonacci/ts/iter.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
/**
3+
* 迭代方式来实现斐波那契数列
4+
*
5+
* @param {number} n
6+
* @returns {number}
7+
*/
8+
function iter(n: number): number {
9+
let current = 0;
10+
let next = 1;
11+
/* eslint no-plusplus: 0 */
12+
for (let i = 0; i < n; i++) {
13+
const swap = current;
14+
current = next;
15+
next = swap + next;
16+
}
17+
return current;
18+
}
19+
20+
// test
21+
console.log(iter(5))
22+
console.log(iter(15))
23+
24+
export default iter;

examples/fibonacci/ts/recurse.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
/**
3+
* 递归方法实现斐波那契数列
4+
*
5+
* @param {number} n
6+
* @returns {*}
7+
*/
8+
function recurse(n: number): any {
9+
if (n === 0) return 0;
10+
if (n === 1) return 1;
11+
return recurse(n - 1) + recurse(n - 2);
12+
}
13+
14+
export default recurse;

examples/fibonacci/ts/tail.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
/**
3+
* 尾递归优化
4+
*
5+
* @param {number} n
6+
* @param {number} current
7+
* @param {number} next
8+
* @returns {*}
9+
*/
10+
function fib(n: number, current: number, next: number): any {
11+
if (n === 0) return current;
12+
return fib(n - 1, next, current + next);
13+
}
14+
15+
/**
16+
* 尾递归优化实现斐波那契数列
17+
*
18+
* @param {number} n
19+
* @returns
20+
*/
21+
function tail(n: number) {
22+
return fib(n, 0, 1);
23+
}
24+
25+
export default tail;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
/**
3+
* 迭代方式来实现斐波那契数列
4+
*
5+
* @param {number} n
6+
* @returns {number}
7+
*/
8+
function iter(n: number): number {
9+
let current = 0;
10+
let next = 1;
11+
/* eslint no-plusplus: 0 */
12+
for (let i = 0; i < n; i++) {
13+
const swap = current;
14+
current = next;
15+
next = swap + next;
16+
}
17+
return current;
18+
}
19+
20+
// test
21+
console.log(iter(5))
22+
console.log(iter(15))

0 commit comments

Comments
 (0)