Skip to content

Commit e78d44a

Browse files
committed
Add some solution
1 parent aa5f557 commit e78d44a

File tree

5 files changed

+308
-3
lines changed

5 files changed

+308
-3
lines changed

Diff for: algorithms/greedy.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
### 分配问题
2121

22-
- [455.分发饼干](https://github.com/suukii/91-days-algorithm/blob/master/topics/greedy/ext-assign-cookies.md)
23-
- [135.分发糖果](https://github.com/suukii/91-days-algorithm/blob/master/topics/greedy/ext-candy.md)
22+
- [455.分发饼干](../topics/greedy/ext-assign-cookies.md)
23+
- [135.分发糖果](../topics/greedy/ext-candy.md)
2424

2525
### 区间问题
2626

27-
- [435.无重叠区间](https://github.com/suukii/91-days-algorithm/blob/master/topics/greedy/ext-non-overlapping-intervals.md)
27+
- [435.无重叠区间](../topics/greedy/ext-non-overlapping-intervals.md)
2828

2929
TODO
3030

Diff for: algorithms/string-searching.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# 字符串匹配算法
2+
3+
在一个长字符串或文章中,找出它是否包含一个或多个模式字符串及其位置。可以应用于生物基因匹配、信息检索等。
4+
5+
## Brute Force
6+
7+
数据量不大的情况下,可以使用固定长度的滑动窗口枚举长串的所有子串,逐一与模式串进行比较。
8+
9+
- 防御性编程(如模式串长度大于长串长度)
10+
- 初始化长度为模式串长度的滑动窗口
11+
- 将当前窗口的子串与模式串进行比较,若匹配成功,则记录相关信息如下标等
12+
- 将滑动窗口后移一格
13+
14+
## Rabin-Karp (RK)
15+
16+
Rabin-Karp 是用于字符串匹配的算法
17+
18+
JavaScript Code
19+
20+
TODO
21+
22+
```js
23+
/**
24+
* @param {string} haystack
25+
* @param {string} needle
26+
* @return {number}
27+
*/
28+
var strStr = function (haystack, needle) {
29+
if (!haystack || !needle || haystack.length < needle.length) return -1;
30+
31+
const n = haystack.length,
32+
m = needle.length;
33+
34+
let hash1 = initHash(haystack, 0, m);
35+
const hash2 = initHash(needle, 0, m);
36+
37+
for (let i = 0; i <= n - m; i++) {
38+
if (i > 0 && i <= n - m) {
39+
hash1 = rehash(haystack, hash1, i - 1, i + m - 1, m);
40+
}
41+
42+
if (hash1 === hash2 && compare(haystack, needle, i)) {
43+
return i;
44+
}
45+
}
46+
47+
return -1;
48+
49+
// ********************************************
50+
function initHash(string, start, end) {
51+
let hashVal = 0;
52+
for (let i = start; i < end; i++) {
53+
const c = string[i];
54+
hashVal +=
55+
(c.charCodeAt(0) - 'a'.charCodeAt(0)) *
56+
Math.pow(26, end - start);
57+
}
58+
return hashVal;
59+
}
60+
61+
function rehash(string, hashVal, oldIdx, newIdx, patLen) {
62+
return (
63+
(hashVal -
64+
(string.charCodeAt(oldIdx) - 'a'.charCodeAt(0) + 1) *
65+
Math.pow(26, patLen - 1)) *
66+
26 +
67+
(string.charCodeAt(newIdx) - 'a'.charCodeAt(0) + 1)
68+
);
69+
}
70+
71+
function compare(string, pattern, start) {
72+
for (let i = 0; i < pattern.length; i++) {
73+
if (string[i + start] !== pattern[i]) return false;
74+
}
75+
return true;
76+
}
77+
};
78+
79+
var a = strStr('lcode', 'code');
80+
81+
console.log(a);
82+
```

Diff for: medium/prune/50.combination-sum-ii.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 40. 组合总和 II
2+
3+
https://leetcode-cn.com/problems/combination-sum-ii
4+
5+
## 题目描述
6+
7+
```
8+
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
9+
10+
candidates 中的每个数字在每个组合中只能使用一次。
11+
12+
说明:
13+
14+
所有数字(包括目标数)都是正整数。
15+
解集不能包含重复的组合。
16+
示例 1:
17+
18+
输入: candidates = [10,1,2,7,6,1,5], target = 8,
19+
所求解集为:
20+
[
21+
[1, 7],
22+
[1, 2, 5],
23+
[2, 6],
24+
[1, 1, 6]
25+
]
26+
示例 2:
27+
28+
输入: candidates = [2,5,2,1,2], target = 5,
29+
所求解集为:
30+
[
31+
[1,2,2],
32+
[5]
33+
]
34+
35+
来源:力扣(LeetCode)
36+
链接:https://leetcode-cn.com/problems/combination-sum-ii
37+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
38+
```
39+
40+
## 思路
41+
42+
## 复杂度分析
43+
44+
- 时间复杂度:
45+
- 空间复杂度:
46+
47+
## 代码
48+
49+
JavaScript Code
50+
51+
```js
52+
/**
53+
* @param {number[]} candidates
54+
* @param {number} target
55+
* @return {number[][]}
56+
*/
57+
var combinationSum2 = function (candidates, target) {
58+
const helper = (candidates, arr, remain, cur, res) => {
59+
for (let i = cur; i < candidates.length; i++) {
60+
if (candidates[i] > remain) continue
61+
if (candidates[i] === candidates[i - 1] && i > cur) continue
62+
63+
if (candidates[i] === remain) {
64+
res.push([...arr, candidates[i]])
65+
return
66+
}
67+
68+
arr.push(candidates[i])
69+
helper(candidates, [...arr], remain - candidates[i], i + 1, res)
70+
arr.pop()
71+
}
72+
}
73+
74+
const res = []
75+
candidates.sort((a, b) => a - b)
76+
helper(candidates, [], target, 0, res)
77+
return res
78+
}
79+
```

Diff for: medium/prune/51.permutations-ii.md

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# 47. 全排列 II
2+
3+
https://leetcode-cn.com/problems/permutations-ii
4+
5+
## 题目描述
6+
7+
```
8+
给定一个可包含重复数字的序列,返回所有不重复的全排列。
9+
10+
示例:
11+
12+
输入: [1,1,2]
13+
输出:
14+
[
15+
[1,1,2],
16+
[1,2,1],
17+
[2,1,1]
18+
]
19+
20+
来源:力扣(LeetCode)
21+
链接:https://leetcode-cn.com/problems/permutations-ii
22+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
23+
24+
25+
```
26+
27+
## 思路
28+
29+
## 复杂度分析
30+
31+
- 时间复杂度:
32+
- 空间复杂度:
33+
34+
## 代码
35+
36+
JavaScript Code
37+
38+
```js
39+
/**
40+
* @param {number[]} nums
41+
* @return {number[][]}
42+
*/
43+
var permuteUnique = function (nums) {
44+
const backtrack = (nums, path, res, visited) => {
45+
if (path.length === nums.length) {
46+
res.push(path);
47+
return;
48+
}
49+
50+
for (let i = 0; i < nums.length; i++) {
51+
if (
52+
visited[i] ||
53+
(i > 0 && nums[i] === nums[i - 1] && visited[i - 1])
54+
)
55+
continue;
56+
visited[i] = true;
57+
path.push(nums[i]);
58+
backtrack(nums, [...path], res, visited);
59+
path.pop();
60+
visited[i] = false;
61+
}
62+
};
63+
64+
const res = [];
65+
nums.sort((a, b) => a - b);
66+
backtrack(nums, [], res, Array(nums.length));
67+
return res;
68+
};
69+
```

Diff for: medium/rk-kpm/52.implement-strstr.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# 28. 实现 strStr()
2+
3+
https://leetcode-cn.com/problems/implement-strstr/
4+
5+
## 题目描述
6+
7+
```
8+
实现 strStr() 函数。
9+
10+
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1。
11+
12+
示例 1:
13+
14+
输入: haystack = "hello", needle = "ll"
15+
输出: 2
16+
示例 2:
17+
18+
输入: haystack = "aaaaa", needle = "bba"
19+
输出: -1
20+
说明:
21+
22+
当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
23+
24+
对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
25+
26+
来源:力扣(LeetCode)
27+
链接:https://leetcode-cn.com/problems/implement-strstr
28+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
29+
```
30+
31+
## 方法 1:暴力
32+
33+
### 思路
34+
35+
将长串的所有子串与短串进行一一对比。
36+
37+
### 复杂度分析
38+
39+
- 时间复杂度:$O(M*N)$,M 是长串的长度,N 是短串的长度。
40+
- 空间复杂度:$O(1)$。
41+
42+
### 代码
43+
44+
JavaScript Code
45+
46+
```js
47+
/**
48+
* @param {string} haystack
49+
* @param {string} needle
50+
* @return {number}
51+
*/
52+
var strStr = function (haystack, needle) {
53+
if (!needle) return 0;
54+
55+
let l = 0,
56+
r = needle.length;
57+
if (haystack[l] === needle[0]) {
58+
if (compare(haystack, needle, l, r)) return l;
59+
}
60+
l++;
61+
r++;
62+
}
63+
return -1;
64+
65+
// ********************************
66+
function compare(long, short, l, r) {
67+
for (let i = 0; i < r - l; i++) {
68+
if (long[i + l] !== short[i]) return false;
69+
}
70+
return true;
71+
}
72+
};
73+
```
74+
75+
更多题解可以访问:[https://github.com/suukii/91-days-algorithm](https://github.com/suukii/91-days-algorithm)

0 commit comments

Comments
 (0)