Skip to content

Commit f12049b

Browse files
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
2 parents 122a8e6 + e01a385 commit f12049b

5 files changed

+95
-5
lines changed

Diff for: problems/0090.子集II.md

+31-3
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ impl Solution {
440440
let len = nums.len();
441441
// if start_index >= len { return; }
442442
for i in start_index..len {
443-
if i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false { continue; }
443+
if i > 0 && nums[i] == nums[i - 1] && !used[i - 1] { continue; }
444444
path.push(nums[i]);
445445
used[i] = true;
446446
Self::backtracking(result, path, nums, i + 1, used);
@@ -449,18 +449,46 @@ impl Solution {
449449
}
450450
}
451451

452-
pub fn subsets_with_dup(nums: Vec<i32>) -> Vec<Vec<i32>> {
452+
pub fn subsets_with_dup(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
453453
let mut result: Vec<Vec<i32>> = Vec::new();
454454
let mut path: Vec<i32> = Vec::new();
455455
let mut used = vec![false; nums.len()];
456-
let mut nums = nums;
457456
nums.sort();
458457
Self::backtracking(&mut result, &mut path, &nums, 0, &mut used);
459458
result
460459
}
461460
}
462461
```
463462

463+
set 去重版本
464+
465+
```rust
466+
use std::collections::HashSet;
467+
impl Solution {
468+
pub fn subsets_with_dup(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
469+
let mut res = HashSet::new();
470+
let mut path = vec![];
471+
nums.sort();
472+
Self::backtracking(&nums, &mut path, &mut res, 0);
473+
res.into_iter().collect()
474+
}
475+
476+
pub fn backtracking(
477+
nums: &Vec<i32>,
478+
path: &mut Vec<i32>,
479+
res: &mut HashSet<Vec<i32>>,
480+
start_index: usize,
481+
) {
482+
res.insert(path.clone());
483+
for i in start_index..nums.len() {
484+
path.push(nums[i]);
485+
Self::backtracking(nums, path, res, i + 1);
486+
path.pop();
487+
}
488+
}
489+
}
490+
```
491+
464492
### C
465493

466494
```c

Diff for: problems/0203.移除链表元素.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
66

77

8-
> 链表操作中,可以使用原链表来直接进行删除操作,也可以设置一个虚拟头结点在进行删除操作,接下来看一看哪种方式更方便。
8+
> 链表操作中,可以使用原链表来直接进行删除操作,也可以设置一个虚拟头结点再进行删除操作,接下来看一看哪种方式更方便。
99
1010
# 203.移除链表元素
1111

Diff for: problems/1207.独一无二的出现次数.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ class Solution {
9898
```
9999

100100
Python:
101-
```python
101+
```python
102+
# 方法 1: 数组在哈西法的应用
102103
class Solution:
103104
def uniqueOccurrences(self, arr: List[int]) -> bool:
104105
count = [0] * 2002
@@ -113,6 +114,26 @@ class Solution:
113114
return False
114115
return True
115116
```
117+
```python
118+
# 方法 2: map 在哈西法的应用
119+
class Solution:
120+
def uniqueOccurrences(self, arr: List[int]) -> bool:
121+
ref = dict()
122+
123+
for i in range(len(arr)):
124+
ref[arr[i]] = ref.get(arr[i], 0) + 1
125+
126+
value_list = sorted(ref.values())
127+
128+
for i in range(len(value_list) - 1):
129+
if value_list[i + 1] == value_list[i]:
130+
return False
131+
132+
return True
133+
134+
```
135+
136+
116137
Go:
117138

118139
JavaScript:

Diff for: problems/二叉树理论基础.md

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
* 它的左、右子树也分别为二叉排序树
6060

6161
下面这两棵树都是搜索树
62+
6263
<img src='https://img-blog.csdnimg.cn/20200806190304693.png' width=600> </img></div>
6364

6465

Diff for: problems/算法模板.md

+40
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,46 @@ Java:
803803

804804

805805
Python:
806+
## 二分查找法
807+
```python
808+
def binarysearch(nums, target):
809+
low = 0
810+
high = len(nums) - 1
811+
while (low <= high):
812+
mid = (high + low)//2
813+
814+
if (nums[mid] < target):
815+
low = mid + 1
816+
817+
if (nums[mid] > target):
818+
high = mid - 1
819+
820+
if (nums[mid] == target):
821+
return mid
822+
823+
return -1
824+
```
825+
826+
## KMP
827+
828+
```python
829+
def kmp(self, a, s):
830+
# a: length of the array
831+
# s: string
832+
833+
next = [0]*a
834+
j = 0
835+
next[0] = 0
836+
837+
for i in range(1, len(s)):
838+
while j > 0 and s[j] != s[i]:
839+
j = next[j - 1]
840+
841+
if s[j] == s[i]:
842+
j += 1
843+
next[i] = j
844+
return next
845+
```
806846

807847

808848
Go:

0 commit comments

Comments
 (0)