mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -332,6 +332,66 @@ func maxSlidingWindow(nums []int, k int) []int {
|
||||
|
||||
```
|
||||
|
||||
```go
|
||||
// 封装单调队列的方式解题
|
||||
type MyQueue struct {
|
||||
queue []int
|
||||
}
|
||||
|
||||
func NewMyQueue() *MyQueue {
|
||||
return &MyQueue{
|
||||
queue: make([]int, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MyQueue) Front() int {
|
||||
return m.queue[0]
|
||||
}
|
||||
|
||||
func (m *MyQueue) Back() int {
|
||||
return m.queue[len(m.queue)-1]
|
||||
}
|
||||
|
||||
func (m *MyQueue) Empty() bool {
|
||||
return len(m.queue) == 0
|
||||
}
|
||||
|
||||
func (m *MyQueue) Push(val int) {
|
||||
for !m.Empty() && val > m.Back() {
|
||||
m.queue = m.queue[:len(m.queue)-1]
|
||||
}
|
||||
m.queue = append(m.queue, val)
|
||||
}
|
||||
|
||||
func (m *MyQueue) Pop(val int) {
|
||||
if !m.Empty() && val == m.Front() {
|
||||
m.queue = m.queue[1:]
|
||||
}
|
||||
}
|
||||
|
||||
func maxSlidingWindow(nums []int, k int) []int {
|
||||
queue := NewMyQueue()
|
||||
length := len(nums)
|
||||
res := make([]int, 0)
|
||||
// 先将前k个元素放入队列
|
||||
for i := 0; i < k; i++ {
|
||||
queue.Push(nums[i])
|
||||
}
|
||||
// 记录前k个元素的最大值
|
||||
res = append(res, queue.Front())
|
||||
|
||||
for i := k; i < length; i++ {
|
||||
// 滑动窗口移除最前面的元素
|
||||
queue.Pop(nums[i-k])
|
||||
// 滑动窗口添加最后面的元素
|
||||
queue.Push(nums[i])
|
||||
// 记录最大值
|
||||
res = append(res, queue.Front())
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
```javascript
|
||||
var maxSlidingWindow = function (nums, k) {
|
||||
|
@ -130,7 +130,27 @@ class Solution:
|
||||
return True
|
||||
```
|
||||
|
||||
Python写法二(没有使用数组作为哈希表,只是介绍defaultdict这样一种解题思路):
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def isAnagram(self, s: str, t: str) -> bool:
|
||||
from collections import defaultdict
|
||||
|
||||
s_dict = defaultdict(int)
|
||||
t_dict = defaultdict(int)
|
||||
|
||||
for x in s:
|
||||
s_dict[x] += 1
|
||||
|
||||
for x in t:
|
||||
t_dict[x] += 1
|
||||
|
||||
return s_dict == t_dict
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
```go
|
||||
func isAnagram(s string, t string) bool {
|
||||
if len(s)!=len(t){
|
||||
|
@ -218,7 +218,7 @@ class Solution:
|
||||
# 假设对正整数 i 拆分出的第一个正整数是 j(1 <= j < i),则有以下两种方案:
|
||||
# 1) 将 i 拆分成 j 和 i−j 的和,且 i−j 不再拆分成多个正整数,此时的乘积是 j * (i-j)
|
||||
# 2) 将 i 拆分成 j 和 i−j 的和,且 i−j 继续拆分成多个正整数,此时的乘积是 j * dp[i-j]
|
||||
for j in range(1, i):
|
||||
for j in range(1, i - 1):
|
||||
dp[i] = max(dp[i], max(j * (i - j), j * dp[i - j]))
|
||||
return dp[n]
|
||||
```
|
||||
|
@ -268,7 +268,7 @@ int main() {
|
||||
Java:
|
||||
|
||||
```java
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) {
|
||||
int[] weight = {1, 3, 4};
|
||||
int[] value = {15, 20, 30};
|
||||
int bagSize = 4;
|
||||
@ -307,6 +307,41 @@ Java:
|
||||
|
||||
|
||||
Python:
|
||||
```python
|
||||
def test_2_wei_bag_problem1(bag_size, weight, value) -> int:
|
||||
rows, cols = len(weight), bag_size + 1
|
||||
dp = [[0 for _ in range(cols)] for _ in range(rows)]
|
||||
res = 0
|
||||
|
||||
# 初始化dp数组.
|
||||
for i in range(rows):
|
||||
dp[i][0] = 0
|
||||
first_item_weight, first_item_value = weight[0], value[0]
|
||||
for j in range(1, cols):
|
||||
if first_item_weight <= j:
|
||||
dp[0][j] = first_item_value
|
||||
|
||||
# 更新dp数组: 先遍历物品, 再遍历背包.
|
||||
for i in range(1, len(weight)):
|
||||
cur_weight, cur_val = weight[i], value[i]
|
||||
for j in range(1, cols):
|
||||
if cur_weight > j: # 说明背包装不下当前物品.
|
||||
dp[i][j] = dp[i - 1][j] # 所以不装当前物品.
|
||||
else:
|
||||
# 定义dp数组: dp[i][j] 前i个物品里,放进容量为j的背包,价值总和最大是多少。
|
||||
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - cur_weight]+ cur_val)
|
||||
if dp[i][j] > res:
|
||||
res = dp[i][j]
|
||||
|
||||
print(dp)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
bag_size = 4
|
||||
weight = [1, 3, 4]
|
||||
value = [15, 20, 30]
|
||||
test_2_wei_bag_problem1(bag_size, weight, value)
|
||||
```
|
||||
|
||||
|
||||
Go:
|
||||
|
Reference in New Issue
Block a user