Skip to content

Commit 788e3fd

Browse files
committed
update array-part lastIndexOf判断是否有重复
1 parent 4686a97 commit 788e3fd

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

array-part/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ console.log(myArray.sort(dynamicSort('-age'))); // 按降序排列
9292
- 负数 = arr.length - num
9393
- lastIndexOf() 从后往前查
9494

95+
```js
96+
arr.indexOf(v) === arr.lastIndexOf(v) ? '唯一' : '重复'
97+
```
98+
9599
## 迭代
96100

97101

@@ -425,3 +429,36 @@ var r2 = arr => arr.join(',').split(',')
425429
// reduce 尾递归
426430
const r3 = arr => arr.reduce((a, b) => a.concat(Array.isArray(b) ? r3(b) : b), [])
427431
```
432+
433+
### 莱文斯坦距离
434+
435+
> [Wiki](http://en.wikipedia.org/wiki/Levenshtein_distance)
436+
437+
>[js的实现](https://rosettacode.org/wiki/Levenshtein_distance#ES5)
438+
439+
>[在Regularjs中的使用](https://leeluolee.github.io/2014/10/21/how-ls-help-you-diff-two-array/)
440+
441+
又称Levenshtein距离,是编辑距离的一种。指两个字串之间,由一个转成另一个所需的最少编辑操作次数。许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。
442+
443+
![ld算法](http://upload.wikimedia.org/math/d/4/f/d4f80cafb626ae9d9b8dc748360f61ec.png)
444+
445+
```js
446+
function levenshtein(x, y) {
447+
const m = x.length
448+
const n = y.length
449+
if (x === y) return 0
450+
if (!m) return n
451+
if (!n) return m
452+
let a = [...Array(m + 1).keys()]
453+
let b = []
454+
for (let i = 0; i < n; i++) {
455+
b[0] = i + 1
456+
for (let j = 0; j < m; j++) {
457+
const k = y.charAt(i) === x.charAt(j) ? 0 : 1
458+
b[j + 1] = Math.min(b[j] + 1, a[j + 1] + 1, a[j] + k)
459+
}
460+
[a, b] = [b, a]
461+
}
462+
return a[m]
463+
}
464+
```

0 commit comments

Comments
 (0)