mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
This commit is contained in:
@ -440,7 +440,7 @@ impl Solution {
|
|||||||
let len = nums.len();
|
let len = nums.len();
|
||||||
// if start_index >= len { return; }
|
// if start_index >= len { return; }
|
||||||
for i in start_index..len {
|
for i in start_index..len {
|
||||||
if i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false { continue; }
|
if i > 0 && nums[i] == nums[i - 1] && !used[i - 1] { continue; }
|
||||||
path.push(nums[i]);
|
path.push(nums[i]);
|
||||||
used[i] = true;
|
used[i] = true;
|
||||||
Self::backtracking(result, path, nums, i + 1, used);
|
Self::backtracking(result, path, nums, i + 1, used);
|
||||||
@ -449,11 +449,10 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn subsets_with_dup(nums: Vec<i32>) -> Vec<Vec<i32>> {
|
pub fn subsets_with_dup(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
|
||||||
let mut result: Vec<Vec<i32>> = Vec::new();
|
let mut result: Vec<Vec<i32>> = Vec::new();
|
||||||
let mut path: Vec<i32> = Vec::new();
|
let mut path: Vec<i32> = Vec::new();
|
||||||
let mut used = vec![false; nums.len()];
|
let mut used = vec![false; nums.len()];
|
||||||
let mut nums = nums;
|
|
||||||
nums.sort();
|
nums.sort();
|
||||||
Self::backtracking(&mut result, &mut path, &nums, 0, &mut used);
|
Self::backtracking(&mut result, &mut path, &nums, 0, &mut used);
|
||||||
result
|
result
|
||||||
@ -461,6 +460,35 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
set 去重版本
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use std::collections::HashSet;
|
||||||
|
impl Solution {
|
||||||
|
pub fn subsets_with_dup(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
|
||||||
|
let mut res = HashSet::new();
|
||||||
|
let mut path = vec![];
|
||||||
|
nums.sort();
|
||||||
|
Self::backtracking(&nums, &mut path, &mut res, 0);
|
||||||
|
res.into_iter().collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn backtracking(
|
||||||
|
nums: &Vec<i32>,
|
||||||
|
path: &mut Vec<i32>,
|
||||||
|
res: &mut HashSet<Vec<i32>>,
|
||||||
|
start_index: usize,
|
||||||
|
) {
|
||||||
|
res.insert(path.clone());
|
||||||
|
for i in start_index..nums.len() {
|
||||||
|
path.push(nums[i]);
|
||||||
|
Self::backtracking(nums, path, res, i + 1);
|
||||||
|
path.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### C
|
### C
|
||||||
|
|
||||||
```c
|
```c
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||||
|
|
||||||
|
|
||||||
> 链表操作中,可以使用原链表来直接进行删除操作,也可以设置一个虚拟头结点在进行删除操作,接下来看一看哪种方式更方便。
|
> 链表操作中,可以使用原链表来直接进行删除操作,也可以设置一个虚拟头结点再进行删除操作,接下来看一看哪种方式更方便。
|
||||||
|
|
||||||
# 203.移除链表元素
|
# 203.移除链表元素
|
||||||
|
|
||||||
|
@ -99,6 +99,7 @@ class Solution {
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
```python
|
```python
|
||||||
|
# 方法 1: 数组在哈西法的应用
|
||||||
class Solution:
|
class Solution:
|
||||||
def uniqueOccurrences(self, arr: List[int]) -> bool:
|
def uniqueOccurrences(self, arr: List[int]) -> bool:
|
||||||
count = [0] * 2002
|
count = [0] * 2002
|
||||||
@ -113,6 +114,26 @@ class Solution:
|
|||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
```
|
```
|
||||||
|
```python
|
||||||
|
# 方法 2: map 在哈西法的应用
|
||||||
|
class Solution:
|
||||||
|
def uniqueOccurrences(self, arr: List[int]) -> bool:
|
||||||
|
ref = dict()
|
||||||
|
|
||||||
|
for i in range(len(arr)):
|
||||||
|
ref[arr[i]] = ref.get(arr[i], 0) + 1
|
||||||
|
|
||||||
|
value_list = sorted(ref.values())
|
||||||
|
|
||||||
|
for i in range(len(value_list) - 1):
|
||||||
|
if value_list[i + 1] == value_list[i]:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
JavaScript:
|
JavaScript:
|
||||||
|
@ -59,6 +59,7 @@
|
|||||||
* 它的左、右子树也分别为二叉排序树
|
* 它的左、右子树也分别为二叉排序树
|
||||||
|
|
||||||
下面这两棵树都是搜索树
|
下面这两棵树都是搜索树
|
||||||
|
|
||||||
<img src='https://img-blog.csdnimg.cn/20200806190304693.png' width=600> </img></div>
|
<img src='https://img-blog.csdnimg.cn/20200806190304693.png' width=600> </img></div>
|
||||||
|
|
||||||
|
|
||||||
|
@ -803,6 +803,46 @@ Java:
|
|||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
## 二分查找法
|
||||||
|
```python
|
||||||
|
def binarysearch(nums, target):
|
||||||
|
low = 0
|
||||||
|
high = len(nums) - 1
|
||||||
|
while (low <= high):
|
||||||
|
mid = (high + low)//2
|
||||||
|
|
||||||
|
if (nums[mid] < target):
|
||||||
|
low = mid + 1
|
||||||
|
|
||||||
|
if (nums[mid] > target):
|
||||||
|
high = mid - 1
|
||||||
|
|
||||||
|
if (nums[mid] == target):
|
||||||
|
return mid
|
||||||
|
|
||||||
|
return -1
|
||||||
|
```
|
||||||
|
|
||||||
|
## KMP
|
||||||
|
|
||||||
|
```python
|
||||||
|
def kmp(self, a, s):
|
||||||
|
# a: length of the array
|
||||||
|
# s: string
|
||||||
|
|
||||||
|
next = [0]*a
|
||||||
|
j = 0
|
||||||
|
next[0] = 0
|
||||||
|
|
||||||
|
for i in range(1, len(s)):
|
||||||
|
while j > 0 and s[j] != s[i]:
|
||||||
|
j = next[j - 1]
|
||||||
|
|
||||||
|
if s[j] == s[i]:
|
||||||
|
j += 1
|
||||||
|
next[i] = j
|
||||||
|
return next
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
Reference in New Issue
Block a user